Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arealmaas committed Jan 6, 2025
1 parent 2a60b0d commit 7c00782
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 223 deletions.
50 changes: 23 additions & 27 deletions local-otel-configuration/dashboards/runtime-metrics.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,19 @@
"type": "timeseries",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "Prometheus"
},
"expr": "dialogporten_process_runtime_dotnet_gc_heap_size_bytes",
"legendFormat": "Heap Size",
"refId": "A"
},
{
"expr": "dialogporten_process_runtime_dotnet_gc_committed_memory_size_bytes",
"legendFormat": "Committed Memory",
"refId": "B"
},
{
"expr": "dialogporten_dotnet_process_memory_working_set_bytes",
"legendFormat": "Working Set",
"refId": "C"
}
]
},
Expand Down Expand Up @@ -171,13 +177,14 @@
"type": "timeseries",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "Prometheus"
},
"expr": "rate(dialogporten_process_runtime_dotnet_gc_collections_count_total[5m])",
"legendFormat": "Gen {{generation}}",
"legendFormat": "Collections/sec",
"refId": "A"
},
{
"expr": "rate(dialogporten_process_runtime_dotnet_gc_duration_nanoseconds_total[5m])",
"legendFormat": "GC Duration/sec",
"refId": "B"
}
]
},
Expand Down Expand Up @@ -257,22 +264,19 @@
"type": "timeseries",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "Prometheus"
},
"expr": "dialogporten_process_runtime_dotnet_thread_pool_queue_length",
"legendFormat": "Queue Length",
"refId": "A"
},
{
"datasource": {
"type": "prometheus",
"uid": "Prometheus"
},
"expr": "dialogporten_process_runtime_dotnet_thread_pool_threads_count",
"legendFormat": "Thread Count",
"refId": "B"
},
{
"expr": "rate(dialogporten_process_runtime_dotnet_thread_pool_completed_items_count_total[5m])",
"legendFormat": "Completed Items/sec",
"refId": "C"
}
]
},
Expand Down Expand Up @@ -352,20 +356,12 @@
"type": "timeseries",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "Prometheus"
},
"expr": "rate(dialogporten_process_runtime_dotnet_exceptions_count_total[$__rate_interval])",
"expr": "rate(dialogporten_process_runtime_dotnet_exceptions_count_total[5m])",
"legendFormat": "Exceptions/sec",
"refId": "A"
},
{
"datasource": {
"type": "prometheus",
"uid": "Prometheus"
},
"expr": "rate(dialogporten_process_runtime_dotnet_monitor_lock_contention_count_total[$__rate_interval])",
"expr": "rate(dialogporten_process_runtime_dotnet_monitor_lock_contention_count_total[5m])",
"legendFormat": "Lock Contentions/sec",
"refId": "B"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using Azure.Monitor.OpenTelemetry.Exporter;
using Npgsql;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using System.Diagnostics;

namespace Digdir.Domain.Dialogporten.WebApi.Common.Extensions;

internal static class OpenTelemetryExtensions
{
public static IServiceCollection AddDialogportenTelemetry(
this IServiceCollection services,
IConfiguration configuration,
IHostEnvironment environment)
{
if (string.IsNullOrEmpty(configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]))
return services;

var otlpProtocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"]?.ToLowerInvariant() switch
{
"grpc" => OtlpExportProtocol.Grpc,
"http/protobuf" => OtlpExportProtocol.HttpProtobuf,
"http" => OtlpExportProtocol.HttpProtobuf,
_ => throw new ArgumentException($"Unsupported protocol: {configuration["OTEL_EXPORTER_OTLP_PROTOCOL"]}")
};

var endpoint = new Uri(configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]!);

return services.AddOpenTelemetry()
.ConfigureResource(resource =>
{
resource.AddService(
serviceName: configuration["OTEL_SERVICE_NAME"] ?? environment.ApplicationName);
})
.WithTracing(tracing =>
{
if (environment.IsDevelopment())
{
tracing.SetSampler(new AlwaysOnSampler());
}

tracing
.AddAspNetCoreInstrumentation(opts =>
{
opts.RecordException = true;
opts.Filter = httpContext => !httpContext.Request.Path.StartsWithSegments("/health");
})
.AddHttpClientInstrumentation(o =>
{
o.RecordException = true;
o.FilterHttpRequestMessage = _ =>
{
var parentActivity = Activity.Current?.Parent;
if (parentActivity != null && parentActivity.Source.Name.Equals("Azure.Core.Http", StringComparison.Ordinal))
{
return false;
}
return true;
};
})
.AddEntityFrameworkCoreInstrumentation()
.AddNpgsql()
.AddFusionCacheInstrumentation()
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(endpoint, "/v1/traces");
options.Protocol = otlpProtocol;
});
})
.WithMetrics(metrics =>
{
metrics.AddRuntimeInstrumentation()
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation();

var appInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
if (!string.IsNullOrEmpty(appInsightsConnectionString))
{
metrics.AddAzureMonitorMetricExporter(options =>
{
options.ConnectionString = appInsightsConnectionString;
});
}
else
{
metrics.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(endpoint, "/v1/metrics");
options.Protocol = otlpProtocol;
});
}
})
.Services;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.13.1" />
<PackageReference Include="FastEndpoints.Swagger" Version="5.32.0"/>
<PackageReference Include="FastEndpoints.Swagger" Version="5.32.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.3.0" />
<PackageReference Include="Serilog.Sinks.OpenTelemetry" Version="4.1.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.0" />
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.2.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.EntityFrameworkCore" Version="1.10.0-beta.1" />
<PackageReference Include="OpenTelemetry" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.10.0" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="9.0.2" />
<PackageReference Include="ZiggyCreatures.FusionCache.OpenTelemetry" Version="1.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down
66 changes: 49 additions & 17 deletions src/Digdir.Domain.Dialogporten.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Text.Json.Serialization;
Expand All @@ -7,26 +8,32 @@
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;
using Azure.Monitor.OpenTelemetry.Exporter;
using FastEndpoints;
using FastEndpoints.Swagger;
using FluentValidation;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using Npgsql;
using NSwag;
using OpenTelemetry;
using OpenTelemetry.Exporter;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using Serilog;
using Microsoft.Extensions.Options;
using Digdir.Domain.Dialogporten.WebApi.Common.Middleware;

var builder = WebApplication.CreateBuilder(args);
using Serilog.Sinks.OpenTelemetry;

// Using two-stage initialization to catch startup errors.
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
Expand All @@ -39,21 +46,54 @@

try
{
BuildAndRun(builder);
BuildAndRun(args);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
Log.Fatal(ex, "Application terminated unexpectedly");
throw;
}

static void BuildAndRun(WebApplicationBuilder builder)
static void BuildAndRun(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(kestrelOptions =>
{
kestrelOptions.Limits.MaxRequestBodySize = Constants.MaxRequestBodySize;
});

builder.Host.UseSerilog((context, services, configuration) =>
{
var loggerConfig = configuration
.MinimumLevel.Warning()
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.WithEnvironmentName()
.Enrich.FromLogContext();

var otlpEndpoint = context.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
if (!string.IsNullOrEmpty(otlpEndpoint))
{
var protocol = context.Configuration["OTEL_EXPORTER_OTLP_PROTOCOL"] switch
{
"grpc" => OtlpProtocol.Grpc,
"http/protobuf" => OtlpProtocol.HttpProtobuf,
_ => throw new InvalidOperationException($"Invalid OTLP protocol: {context.Configuration["OTEL_EXPORTER_OTLP_PROTOCOL"]}")
};

loggerConfig.WriteTo.OpenTelemetry(options =>
{
options.Endpoint = otlpEndpoint;
options.Protocol = protocol;
});
}
else
{
loggerConfig.WriteTo.Console(formatProvider: CultureInfo.InvariantCulture);
}
});

builder.Configuration
.AddAzureConfiguration(builder.Environment.EnvironmentName)
.AddLocalConfiguration(builder.Environment);
Expand All @@ -66,16 +106,8 @@ static void BuildAndRun(WebApplicationBuilder builder)

var thisAssembly = Assembly.GetExecutingAssembly();

builder.ConfigureTelemetry((settings, configuration) =>
{
settings.ServiceName = configuration["OTEL_SERVICE_NAME"] ?? builder.Environment.ApplicationName;
settings.Endpoint = configuration["OTEL_EXPORTER_OTLP_ENDPOINT"];
settings.Protocol = configuration["OTEL_EXPORTER_OTLP_PROTOCOL"];
settings.AppInsightsConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];
settings.ResourceAttributes = configuration["OTEL_RESOURCE_ATTRIBUTES"];
});

builder.Services
.AddDialogportenTelemetry(builder.Configuration, builder.Environment)
// Options setup
.ConfigureOptions<AuthorizationOptionsSetup>()

Expand Down Expand Up @@ -143,7 +175,7 @@ static void BuildAndRun(WebApplicationBuilder builder)
var app = builder.Build();

app.UseHttpsRedirection()
.UseRequestLogging()
.UseSerilogRequestLogging()
.UseDefaultExceptionHandler()
.UseJwtSchemeSelector()
.UseAuthentication()
Expand Down
Loading

0 comments on commit 7c00782

Please sign in to comment.