Skip to content

Commit

Permalink
refactoring, add app insights SDK for local dev
Browse files Browse the repository at this point in the history
  • Loading branch information
knuhau committed Oct 18, 2024
1 parent 465d25c commit e10b1e3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 36 deletions.
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;
}
}
40 changes: 4 additions & 36 deletions src/Digdir.Domain.Dialogporten.WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
using System.Reflection;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Azure.Monitor.OpenTelemetry.AspNetCore;
using Digdir.Domain.Dialogporten.Application;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.OptionExtensions;
using Digdir.Domain.Dialogporten.Application.Externals.Presentation;
using Digdir.Domain.Dialogporten.WebApi.Common.Extensions;
using Digdir.Domain.Dialogporten.Infrastructure;
using Digdir.Domain.Dialogporten.WebApi;
using Digdir.Domain.Dialogporten.WebApi.Common;
using Digdir.Domain.Dialogporten.WebApi.Common.Authentication;
using Digdir.Domain.Dialogporten.WebApi.Common.Authorization;
using Digdir.Domain.Dialogporten.WebApi.Common.Extensions;
using Digdir.Domain.Dialogporten.WebApi.Common.Json;
using Digdir.Domain.Dialogporten.WebApi.Common.Swagger;
using Digdir.Library.Utils.AspNet;
Expand All @@ -22,11 +21,7 @@
using FluentValidation;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Authorization;
using Npgsql;
using NSwag;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OpenTelemetry.Metrics;
using Serilog;
using Microsoft.Extensions.Options;

Expand Down Expand Up @@ -76,6 +71,8 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura

var thisAssembly = Assembly.GetExecutingAssembly();

builder.ConfigureTelemetry();

builder.Services
// Options setup
.ConfigureOptions<AuthorizationOptionsSetup>()
Expand Down Expand Up @@ -128,38 +125,9 @@ static void BuildAndRun(string[] args, TelemetryConfiguration telemetryConfigura
.ToList() ?? [])
// Auth
.AddDialogportenAuthentication(builder.Configuration)
.AddAuthorization()
.AddAuthorization();

// OpenTelemetry
.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();
}

if (builder.Environment.IsDevelopment())
{
Expand Down

0 comments on commit e10b1e3

Please sign in to comment.