I have a C# client application that calls a web application in Azure (also in written in C#, ASP .NET Core). Traffic gets captured in Azure Log Analytics so I can see it in Azure Application Insights.
When calling the web application from my client application, I would like to add some information about the version of the client application. I want this information to be easily viewable in Application Insights.
I notice that the requests table in Application Insights has a column called customDimensions. Maybe I can put stuff there, but I don't know how.
My idea is to add this information as a header and somehow configure the application to copy this information from the header into customDimensions. Is this a good way to go? If so, how do I accomplish the latter half (configure the application to copy this information from the header into customDimensions)?
CodePudding user response:
You could write a telemetry initializer for this, as explained in the docs:
Use telemetry initializers to enrich telemetry with additional information or to override telemetry properties set by the standard telemetry modules.
There is a base class (TelemetryInitializerBase) that you can use which provides access to the http context to get details of the headers:
public class CustomInitializer : TelemetryInitializerBase
{
public CustomInitializer(IHttpContextAccessor contextAccessor) : base(contextAccessor)
{
}
protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{
var appVersion = platformContext.Request.Headers["x-app-version"].ToString();
if(!string.IsNullOrWhiteSpace(appVersion))
requestTelemetry.Properties["appVersion"] = appVersion;
}
}
Add this initializer to the web app running in Azure. Then, in the customDimensions field of the request telemetry you can find the value of appVersion
.
Another option is to set custom propertions in the controller action like this:
var appVersion = Request.Headers["x-app-version"].ToString();
if (!string.IsNullOrWhiteSpace(appVersion))
{
var requestTelemetry = HttpContext.Features.Get<RequestTelemetry>();
requestTelemetry.Properties.Add("appVersion", appVersion);
}