PSPromFile.psm1

Class PromFileLabel{
    [String]$name
    [string]$value

    PromFileLabel(){

    }

    PromFileLabel([String]$Name,[String]$Value){
        $this.Name = $Name
        $this.value = $Value
    }

    [String]ToString(){
        return "$($this.Name)=`"$($this.Value)`""
    }
}

class PrometheusGauge {
    [string]$metricName
    [string]$helpText
    [string]$value
    [System.Collections.Generic.List[PromFileLabel]] $Labels = [System.Collections.Generic.List[PromFileLabel]]::new()


    PrometheusGauge([string]$name, [string]$help) {
        $this.metricName = $name
        $this.helpText = $help
        
        $this.value = $null
    }

    [Void]AddLabel([PromFileLabel[]]$Labels){
        foreach($label in $labels){

            $this.Labels.Add($Label)
        }
    }


    [string] ToString() {
        $stringBuilder = New-Object System.Text.StringBuilder

        $null = $stringBuilder.AppendLine("# HELP $($this.MetricName) $($this.helpText)")
        $null = $stringBuilder.AppendLine("# TYPE $($this.metricName) gauge")

        if ($this.value) {
            $labelString = ""
            if ($this.labels) {
                $ls = $this.labels.GetEnumerator() | ForEach-Object { $_.ToString() }
                $joined = $ls -join ','
                
                $labelString = "$($this.metricName){$($Joined)}"
                $null = $stringBuilder.AppendLine($labelString)
            }
            #Add emtpy line as prom files need to end with an empty line
            $stringBuilder.AppendLine()
            
        }

        return $stringBuilder.ToString()
    }

    [void] SetValue([string]$value) {
        $this.value = $value
    }
}

Function New-PrometheusMetricGauge {
    [CmdletBinding()]
    Param(


        [Parameter(Mandatory = $true)]
        [ValidatePattern("^[a-zA-Z_:][a-zA-Z0-9_:]*$")]
        [string]$Name,

        [Parameter(Mandatory = $true)]
        [string]$Description,

        [Parameter(Mandatory = $False)]
        [PromFileLabel[]]$Labels,

        [Parameter(Mandatory = $True)]
        $Value
    )

    $gauge = [PrometheusGauge]::new($Name, $Description)
    if($Labels){

        $gauge.AddLabel($Labels)
    }
    $gauge.SetValue($Value)
    return $gauge
}

Function New-PrometheusLabel {
    Param(
        [Parameter(Mandatory = $true)]
        [string]$Name,

        [Parameter(Mandatory = $true)]
        [string]$Value
    )

    $label = [PromFileLabel]::new($Name, $Value)
    return $label
}