-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(webapi): Add Opentelemetry tracing and metrics to webapi (#1202)
<!--- Provide a general summary of your changes in the Title above --> ## Description Replace Application Insights tracing with Opentelemetry in the webapi <!--- Describe your changes in detail --> ## Related Issue(s) - #1101 ## Verification - [x] **Your** code builds clean without any errors or warnings - [x] Manual testing done (required) - [ ] Relevant automated test added (if you find this hard, leave it and we'll help out)
- Loading branch information
Showing
4 changed files
with
63 additions
and
13 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/Digdir.Domain.Dialogporten.WebApi/Common/Extensions/WebApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Azure.Monitor.OpenTelemetry.AspNetCore; | ||
using OpenTelemetry.Trace; | ||
using Npgsql; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
|
||
namespace Digdir.Domain.Dialogporten.WebApi.Common.Extensions; | ||
|
||
internal static class WebApplicationBuilderExtensions | ||
{ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters