Private/Notifiers/Invoke-NotifierInfluxDB.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

function Invoke-NotifierInfluxDB {
    [cmdletbinding()]
    param(
        [parameter(Mandatory, ValueFromPipeline)]
        [ValidateScript({ $_.PSObject.TypeNames[0] -eq 'Watchmen.Notifier.InfluxDB' })]
        [pscustomobject]$Notifier,

        [parameter(Mandatory)]
        [ValidateScript({ $_.PSObject.TypeNames[0] -eq 'OperationValidationResult' })]
        [pscustomobject]$Results
    )

    begin {}

    process {
        # InfluxDB needs spaces and commas escaped with '\'
        # https://docs.influxdata.com/influxdb/v0.13/write_protocols/line/#key
        function Escape([string]$value) {
            return $value -replace ' ', '\ ' -replace ',', '\,' -replace '=', '\='
        }

        if ($Notifier.Enabled) {

            $o = ($Notifier | Format-Table -Property * -AutoSize | Out-String)
            Write-Debug -Message "InfluxDB notifier called with options:`n$o"

            # Tags
            $tags = [ordered]@{
                context = Escape($Results.RawResult.Context)
                describe = Escape($Results.RawResult.Describe)
                filename = Escape($Results.ShortName)
                module = Escape($Results.Module)
                test = Escape($Results.RawResult.Name)
                host = ($env:computerName).ToLower()
                fqdn = ([System.Net.Dns]::GetHostByName(($env:computerName)) | Select-Object -ExpandProperty hostname).ToLower()
            }
            # Add any additional tags that may have been specified on the notifier
            if ($Notifier.Tags.Keys.Count -gt 0) {
                foreach ($tag in $Notifier.Tags.Keys) {
                    $tags.$Tag = Escape($Notifier.Tags[$tag])
                }
            }

            # Field values for pass/fail result and test duration
            # Force the value to an integer by appending 'i'
            # https://docs.influxdata.com/influxdb/v0.13/write_protocols/line/#fields
            if ($Results.Result -eq 'Passed') {
                $value = '0i'
            } else {
                $value = '1i'
            }
            $durationMS = $Results.RawResult.Time.TotalMilliseconds

            # Unix time
            $time = [int][double]::Parse($(Get-Date -date (Get-Date).ToUniversalTime()-uformat %s))

            # Create metric string
            $metric = $Notifier.MeasurementName
            foreach ($tag in $tags.Keys) {
                $metric += ",$tag=$($tags[$tag])"
            }
            $metric += " durationMS=$durationMS,value=$value $time"

            Write-Debug -Message " $metric"

            $url = "$($Notifier.Url):$($Notifier.Port)/write?db=$($Notifier.Database)"
            if ($Notifier.RetentionPolicy -ne [string]::Empty) {
                $url += "&rp=$($Notifier.RetentionPolicy)"
            }
            $url += '&precision=s'
            $params = @{
                Uri = $url
                Method = 'Post'
                Body = $metric
                UseBasicParsing = $true
                Timeout = $Notifier.Timeout
                UserAgent = $Notifier.UserAgent
                Verbose = $false
            }
            if ($Notifier.Credential) {
                $params.Credential = $Notifier.Credential
            }
            Invoke-RestMethod @params | Out-Null
        }
    }

    end {}    
}