From bd8a3d750eb63e4e021b7382d0431572ff94382c Mon Sep 17 00:00:00 2001 From: Andra Constantin Date: Tue, 24 Sep 2024 10:18:54 -0400 Subject: [PATCH] separate code to exclude from testing --- Backend/Otel/OtelKernel.cs | 141 ++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 66 deletions(-) diff --git a/Backend/Otel/OtelKernel.cs b/Backend/Otel/OtelKernel.cs index a05ed4a3d9..fe8e539b6a 100644 --- a/Backend/Otel/OtelKernel.cs +++ b/Backend/Otel/OtelKernel.cs @@ -1,6 +1,7 @@ // using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; // using System.Diagnostics.Metrics; // using System.Net.Http; // using System.Net.Http.Json; @@ -10,6 +11,8 @@ // using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using OpenTelemetry; +using OpenTelemetry.Instrumentation.AspNetCore; +using OpenTelemetry.Instrumentation.Http; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; using OpenTelemetry.Trace; @@ -29,6 +32,76 @@ public static class OtelKernel // AddOpenTelemetryInstrumentation(serviceCollection); // } + [ExcludeFromCodeCoverage] + private static void AspNetCoreBuilder(AspNetCoreTraceInstrumentationOptions options) + { + options.RecordException = true; + options.EnrichWithHttpRequest = (activity, request) => + { + var contentLength = request.Headers.ContentLength; + if (contentLength.HasValue) + { + activity.SetTag("inbound.http.request.body.size", contentLength.Value); + } + else + { + activity.SetTag("inbound.http.request.body.size", "no content"); + } + // activity.EnrichWithUser(request.HttpContext); + }; + options.EnrichWithHttpResponse = (activity, response) => + { + var contentLength = response.Headers.ContentLength; + if (contentLength.HasValue) + { + activity.SetTag("inbound.http.response.body.size", contentLength.Value); + } + else + { + activity.SetTag("inbound.http.response.body.size", "no content"); + } + // activity.EnrichWithUser(response.HttpContext); + }; + } + + [ExcludeFromCodeCoverage] + private static void HttpClientBuilder(HttpClientTraceInstrumentationOptions options) + { + options.EnrichWithHttpRequestMessage = (activity, request) => + { + var contentLength = request.Content?.Headers.ContentLength; + if (contentLength.HasValue) + { + activity.SetTag("outbound.http.request.body.size", contentLength.Value); + } + else + { + activity.SetTag("outbound.http.request.body.size", "no content"); + } + + if (request.RequestUri is not null) + { + activity.SetTag("url.pathHERE", request.RequestUri.AbsolutePath); + if (!string.IsNullOrEmpty(request.RequestUri.Query)) + activity.SetTag("url.query", request.RequestUri.Query); + } + else + { + activity.SetTag("outbound.http.response.body.size", "no content"); + } + + // await GetLocation(activity); + }; + options.EnrichWithHttpResponseMessage = (activity, response) => + { + var contentLength = response.Content.Headers.ContentLength; + if (contentLength.HasValue) + { + activity.SetTag("outbound.http.response.body.size", contentLength.Value); + } + }; + } + public static void AddOpenTelemetryInstrumentation(this IServiceCollection services) { var appResourceBuilder = ResourceBuilder.CreateDefault(); @@ -38,72 +111,8 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi .SetResourceBuilder(appResourceBuilder) .AddSource(SourceName) .AddProcessor() - .AddAspNetCoreInstrumentation(options => - { - options.RecordException = true; - options.EnrichWithHttpRequest = (activity, request) => - { - var contentLength = request.Headers.ContentLength; - if (contentLength.HasValue) - { - activity.SetTag("inbound.http.request.body.size", contentLength.Value); - } - else - { - activity.SetTag("inbound.http.request.body.size", "no content"); - } - // activity.EnrichWithUser(request.HttpContext); - }; - options.EnrichWithHttpResponse = (activity, response) => - { - var contentLength = response.Headers.ContentLength; - if (contentLength.HasValue) - { - activity.SetTag("inbound.http.response.body.size", contentLength.Value); - } - else - { - activity.SetTag("inbound.http.response.body.size", "no content"); - } - // activity.EnrichWithUser(response.HttpContext); - }; - }) - .AddHttpClientInstrumentation(options => - { - options.EnrichWithHttpRequestMessage = (activity, request) => - { - var contentLength = request.Content?.Headers.ContentLength; - if (contentLength.HasValue) - { - activity.SetTag("outbound.http.request.body.size", contentLength.Value); - } - else - { - activity.SetTag("outbound.http.request.body.size", "no content"); - } - - if (request.RequestUri is not null) - { - activity.SetTag("url.pathHERE", request.RequestUri.AbsolutePath); - if (!string.IsNullOrEmpty(request.RequestUri.Query)) - activity.SetTag("url.query", request.RequestUri.Query); - } - else - { - activity.SetTag("outbound.http.response.body.size", "no content"); - } - - // await GetLocation(activity); - }; - options.EnrichWithHttpResponseMessage = (activity, response) => - { - var contentLength = response.Content.Headers.ContentLength; - if (contentLength.HasValue) - { - activity.SetTag("outbound.http.response.body.size", contentLength.Value); - } - }; - }) + .AddAspNetCoreInstrumentation(AspNetCoreBuilder) + .AddHttpClientInstrumentation(HttpClientBuilder) // .AddSource("MongoDB.Driver.Core.Extensions.DiagnosticSources") .AddConsoleExporter() .AddOtlpExporter()