Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
KrothuTheCoder committed Mar 14, 2024
2 parents 5f20696 + 5c75df1 commit 5fe7952
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
19 changes: 17 additions & 2 deletions DoSomethingService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@
using Swashbuckle.AspNetCore.SwaggerUI;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Reflection;
using Microsoft.ApplicationInsights.Extensibility;


var myAllowSpecificOrigins = "_myAllowSpecificOrigins";
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddSingleton<ITelemetryInitializer, UpstreamProxyTraceHeaderTelemetryInitializer>((serviceProvider)=> {
var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
// Az App Gateway and Front door trace/correlation headers are defaulted
return new UpstreamProxyTraceHeaderTelemetryInitializer(httpContextAccessor);
});

builder.Services.AddHttpContextAccessor();

var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions();
aiOptions.EnableAdaptiveSampling = false;
aiOptions.EnableQuickPulseMetricStream = true;
aiOptions.EnableRequestTrackingTelemetryModule = true;
aiOptions.EnableDependencyTrackingTelemetryModule = true;

builder.Services.AddApplicationInsightsTelemetry(aiOptions);

var apiVersions = new Dictionary<string, string>();

Expand Down Expand Up @@ -103,4 +118,4 @@ public void Apply(ControllerModel controller)
var apiVersion = controllerNamespace.Split('.').Last().ToLower();

Check warning on line 118 in DoSomethingService/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 118 in DoSomethingService/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
controller.ApiExplorer.GroupName=apiVersion;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;

public class UpstreamProxyTraceHeaderTelemetryInitializer : TelemetryInitializerBase
{
public ICollection<string> HeaderNames { get; }

public UpstreamProxyTraceHeaderTelemetryInitializer(IHttpContextAccessor httpContextAccessor, ICollection<string>? headerNames = default(ICollection<string>))
: base(httpContextAccessor)
{
if ( headerNames == null){
headerNames =new string[] { "x-azure-ref", "x-appgw-trace-id"};
}
HeaderNames = headerNames;
}

protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
{

requestTelemetry.Context.Cloud.RoleName = "DoSomething Service";

if ( null == this.HeaderNames || this.HeaderNames.Count==0) {
return;
}
if (telemetry == null) {
throw new ArgumentNullException(nameof(telemetry));
}
if (requestTelemetry == null) {
throw new ArgumentNullException(nameof(requestTelemetry));
}
if ( telemetry== requestTelemetry ) { // only do this for the request telemetry
if (platformContext == null) {
throw new ArgumentNullException(nameof(platformContext));
}
if (platformContext.Request?.Headers != null && platformContext.Request?.Headers.Count> 0) {
foreach (var name in this.HeaderNames) {
var value = GetHeaderValue(name,platformContext.Request.Headers);

if ( !String.IsNullOrEmpty(value)) {
AddReference(requestTelemetry,name, value);
}
}
}
}
}

private string GetHeaderValue(string headerNameToSearch, IHeaderDictionary requestHeaders){
if ( null == requestHeaders || requestHeaders.Count== 0){
return string.Empty;
}
if (requestHeaders.ContainsKey(headerNameToSearch)) {
return requestHeaders[headerNameToSearch];

Check warning on line 53 in DoSomethingService/Telemetry/UpstreamProxyTraceHeaderTelemetryInitializer.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 53 in DoSomethingService/Telemetry/UpstreamProxyTraceHeaderTelemetryInitializer.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}
return string.Empty;
}
private void AddReference(RequestTelemetry requestTelemetry, string headerName, string headerValue){
if ( null == requestTelemetry ){
return;
}
requestTelemetry.Properties.Add(headerName, headerValue);
}

}

0 comments on commit 5fe7952

Please sign in to comment.