I 'm working on a powershell script that sends metrics to Azure Application Insights. I can send a metric sample using this code, np problem.
$telemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
$telemetryClient.TelemetryConfiguration.ConnectionString = 'InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'
$Sample = [Microsoft.ApplicationInsights.DataContracts.MetricTelemetry]::new()
$Sample.Name = 'TotalAppRegs'
$Sample.Timestamp = $now
$Sample.Value = 100
$telemetryClient.TrackMetric($Sample)
$telemetryClient.Flush()
Now I'd like to add some properties to $Sample that, as I understand, will appear as Custom Dimensions in appinsights
$telemetryClient = [Microsoft.ApplicationInsights.TelemetryClient]::new()
$telemetryClient.TelemetryConfiguration.ConnectionString = 'InstrumentationKey=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/'
$Sample = [Microsoft.ApplicationInsights.DataContracts.MetricTelemetry]::new()
$Sample.Name = 'TotalAppRegs'
$Sample.Timestamp = $now
$Sample.Value = 100
$Sample.Properties.Add('key','value')
$telemetryClient.TrackMetric($Sample)
$telemetryClient.Flush()
I get an error: MethodException: Cannot find an overload for "Add" and the argument count: "2". While the definition states:
PS: $Sample.Properties|gm add|fl
TypeName : System.Collections.Concurrent.ConcurrentDictionary`2[[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral,
PublicKeyToken=7cec85d7bea7798e]]
Name : Add
MemberType : Method
Definition : void IDictionary[string,string].Add(string key, string value), void ICollection[KeyValuePair[string,string]].Add(System.Collections.Generic.KeyValuePair[string,string] item), void IDictionary.Add(System.Object key, System.Object value)
What am I doing wrong here?
The TrackMetric
function has an overload which accepts a dictionary to set the customProperties (docs here). The following call will do the trick ( I left out the initialization part)
$p = [System.Collections.Generic.Dictionary[String,String]]::new()
$p.Add('myProperty','myValue')
$telemetryClient.TrackMetric('TotalAppRegs',100, $p)