-
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.
refactoring, add app insights SDK for local dev
- Loading branch information
Showing
2 changed files
with
54 additions
and
36 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
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,50 @@ | ||
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 ServiceCollectionExtensions | ||
{ | ||
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(); | ||
tracing.AddRedisInstrumentation(options => options.SetVerboseDatabaseStatements = true); | ||
}) | ||
.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