From 8d4d7c7ce32a8fe31a2f7a5e9f70063d414c4b78 Mon Sep 17 00:00:00 2001 From: Spencer Twaddle <7374698+stwaddle@users.noreply.github.com> Date: Mon, 4 May 2026 17:34:45 -0500 Subject: [PATCH] Fix OTel wiring to match working Auth implementation Three corrections vs Auth project: - Replace AddOtlpExporter() (traces only) with UseOtlpExporter() so both traces and logs are exported via OTLP - Remove redundant .WithLogging() call; builder.Logging.AddOpenTelemetry() is sufficient on its own - Pass OTEL_EXPORTER_OTLP_ENDPOINT/PROTOCOL through from host env vars instead of hardcoding the collector URL Co-Authored-By: Claude Sonnet 4.6 --- docker-compose.yml | 4 ++-- src/Budget.Api/Program.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 4745ad9..ae22497 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,8 +7,8 @@ services: - VIRTUAL_PORT=8080 - ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT:-Production} - ConnectionStrings__DefaultConnection=Host=apps-db;Port=5432;Database=${POSTGRES_DB:-budget};Username=${POSTGRES_USER:-budget};Password=${POSTGRES_PASSWORD} - - OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4317 - - OTEL_EXPORTER_OTLP_PROTOCOL=grpc + - OTEL_EXPORTER_OTLP_ENDPOINT=${OTEL_EXPORTER_OTLP_ENDPOINT} + - OTEL_EXPORTER_OTLP_PROTOCOL=${OTEL_EXPORTER_OTLP_PROTOCOL} - OTEL_SERVICE_NAME=budget depends_on: - apps-db diff --git a/src/Budget.Api/Program.cs b/src/Budget.Api/Program.cs index 43c9f50..f7cc3f0 100644 --- a/src/Budget.Api/Program.cs +++ b/src/Budget.Api/Program.cs @@ -10,6 +10,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; +using OpenTelemetry; using OpenTelemetry.Resources; using OpenTelemetry.Trace; @@ -24,12 +25,11 @@ builder.Logging.AddOpenTelemetry(logging => builder.Services.AddOpenTelemetry() .ConfigureResource(resource => resource .AddService(serviceName: "budget", serviceVersion: "1.0.0")) - .WithLogging() .WithTracing(tracing => tracing .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() - .AddEntityFrameworkCoreInstrumentation() - .AddOtlpExporter()); + .AddEntityFrameworkCoreInstrumentation()) + .UseOtlpExporter(); var connStr = builder.Configuration.GetConnectionString("DefaultConnection") ?? $"Host={builder.Configuration["POSTGRES_HOST"] ?? "localhost"};" +