< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 84
Coverable lines: 84
Total lines: 145
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/home/runner/work/dotnet-microservice/dotnet-microservice/Gateway/GlobalConfigurations/OpenTelemetryIntegration.cs

#LineLine coverage
 1using System.Diagnostics.Metrics;
 2using OpenTelemetry;
 3using OpenTelemetry.Metrics;
 4using OpenTelemetry.Resources;
 5using OpenTelemetry.Trace;
 6
 7namespace Gateway;
 8
 9public static partial class OpenTelemetryIntegration
 10{
 11    public static WebApplicationBuilder AddOpenTelemetryIntegration(
 12        this WebApplicationBuilder builder,
 13        ServiceMetadata serviceMetadata
 14    )
 15    {
 016        builder.Services.Configure<OpenTelemetryOptions>(builder.Configuration);
 017        var openTelemetryOptions = new OpenTelemetryOptions();
 018        builder.Configuration.Bind(openTelemetryOptions);
 019        builder
 020            .Services.AddOpenTelemetry()
 021            .AddTracing(serviceMetadata, openTelemetryOptions)
 022            .AddMetrics(serviceMetadata, openTelemetryOptions);
 023        return builder;
 24    }
 25
 26    private static IOpenTelemetryBuilder AddTracing(
 27        this IOpenTelemetryBuilder builder,
 28        ServiceMetadata serviceMetadata,
 29        OpenTelemetryOptions config
 30    )
 31    {
 032        builder.WithTracing(tracing =>
 033        {
 034            tracing
 035                .AddSource()
 036                .SetResourceBuilder(
 037                    ResourceBuilder.CreateDefault().AddService(serviceMetadata.ServiceName)
 038                )
 039                .SetErrorStatusOnException()
 040                .SetSampler(new AlwaysOnSampler())
 041                .AddAspNetCoreInstrumentation(options =>
 042                {
 043                    options.RecordException = true;
 044                })
 045                .AddGrpcClientInstrumentation()
 046                .AddHttpClientInstrumentation(o =>
 047                {
 048                    o.RecordException = true;
 049                });
 050            builder.Services.ConfigureOpenTelemetryTracerProvider(
 051                (tracing) =>
 052                {
 053                    if (config.Enabled)
 054                        tracing.AddOtlpExporter(x =>
 055                        {
 056                            x.ExportProcessorType = ExportProcessorType.Batch;
 057                            x.Headers = config.Headers;
 058                            x.Endpoint = config.Endpoint!;
 059                            x.TimeoutMilliseconds = config.TimeoutMilliseconds;
 060                            x.Protocol = config.Protocol;
 061                        });
 062                }
 063            );
 064        });
 65
 066        return builder;
 67    }
 68
 69    private static IOpenTelemetryBuilder AddMetrics(
 70        this IOpenTelemetryBuilder builder,
 71        ServiceMetadata serviceMetadata,
 72        OpenTelemetryOptions config
 73    )
 74    {
 075        builder.WithMetrics(metrics =>
 076        {
 077            var meter = new Meter(serviceMetadata.ServiceName);
 078
 079            metrics
 080                .AddMeter(meter.Name)
 081                .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(meter.Name))
 082                .AddRuntimeInstrumentation()
 083                .AddHttpClientInstrumentation()
 084                .AddEventCountersInstrumentation()
 085                .AddProcessInstrumentation();
 086
 087            builder.Services.ConfigureOpenTelemetryMeterProvider(
 088                (metrics) =>
 089                {
 090                    if (config.Enabled)
 091                        metrics.AddOtlpExporter(x =>
 092                        {
 093                            x.ExportProcessorType = ExportProcessorType.Batch;
 094                            x.Headers = config.Headers;
 095                            x.Endpoint = config.Endpoint!;
 096                            x.TimeoutMilliseconds = config.TimeoutMilliseconds;
 097                            x.Protocol = config.Protocol;
 098                        });
 099                }
 0100            );
 0101        });
 102
 0103        return builder;
 104    }
 105}

/home/runner/work/dotnet-microservice/dotnet-microservice/Gateway/GlobalConfigurations/OpenTelemetryOptions.cs

#LineLine coverage
 1using OpenTelemetry.Exporter;
 2
 3namespace Gateway;
 4
 5public static partial class OpenTelemetryIntegration
 6{
 7    public class OpenTelemetryOptions
 8    {
 09        public bool Enabled => Endpoint is not null;
 10
 11        [ConfigurationKeyName("OTEL_EXPORTER_OTLP_ENDPOINT")]
 012        public Uri? Endpoint { get; set; }
 13
 14        [ConfigurationKeyName("OTEL_EXPORTER_OTLP_HEADERS")]
 015        public string? Headers { get; set; }
 16
 17        [ConfigurationKeyName("OTEL_EXPORTER_OTLP_TIMEOUT")]
 018        public int TimeoutMilliseconds { get; set; } = 10000;
 19
 20        [ConfigurationKeyName("OTEL_EXPORTER_OTLP_PROTOCOL")]
 021        public OtlpExportProtocol Protocol { get; set; } = OtlpExportProtocol.Grpc;
 22
 023        public IDictionary<string, string> ParsedHeaders => ParseStringToDictionary(Headers);
 24
 25        private static Dictionary<string, string> ParseStringToDictionary(string? input)
 26        {
 027            if (string.IsNullOrWhiteSpace(input))
 028                return [];
 029            var dict = new Dictionary<string, string>();
 30
 031            string[] pairs = input.Split(',');
 032            foreach (string pair in pairs)
 33            {
 034                string[] keyValue = pair.Split('=');
 035                dict.Add(keyValue[0], keyValue[1]);
 36            }
 037            return dict;
 38        }
 39    }
 40}