Skip to content

Commit

Permalink
Merge pull request #1807 from mirecad/feature/appinisghts-opration-name
Browse files Browse the repository at this point in the history
Use grouped operation name in application insights telemetry
  • Loading branch information
tomasherceg committed Apr 27, 2024
2 parents 712bb09 + 19dbeed commit 7a170a2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
@@ -0,0 +1,32 @@
using DotVVM.Framework.Hosting;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.AspNetCore.Http;

namespace DotVVM.Tracing.ApplicationInsights.AspNetCore;

public class OperationNameTelemetryInitializer : ITelemetryInitializer
{
private readonly IHttpContextAccessor _accessor;

public OperationNameTelemetryInitializer(IHttpContextAccessor accessor)
{
_accessor = accessor;
}

public void Initialize(ITelemetry telemetry)
{
var context = _accessor.HttpContext;
var url = context?.GetDotvvmContext()?.Route?.Url;
if (url != null && telemetry is RequestTelemetry)
{
var method = context.Request.Method;
var operationName = $"{method} /{url}";

var requestTelemetry = telemetry as RequestTelemetry;
requestTelemetry.Name = operationName;
requestTelemetry.Context.Operation.Name = operationName;
}
}
}
Expand Up @@ -30,6 +30,7 @@ public static IDotvvmServiceCollection AddApplicationInsightsTracing(this IDotvv
services.AddDotvvmApplicationInsights();

services.Services.AddApplicationInsightsTelemetryProcessor<RequestTelemetryFilter>();
services.Services.AddSingleton<ITelemetryInitializer, OperationNameTelemetryInitializer>();

services.Services.TryAddSingleton<JavaScriptSnippet>();
services.Services.AddTransient<IConfigureOptions<DotvvmConfiguration>, ApplicationInsightSetup>();
Expand Down
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="Microsoft.Owin.Host.SystemWeb" Version="4.2.2" />
<PackageReference Include="System.Text.Encodings.Web" Version="4.5.1" />
</ItemGroup>
<ItemGroup>
Expand Down
@@ -0,0 +1,25 @@
using System.Web;
using DotVVM.Framework.Hosting;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Channel;

namespace DotVVM.Tracing.ApplicationInsights.Owin;

public class OperationNameTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var context = HttpContext.Current;
var url = context?.GetOwinContext()?.GetDotvvmContext()?.Route?.Url;
if (url != null && telemetry is RequestTelemetry)
{
var method = context.Request.HttpMethod;
var operationName = $"{method} /{url}";

var requestTelemetry = telemetry as RequestTelemetry;
requestTelemetry.Name = operationName;
requestTelemetry.Context.Operation.Name = operationName;
}
}
}
Expand Up @@ -18,6 +18,7 @@ public static class TracingBuilderExtensions
public static IDotvvmServiceCollection AddApplicationInsightsTracing(this IDotvvmServiceCollection services)
{
TelemetryConfiguration.Active.TelemetryProcessorChainBuilder.Use(next => new RequestTelemetryFilter(next)).Build();
TelemetryConfiguration.Active.TelemetryInitializers.Add(new OperationNameTelemetryInitializer());

services.Services.TryAddSingleton<TelemetryClient>();
services.AddDotvvmApplicationInsights();
Expand Down

0 comments on commit 7a170a2

Please sign in to comment.