Skip to content

PowerShell module for publishing metrics to Graphite

License

Notifications You must be signed in to change notification settings

stefanes/PSGraphite

Repository files navigation

PSGraphite

PowerShell module for publishing metrics to Graphite.

Latest version Download count

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

Grafana

✔️ See here for an example of this module in action...

Installation

Using the latest version of PowerShellGet:

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

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

Update-Module -Name PSGraphite
Import-Module -Name PSGraphite -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 GRAPHITE_ACCESS_TOKEN environment variable:

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

Usage

Use Get-Command -Module PSGraphite 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-GraphiteMetric -Detailed, and the documentation available in docs for more details:

Examples

Get Unix Epoch for current date/time

$timestamp = Get-GraphiteTimestamp
Write-Host "Current Unix Epoch is: $timestamp"

Get Graphite metrics data

$graphiteMetrics = Get-GraphiteMetric -Metrics @(
    @{
        name = 'test.series.1'; value = '3.14159'
    }
    @{
        name  = 'test.series.2'
        value = '3.14159'
        tags  = @(
            'tag3=value3'
            'tag4=value4'
        )
    }
) -IntervalInSeconds 10 -Timestamp $timestamp -Tags @('tag1=value1', 'tag2=value2')
Write-Host "Will send the following metrics to Graphite: $graphiteMetrics"

Send metrics to Graphite

$response = Send-GraphiteMetric -URI "https://graphite-blocks-prod-us-central1.grafana.net/graphite" -Metrics $graphiteMetrics
Write-Host "Metrics sent to Graphite [$($response.StatusCode) $($response.StatusDescription)]: $($response.Content | ConvertFrom-Json | Select Invalid, Published)"

The endpoint URL can also be specified by setting the GRAPHITE_ENDPOINT or GRAPHITE_HOST environment variables.

Debugging

To view the actual Graphite metrics sent in the requests, add the -Debug switch to the command.

Example:

PS> Send-GraphiteMetric -Metrics $graphiteMetrics -Debug
DEBUG: Invoking web request: POST https://graphite-blocks-prod-us-central1.grafana.net/graphite
DEBUG: Graphite metrics: [
  {
    "name": "test.series.1",
    "value": 3.14159,
    "interval": 10,
    "time": 1662578870
  },
  {
    "name": "test.series.2",
    "value": 3.14159,
    "interval": 10,
    "time": 1662578870
  }
]