Skip to content

PowerShell module for pushing log entries to Loki

License

Notifications You must be signed in to change notification settings

stefanes/PSLoki

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PSLoki

PowerShell module for pushing log entries to Loki.

Latest version Download count

✔️ See CHANGELOG.md for what's new!

Grafana

Installation

Using the latest version of PowerShellGet:

Install-Module -Name PSLoki -Repository PSGallery -Scope CurrentUser -Force -PassThru
Import-Module -Name PSLoki -Force -PassThru

Or if you already have the module installed, update to the latest version:

Update-Module -Name PSLoki
Import-Module -Name PSLoki -Force -PassThru

Authentication

For use with e.g. Grafana Cloud you must have an account to access the API. Access tokens (API Keys) for Grafana can be generated at https://grafana.com/orgs/[your-user-name]/api-keys.

To authenticate, pass the generated access token using the -AccessToken parameter with each call or set the LOKI_ACCESS_TOKEN environment variable:

$env:LOKI_ACCESS_TOKEN = "<your access token>"

Usage

Use Get-Command -Module PSLoki for a list of functions provided by this module. See the help associated with each function using the Get-Help command, e.g. Get-Help Get-LokiLogEntry -Detailed, and the documentation available in docs for more details:

Examples

Get Unix Epoch for current date/time

$timestamp = Get-LokiTimestamp
Write-Host "Current Unix Epoch (in nanoseconds) is: $timestamp"

Send log entries to Loki

$labels = @{
    'label' = 'value'
    'foo'   = 'bar'
}
$logEntries = @(
    @{
        time = "1666644815000000000"
        line = "log something"
    }
    @{
        time = "1666644823000000000"
        line = "log something else"
    }
)
$response = Send-LokiLogEntry -URI "https://logs-prod-us-central1.grafana.net/loki/api/v1/push" -Labels $labels -Entries $logEntries
Write-Host "Log entries sent to Loki [$($response.StatusCode) $($response.StatusDescription)]"

The endpoint URL can also be specified by setting the LOKI_ENDPOINT or LOKI_HOST environment variables.

Debugging

To view the actual Loki log entries sent in the requests, add the -Debug switch to the command.

Example:

PS> Send-LokiLogEntry -Labels $labels -Entries $logEntries -Debug
DEBUG: Invoking web request: POST https://logs-prod-us-central1.grafana.net/loki/api/v1/push
DEBUG: Loki log entries: {
  "streams": [
    {
      "values": [
        [
          "1666644815000000000",
          "log something"
        ],
        [
          "1666644823000000000",
          "log something else"
        ]
      ],
      "stream": {
        "label": "value",
        "foo": "bar"
      }
    }
  ]
}