Skip to content

Commit

Permalink
separate code to exclude from testing
Browse files Browse the repository at this point in the history
  • Loading branch information
andracc committed Sep 24, 2024
1 parent f26bc45 commit bd8a3d7
Showing 1 changed file with 75 additions and 66 deletions.
141 changes: 75 additions & 66 deletions Backend/Otel/OtelKernel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -38,72 +111,8 @@ public static void AddOpenTelemetryInstrumentation(this IServiceCollection servi
.SetResourceBuilder(appResourceBuilder)
.AddSource(SourceName)
.AddProcessor<LocationEnricher>()
.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()
Expand Down

0 comments on commit bd8a3d7

Please sign in to comment.