From cb78f6a2ecdf53581400b1581d30d69f6d928167 Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Fri, 5 Jan 2024 09:16:16 -0700 Subject: [PATCH] added quieter opentelemetry tracer --- Management.Web/ConfigurationSetup.cs | 14 ++++++ Management.Web/CustomConsoleExporter.cs | 20 +++++++++ Management.Web/Program.cs | 59 +++++++++++-------------- build.sh | 2 +- docker-compose.yml | 10 ----- ops/local-dev/docker-compose.yml | 23 ++++++++++ 6 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 Management.Web/ConfigurationSetup.cs create mode 100644 Management.Web/CustomConsoleExporter.cs create mode 100644 ops/local-dev/docker-compose.yml diff --git a/Management.Web/ConfigurationSetup.cs b/Management.Web/ConfigurationSetup.cs new file mode 100644 index 0000000..33a9c28 --- /dev/null +++ b/Management.Web/ConfigurationSetup.cs @@ -0,0 +1,14 @@ +public static class ConfigurationSetup +{ + public static void Canvas(WebApplicationBuilder builder) + { + var canvas_token = builder.Configuration["CANVAS_TOKEN"] ?? throw new Exception("CANVAS_TOKEN is null"); + + var canvas_url = builder.Configuration["CANVAS_URL"]; + if (canvas_url == null) + { + Console.WriteLine("CANVAS_URL is null, defaulting to https://snow.instructure.com"); + builder.Configuration["CANVAS_URL"] = "https://snow.instructure.com"; + } + } +} \ No newline at end of file diff --git a/Management.Web/CustomConsoleExporter.cs b/Management.Web/CustomConsoleExporter.cs new file mode 100644 index 0000000..2f232b5 --- /dev/null +++ b/Management.Web/CustomConsoleExporter.cs @@ -0,0 +1,20 @@ +using System.Diagnostics; +using OpenTelemetry; + +public class CustomConsoleExporter : BaseExporter +{ + public override ExportResult Export(in Batch batch) + { + using var scope = SuppressInstrumentationScope.Begin(); + + foreach (var activity in batch) + { + string[] ignoreOperations = [ + "Microsoft.AspNetCore.Hosting.HttpRequestIn", + ]; + if (!ignoreOperations.Contains(activity.OperationName)) + Console.WriteLine($"{activity.OperationName}: {activity.DisplayName}"); + } + return ExportResult.Success; + } +} \ No newline at end of file diff --git a/Management.Web/Program.cs b/Management.Web/Program.cs index 0760424..aa7a823 100644 --- a/Management.Web/Program.cs +++ b/Management.Web/Program.cs @@ -18,37 +18,30 @@ using OpenTelemetry.Logs; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; +using OpenTelemetry; DotEnv.Load(); var builder = WebApplication.CreateBuilder(args); -var canvas_token = builder.Configuration["CANVAS_TOKEN"]; -if (canvas_token == null) - throw new Exception("CANVAS_TOKEN is null"); -var canvas_url = builder.Configuration["CANVAS_URL"]; -if (canvas_url == null) -{ - Console.WriteLine("CANVAS_URL is null, defaulting to https://snow.instructure.com"); - builder.Configuration["CANVAS_URL"] = "https://snow.instructure.com"; -} +ConfigurationSetup.Canvas(builder); const string serviceName = "canvas-management"; -// builder.Logging.AddOpenTelemetry(options => -// { -// options -// .SetResourceBuilder( -// ResourceBuilder -// .CreateDefault() -// .AddService(serviceName) -// ) -// .AddOtlpExporter(o => -// { -// o.Endpoint = new Uri("http://localhost:4317/"); -// }) -// .AddConsoleExporter(); -// }); +builder.Logging.AddOpenTelemetry(options => +{ + options + .SetResourceBuilder( + ResourceBuilder + .CreateDefault() + .AddService(serviceName) + ) + .AddOtlpExporter(o => + { + o.Endpoint = new Uri("http://localhost:4317/"); + }); + // .AddConsoleExporter(); +}); builder.Services.AddOpenTelemetry() .ConfigureResource(resource => resource.AddService(serviceName)) @@ -59,15 +52,16 @@ builder.Services.AddOpenTelemetry() o.Endpoint = new Uri("http://localhost:4317/"); }) .AddAspNetCoreInstrumentation() - .AddConsoleExporter() - ); -// .WithMetrics(metrics => metrics -// .AddOtlpExporter(o => { -// o.Endpoint = new Uri("http://localhost:4317/"); -// }) -// .AddAspNetCoreInstrumentation() -// .AddConsoleExporter() -// ); + .AddProcessor(new BatchActivityExportProcessor(new CustomConsoleExporter())) + ) + .WithMetrics(metrics => metrics + .AddOtlpExporter(o => + { + o.Endpoint = new Uri("http://localhost:4317/"); + } + ) + .AddAspNetCoreInstrumentation() +); // Add services to the container. builder.Services.AddRazorPages(); @@ -133,3 +127,4 @@ foreach (var address in addresses) } app.WaitForShutdown(); + diff --git a/build.sh b/build.sh index 9e527e2..4ba4225 100755 --- a/build.sh +++ b/build.sh @@ -2,7 +2,7 @@ MAJOR_VERSION="1" -MINOR_VERSION="3" +MINOR_VERSION="4" VERSION="$MAJOR_VERSION.$MINOR_VERSION" dotnet publish Management.Web/ \ diff --git a/docker-compose.yml b/docker-compose.yml index f4a9770..c4b9c3c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,22 +31,12 @@ services: - 4318:4318 # OTLP http receiver - 55679:55679 # zpages extension - # The zipkin process services the UI, and also exposes a POST endpoint that - # instrumentation can send trace data to. zipkin: image: ghcr.io/openzipkin/zipkin-slim container_name: zipkin # Environment settings are defined here https://github.com/openzipkin/zipkin/blob/master/zipkin-server/README.md#environment-variables environment: - STORAGE_TYPE=mem - # Point the zipkin at the storage backend - # - MYSQL_HOST=mysql - # Uncomment to enable self-tracing - # - SELF_TRACING_ENABLED=true - # Uncomment to increase heap size - # - JAVA_OPTS=-Xms128m -Xmx128m -XX:+ExitOnOutOfMemoryError ports: - # Port used for the Zipkin UI and HTTP Api - 9411:9411 - # Uncomment to enable debug logging # command: --logging.level.zipkin2=DEBUG diff --git a/ops/local-dev/docker-compose.yml b/ops/local-dev/docker-compose.yml new file mode 100644 index 0000000..2a4c5f4 --- /dev/null +++ b/ops/local-dev/docker-compose.yml @@ -0,0 +1,23 @@ +services: + collector: + image: otel/opentelemetry-collector-contrib + volumes: + - ../otel-collector-config.yml:/etc/otelcol-contrib/config.yaml + ports: + - 1888:1888 # pprof extension + - 8888:8888 # Prometheus metrics exposed by the Collector + - 8889:8889 # Prometheus exporter metrics + - 13133:13133 # health_check extension + - 4317:4317 # OTLP gRPC receiver + - 4318:4318 # OTLP http receiver + - 55679:55679 # zpages extension + + zipkin: + image: ghcr.io/openzipkin/zipkin-slim + container_name: zipkin + # Environment settings are defined here https://github.com/openzipkin/zipkin/blob/master/zipkin-server/README.md#environment-variables + environment: + - STORAGE_TYPE=mem + ports: + - 9411:9411 + # command: --logging.level.zipkin2=DEBUG