< Summary

Information
Class: Api.ServiceExtension.OpenTelemetryCollectionExtensions
Assembly: Api
File(s): /home/runner/work/Northwind-Api/Northwind-Api/src/Api/ServiceExtension/OpenTelemetry.cs
Line coverage
100%
Covered lines: 48
Uncovered lines: 0
Coverable lines: 48
Total lines: 72
Line coverage: 100%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddOpenTelemetryWithExporters(...)83.33%66100%
AddJaeger(...)100%11100%
AddZipkin(...)100%11100%

File(s)

/home/runner/work/Northwind-Api/Northwind-Api/src/Api/ServiceExtension/OpenTelemetry.cs

#LineLine coverage
 1using OpenTelemetry.Logs;
 2using OpenTelemetry.Metrics;
 3using OpenTelemetry.Resources;
 4using OpenTelemetry.Trace;
 5
 6namespace Api.ServiceExtension;
 7
 8[Flags]
 9public enum TelemetryExporters {
 10  None = 0,
 11  Jaeger = 1,
 12  Zipkin = 2
 13}
 14
 15public static class OpenTelemetryCollectionExtensions {
 16  public static IServiceCollection AddOpenTelemetryWithExporters(
 17       this IServiceCollection services,
 18       IConfiguration configuration,
 119       TelemetryExporters exporters) {
 120    var serviceName = configuration.GetValue<string>("OpenTelemetry:ServiceName") ?? "DefaultApi";
 21
 122    services.AddOpenTelemetry()
 123        .WithTracing(tracer => {
 124          tracer
 125                  .SetSampler<AlwaysOnSampler>()
 126                  .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName))
 127                  .AddAspNetCoreInstrumentation()
 128                  .AddHttpClientInstrumentation();
 129
 130          if (exporters.HasFlag(TelemetryExporters.Jaeger))
 131            tracer.AddJaeger(configuration);
 132          if (exporters.HasFlag(TelemetryExporters.Zipkin))
 133            tracer.AddZipkin(configuration);
 134        })
 135        .WithMetrics(metrics => {
 136          metrics
 137                  .AddAspNetCoreInstrumentation()
 138                  .AddHttpClientInstrumentation()
 139                  .AddRuntimeInstrumentation()
 140                  .AddMeter(
 141                      "Microsoft.AspNetCore.Hosting",
 142                      "Microsoft.AspNetCore.Server.Kestrel",
 143                      "Microsoft.AspNetCore.Http.Connections",
 144                      "System.Net.Http",
 145                      serviceName,
 146                      "Microsoft.AspNetCore.Routing",
 147                      "Microsoft.AspNetCore.Diagnostics",
 148                      "Microsoft.AspNetCore.RateLimiting");
 249        });
 50
 151    services.Configure<OpenTelemetryLoggerOptions>(logging =>
 152        logging.AddOtlpExporter());
 53
 154    return services;
 155  }
 56
 157  public static TracerProviderBuilder AddJaeger(this TracerProviderBuilder builder, IConfiguration configuration) {
 158    var port = configuration.GetValue("Ports:JAEGER_PORT", "4317");
 259    builder.AddOtlpExporter(opt => {
 160      opt.Endpoint = new Uri($"http://jaeger:{port}");
 261    });
 162    return builder;
 163  }
 64
 165  public static TracerProviderBuilder AddZipkin(this TracerProviderBuilder builder, IConfiguration configuration) {
 166    var port = configuration.GetValue("Ports:ZIPKIN_PORT", "9411");
 267    builder.AddZipkinExporter(opt => {
 168      opt.Endpoint = new Uri($"http://zipkin:{port}/api/v2/spans");
 269    });
 170    return builder;
 171  }
 72}