AIMetrics.psm1

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<#
.Synopsis
    Send a metric in the format of Key = Value
 
    Use TrackMetric to send metrics that are not attached to particular events. For example, you could monitor a queue length at regular intervals.
    Metrics are displayed as statistical charts in metric explorer, but unlike events, you can't search for individual occurrences in diagnostic search.
 
    Number
    ------
    A string that identifies the metric. In the portal, you can select metrics for display by name.
 
    Average
    -------
    Either a single measurement, or the average of several measurements. Should be >=0 to be correctly displayed.
 
    SampleCount
    -----------
    Count of measurements represented by the average. Defaults to 1. Should be >=1.
 
    min
    ---
    The smallest measurement in the sample. Defaults to the average. Should be >= 0.
     
    max
    ---
    The largest measurement in the sample. Defaults to the average. Should be >= 0.
 
.EXAMPLE
        #Report the amount of work in the Q
        Send-AIMetric -Metric "InputQueue" -Value $Q.Count
 
.EXAMPLE
        #Send a range of metrics
        1..100 | %{
            Send-AIMetric -Metric "Counter" -Value $_ -NoStack
        }
 
.EXAMPLE
        $Result = Invoke-SQLQuery -query "Select Count(*) as Users from $TableName" -connection $Connection
        Send-AIMetric -Metric "UsersInDatabase" -Value $Result.Users
 
#>

function Send-AIMetric
{
    [CmdletBinding()]
    #[OutputType([int])]
    Param
    (
        # The Trace Message
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
        [string] $Metric,

        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
        [double] $Value,

        #any custom Properties that need to be added to the event
        [Hashtable]$Properties,

        #The AppInsights Client object to use.
        [Parameter(Mandatory=$false)]
        [Microsoft.ApplicationInsights.TelemetryClient] $Client ,

        #include call stack information (Default)
        [switch] $NoStack,
        #The number of Stacklevels to go up
        [int]$StackWalk = 0,

        #Directly flush the AI events to the service
        [switch] $Flush

    )
    Write-Verbose "Send-AIMetric $Metric = $Value"
    #Check for a specified AI client
    if ($Client -eq $null) {
        If ( ($Global:AISingleton ) -AND ( $Global:AISingleton.Client ) ) {
            #Use Current Client
            $Client = $Global:AISingleton.Client
        }
    }
    #no need to do anything if there is no client
    if ($Client -eq $null) { 
        Write-Verbose 'No AI Client found'
        return 
    }  

    #Setup dictionaries
    $dictProperties = New-Object 'system.collections.generic.dictionary[[string],[string]]'

    #Add the callstack
    if ($NoStack -ne $True) { 
        Write-verbose 'Add Caller information'
        $dictProperties = getCallerInfo -level (2+$StackWalk)
    }
    #Add the Properties to Dictionary
    if ($Properties) { 
        foreach ($h in $Properties.GetEnumerator() ) {
            $dictProperties.Add($h.Name, $h.Value)
        }
    }
    #Send the Metric
    $client.trackMetric($Metric, $Value, $dictProperties);

    #Flush the client is requested
    if ($Flush) { 
        $client.Flush()
    }
}