LiveMetrics.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<#
.Synopsis
    Starts logging a live metrics stream to App Insights
.DESCRIPTION
    The logging stream sends a standard set of perfcounters every second, which allows you to monitor essential counters directly in AppInsights.
     
    Please note that this will incur a continous stream of performance and statictics to the azure AppInsights portal,
    this is in the format of a HTTPS traffic stream which will send about 5 to 6 MBytes per hour to https://rt.services.visualstudio.com
 
    This information is sent asynchronously from the powershell script or module and requires no interaction other that starting or stopping.
    Please note that there is only a single LiveMetrics view for each Instrumentation key.
    > If multiple instances of the same script log to the same Key the counters will be combines
    > if you need to view seperate LiveMetrics, you need to instrument your scripts with different keys.
 
    Implementation uses the Microsoft.ApplicationInsights.PerfCounterCollector package
.Note
    As Powershell scripts are not the main scenario, there are a number of metrics that are not relevant
    the current implementation does support :
    - Server Health
        - Memory
        - CPU
 
    -Incoming Requests
        - Not relevant for Powershell
 
    Future capabilities :
 
    - Dependency Calls - Requires explicit dependency tracking
        - Calls/Sec
        - Duration
        - Failed/Sec
 
.Link
    https://azure.microsoft.com/en-us/blog/live-metrics-stream/
    http://apmtips.com/blog/2017/02/13/enable-application-insights-live-metrics-from-code/
.Link
    Send-AIDependecy
 
.EXAMPLE
    #Start sending of live performance data
    Start-AILiveMetrics -key $key
 
.EXAMPLE
    #Start sending of live performance data,
    #but limit the inclusion of telemetry items; (note that this functionality is not yet fully implemented)
 
    Start-AILiveMetrics -key $key -DisableFullTelemetryItems
#>

function Start-AILiveMetrics
{
[CmdletBinding()]
[Alias('Start-AIQuickPulse')]
[OutputType([void])]
Param
(
    # Instrumentation key
    [Parameter(Mandatory=$false)]
    [string]$Key= [Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]::Active.InstrumentationKey  #Get earlier provided / current key
        
    # DisableFullTelemetryItems - #Needs QuickPulse Processor to provide statistics
    #[switch]$DisableFullTelemetryItems
)

#TODO : Verify Implementation : http://apmtips.com/blog/2017/02/13/enable-application-insights-live-metrics-from-code/
#TODO Add options for DisableFullTelemetryItems DisableTopCpuProcesses ?

    Try { 
        #Make sure a Key is set if one is provided
        if ( [string]::IsNullOrEmpty($key) -eq $false) {
            Write-verbose 'Start-AILiveMetrics - Save IKey'
            # This is a singleton that controls all New AI Client sessions for this process from this moment
            [Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]::Active.InstrumentationKey = $key
        }

        #Check for a specified AI client
        if ([Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]::Active.InstrumentationKey -eq $null) {
            throw [System.Management.Automation.PSArgumentNullException]::new($Global:AISingleton.ErrNoClient)
        }

        #If one is running : Close it
        if ($Global:AISingleton.QuickPulse) {
            Write-verbose "Start-AILiveMetrics - Stop and replace existing Live Metrics"
            Stop-AIQuickPulse
        }
    
        #Create a new QuickPulse / LiveMetric Processor
        $Global:AISingleton.QuickPulse = [Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule]::new()

        #Copy the settings
        # $Global:AISingleton.QuickPulse.DisableFullTelemetryItems = $DisableFullTelemetryItems
         Write-verbose "Start-AILiveMetrics - Initialize"
        $Global:AISingleton.QuickPulse.Initialize( [Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]::Active )

<# void Initialize(
                Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration,
                Microsoft.ApplicationInsights, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 configuration)
        void ITelemetryModule.Initialize(Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration, Microsoft.ApplicationInsights, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 configuration)
#>

    } catch { 
        $ex = $_ 
        Write-Warning "Could not initialise AI Live Metrics"
    }
}

<#
.Synopsis
   Stop the sending of Live metrics for this powershell process.
.EXAMPLE
   Stop-AILiveMetrics
#>

function Stop-AILiveMetrics 
{
[CmdletBinding()]
[Alias('Stop-AIQuickPulse')]
[OutputType([void])]
Param
()
    #If one is running : Close it
    if ($Global:AISingleton.QuickPulse) {
         Write-verbose "Stop-AILiveMetrics - Stopping and disposing client"
        $Global:AISingleton.QuickPulse.Dispose()
        $Global:AISingleton.QuickPulse = $null
    } else {
        Write-Warning "Application Insights Live Metrics not Started"

    }
}