Skip to content

Commit

Permalink
chore(web-api): separate the telemetry-configuration into aspnet package
Browse files Browse the repository at this point in the history
  • Loading branch information
arealmaas committed Oct 24, 2024
1 parent d8ca865 commit 2cb69a1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 56 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.13.0" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
<PackageReference Include="FastEndpoints.Swagger" Version="5.29.0"/>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" />
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="8.0.0" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.5" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageReference Include="OpenTelemetry" Version="1.9.0" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0"/>
</ItemGroup>

Expand Down
49 changes: 47 additions & 2 deletions src/Digdir.Library.Utils.AspNet/AspNetUtilitiesExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Digdir.Library.Utils.AspNet.HealthChecks;
using HealthChecks.UI.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Npgsql;
using OpenTelemetry.Trace;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;

namespace Digdir.Library.Utils.AspNet;

Expand Down Expand Up @@ -40,4 +46,43 @@ private static WebApplication MapHealthCheckEndpoint(this WebApplication app, st
app.MapHealthChecks(path, new HealthCheckOptions { Predicate = predicate, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse });
return app;
}

public static WebApplicationBuilder ConfigureTelemetry(this WebApplicationBuilder builder)
{
builder.Services.AddOpenTelemetry()
.ConfigureResource(resource => resource
.AddService(serviceName: builder.Environment.ApplicationName))
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
tracing.SetSampler(new AlwaysOnSampler());
}

tracing.AddAspNetCoreInstrumentation(options =>
{
options.Filter = (httpContext) =>
!httpContext.Request.Path.StartsWithSegments("/health");
});

tracing.AddHttpClientInstrumentation();
tracing.AddNpgsql();
})
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation();
});

if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")))
{
builder.Services.AddOpenTelemetry().UseAzureMonitor();
}
else
{
// Use Application Insights SDK for local development
builder.Services.AddApplicationInsightsTelemetry();
}

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.13.0" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="8.0.0" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="8.0.5" />
<PackageReference Include="OpenTelemetry" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
</ItemGroup>

</Project>

0 comments on commit 2cb69a1

Please sign in to comment.