Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arealmaas committed Dec 9, 2024
1 parent a08580f commit d0cd2d7
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using System.Diagnostics;
using Azure.Monitor.OpenTelemetry.Exporter;

namespace Digdir.Library.Utils.AspNet;

public static class AspNetUtilitiesExtensions
{
private const string MassTransitSource = "MassTransit";
private const string AzureSource = "Azure.*";

public static IServiceCollection AddAspNetHealthChecks(this IServiceCollection services, Action<AspNetUtilitiesSettings, IServiceProvider>? configure = null)
{
var optionsBuilder = services.AddOptions<AspNetUtilitiesSettings>();
Expand Down Expand Up @@ -94,49 +92,55 @@ public static WebApplicationBuilder ConfigureTelemetry(
};

telemetryBuilder.UseOtlpExporter(otlpProtocol, new Uri(settings.Endpoint));

telemetryBuilder
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
tracing.SetSampler(new AlwaysOnSampler());
}

foreach (var source in settings.TraceSources)
{
tracing.AddSource(source);
}

tracing
.AddAspNetCoreInstrumentation(opts =>
{
opts.RecordException = true;
opts.Filter = httpContext => !httpContext.Request.Path.StartsWithSegments("/health");
})
.AddHttpClientInstrumentation(o => o.FilterHttpRequestMessage = (_) =>
{
o.RecordException = true;
var parentActivity = Activity.Current?.Parent;
if (parentActivity != null && parentActivity.Source.Name.Equals("Azure.Core.Http", StringComparison.Ordinal))
{
return false;
}
return true;
})
.AddNpgsql()
.AddFusionCacheInstrumentation();
});
}
else
{
Console.WriteLine("[OpenTelemetry] OTLP exporter not configured - skipping");
}

telemetryBuilder
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
tracing.SetSampler(new AlwaysOnSampler());
}

foreach (var source in settings.TraceSources)
{
tracing.AddSource(source);
}

tracing.AddAspNetCoreInstrumentation(opts =>
{
opts.RecordException = true;
opts.Filter = httpContext => !httpContext.Request.Path.StartsWithSegments("/health");
})
.AddHttpClientInstrumentation(o => o.FilterHttpRequestMessage = (_) =>
{
o.RecordException = true;
var parentActivity = Activity.Current?.Parent;
if (parentActivity != null && parentActivity.Source.Name.Equals("Azure.Core.Http", StringComparison.Ordinal))
{
return false;
}
return true;
})
.AddNpgsql()
.AddFusionCacheInstrumentation();
})
.WithMetrics(metrics =>
if (!builder.Environment.IsDevelopment())
{
telemetryBuilder.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation()
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation();
.AddHttpClientInstrumentation()
.AddAzureMonitorMetricExporter();
});
}

return builder;
}
Expand Down

0 comments on commit d0cd2d7

Please sign in to comment.