Adding custom properties for each request in Application Insights metrics (VB.NET)
I managed to get it to work as follow:
Imports Microsoft.ApplicationInsights.Channel
Imports Microsoft.ApplicationInsights.DataContracts
Imports Microsoft.ApplicationInsights.Extensibility
Public Class CustomTelemetry
Implements ITelemetryInitializer
Private Sub ITelemetryInitializer_Initialize(telemetry As ITelemetry) Implements ITelemetryInitializer.Initialize
Dim requestTelemetry = CType(telemetry, RequestTelemetry)
If (requestTelemetry Is Nothing) Then
Return
End If
requestTelemetry.Properties.Add("TEST", "TESTERRRRRR")
End Sub
End Class
Add Custom Properties to Application Insights Request Telemetry in WCF
I had to face similar issue as the author described. Tried by implementing ITelemetryInitializer
/ITelemetryProcessor
but did not work.
Ended up writing my own MessageTraceTelemetryModule
class implementing IWcfTelemetryModule
and IWcfMessageTrace
.
In the OnTraceResponse
method, I added my custom property to the request telemetry by extracting value from OperationContext
(which is accessible here!):
internal class MessageTraceTelemetryModule : IWcfTelemetryModule, IWcfMessageTrace
{
public void OnTraceResponse(IOperationContext operation, ref Message response)
{
if (OperationContext.Current.IncomingMessageProperties.TryGetValue("clientID", out object value))
{
operation.Request.Properties.Add("clientID", value.ToString());
}
}
}
New custom property visible in Application Insights telemetry - ClientID custom property Pic.
Note that the clientID
property is being set in the OperationContext
in Message Inspector:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
if(!OperationContext.Current.IncomingMessageProperties.ContainsKey("clientID"))
OperationContext.Current.IncomingMessageProperties.Add("clientID", clientID);
}
Brief Context:
I implemented AAD Token based Authentication in a SOAP based WCF Service.
I needed to store the clientID
from the token (which is validated in message inspector) and add the same as a custom property
in the application insights request telemetry.
References:
- Message Inspectors Documentation
- Application Insights for WCF Documentation
Application Insights - How to add custom metric to your request information?
Using TelemetryInitializers is the right solution. Some comments:
var requestTelemetry = telemetry as RequestTelemetry;
: you do not use requestTelemetry after that. I guess you wanted to check for null.- Adding telemetry initializer in the Active configuration should be fine. You can also consider moving it to the applicationinsights.config
- Custom properties do not show up in the portal immediately. Have you tried to reopen IE after some time and check your request again?
- Can you debug? Do you see that you get in your tememetry initializer? Do you see any AI specific traces in search?
Regarding your second question. Right now telemetry initializers are the only (official) way to get to the autogenerated RequestTelemetry (which is actually in the HttpContext). There are plans to make most of the classes in web public and eventually open source it. But there is no ETA yet. If you create and track request yourself you can add custom properties as you mentioned.
UPDATE: Starting from 2.0.0-beta3 autogenerated request telemetry is accessible though HttpContext extension method: System.Web.HttpContextExtension.GetRequestTelemetry
How to relate custom event properties with the metrics (out of box) provided by Azure App Insights
If you want to slice PageViews by username (set as custom property), you need to set UserName to PageView (instead or in addition to custom event). Just set UserName.
Just replace the simple call to trackPageView in JavaScript snippet to something like:
appInsights.trackPageView(title, url, {UserName: USERNAME})
Azure App Insights - Add Custom Property to Dependency Calls
TelemetryInitializers is the correct way of adding any additional custom property to any telemetry type - Traces/Dependencies/Requests etc.
https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#add-properties-itelemetryinitializer
Also if you are using default config, Dependencies/Traces made within the context of a Request will be automatically correlated together for you with same 'operation_id'.
Related Topics
Date Difference in Years Using C#
Adding Elements to an Xml File in C#
How to Detect If MAChine Is Joined to Domain
How to Get the Colour of a Pixel at X,Y Using C#
How to Run External Program via a C# Program
Convert Time Span Value to Format "Hh:Mm Am/Pm" Using C#
Can You Compile C# So It Doesn't Need the .Net Framework at Runtime
Programmatic Way to Get All the Available Languages (In Satellite Assemblies)
How to Convert String to Integer in C#
Log Off User from Win Xp Programmatically in C#
How to Copy Value from Class X to Class Y with the Same Property Name in C#
C#: How to Create an Attribute on a Method Triggering an Event When It Is Invoked
How to Convert Namevaluecollection to JSON String
Patch Async Requests with Windows.Web.Http.Httpclient Class