jobec / powershell-prom-client

Prometheus instrumentation library for Powershell applications
BSD 2-Clause "Simplified" License
29 stars 10 forks source link

My proof of concept doesn't work #6

Closed DennisL68 closed 1 year ago

DennisL68 commented 1 year ago

I tried setting up the exporter using dynamic values based on what I get using the Cmdlet Get-Function. I don't really care about the Metrics names at the moment, so I'm using a raw conversion.

Import-Module PrometheusExporter

$PlainPerfCounters = Get-Counter

[array]$Global:MetricDescriptors = $null
foreach ($PsCounter in $PlainPerfCounters.CounterSamples) {
    $MetricDescriptorName = $PsCounter.Path -replace '^\\\\.*?\\','' -replace '!|^_|\?|\*|\\|\/|\.$|\.{2}|\[|\]|\(|\)|\%|\s|\-','_'

    $CounterType = @{
        "RateOfCountsPerSecond64" = "gauge"
        "NumberOfItems32" = "gauge"
        "Timer100NsInverse" = "gauge"
        "RawFraction" = "gauge"
        "RateOfCountsPerSecond32" = "gauge"
        "542573824" = "gauge"
    }

    $HelpName = $PsCounter.InstanceName -replace '!|^_|\?|\*|\\|\/|\.$|\.{2}|\[|\]|\(|\)|\%|\s|\-','_'
    if (!$HelpName) {
        $HelpName = '_'
    }

    $MetricDescriptor = New-MetricDescriptor `
        -Name $MetricDescriptorName `
        -Type $CounterType["$($PsCounter.CounterType)"] `
        -Help $HelpName

    $Global:MetricDescriptors += $MetricDescriptor

}

function collector {
    $PlainPerfCounters = Get-Counter

    [array]$Metrics = $null
    for ($i=0; $i -lt $PlainPerfCounters.CounterSamples.Count; $i++) {
        $Metric = New-Metric `
            -MetricDesc $MetricDescriptors[$i] `
            -Value $PlainPerfCounters.CounterSamples[$i].CookedValue

        $Metrics += $Metric
    }

    $Metrics

}

$PerformanceExporter = New-PrometheusExporter -Port 9700
Register-Collector -Exporter $PerformanceExporter  -Collector $Function:collector
$PerformanceExporter.Start()

But when visiting the address as described, the exporter displays the error

Exception calling "Invoke" with "0" argument(s): "Cannot process argument transformation on parameter 'Value'. Connot
convert value "[string]" to type "System.Single". Error: "Input string was not in a correct format.""
DennisL68 commented 1 year ago

Ok, perhaps I should have run this example in specific before posting it. It works without a hitch :)