|
| 1 | +using System; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using OpenTelemetry.Context.Propagation; |
| 5 | +using OpenTelemetry.Trace; |
| 6 | +using OpenTelemetry.Trace.Samplers; |
| 7 | + |
| 8 | +namespace OpenCodeFoundation.OpenTelemetry |
| 9 | +{ |
| 10 | + public static class Extensions |
| 11 | + { |
| 12 | + private const string SectionName = "OpenTelemetry"; |
| 13 | + |
| 14 | + public static IServiceCollection AddOpenTelemetryIntegration( |
| 15 | + this IServiceCollection services, |
| 16 | + Action<OpenTelemetryOptions> options = null, |
| 17 | + string sectionName = SectionName) |
| 18 | + { |
| 19 | + var openTelemetryOptions = services.GetOptions(sectionName, options); |
| 20 | + |
| 21 | + if (openTelemetryOptions.Enabled) |
| 22 | + { |
| 23 | + ConfigureOpenTelemetry(services, openTelemetryOptions); |
| 24 | + } |
| 25 | + |
| 26 | + return services; |
| 27 | + } |
| 28 | + |
| 29 | + public static T GetOptions<T>( |
| 30 | + this IServiceCollection services, |
| 31 | + string sectionName, |
| 32 | + Action<T> configure = null) |
| 33 | + where T : IConfigurationOptions, new() |
| 34 | + { |
| 35 | + var provider = services.BuildServiceProvider(); |
| 36 | + var configuration = provider.GetRequiredService<IConfiguration>(); |
| 37 | + |
| 38 | + var options = new T(); |
| 39 | + configure?.Invoke(options); |
| 40 | + |
| 41 | + configuration.GetSection(sectionName).Bind(options); |
| 42 | + |
| 43 | + options.Validate(); |
| 44 | + return options; |
| 45 | + } |
| 46 | + |
| 47 | + private static void ConfigureOpenTelemetry(IServiceCollection services, OpenTelemetryOptions openTelemetryOptions) |
| 48 | + { |
| 49 | + services.AddOpenTelemetry(configure => |
| 50 | + { |
| 51 | + ConfigureSampler(openTelemetryOptions, configure); |
| 52 | + ConfigureInstrumentations(openTelemetryOptions, configure); |
| 53 | + ConfigureExporters(openTelemetryOptions, configure); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + private static void ConfigureSampler(OpenTelemetryOptions openTelemetryOptions, TracerProviderBuilder configure) |
| 58 | + { |
| 59 | + if (openTelemetryOptions.AlwaysOnSampler) |
| 60 | + { |
| 61 | + configure.SetSampler(new AlwaysOnSampler()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static void ConfigureExporters(OpenTelemetryOptions openTelemetryOptions, TracerProviderBuilder configure) |
| 66 | + { |
| 67 | + if (openTelemetryOptions.Jaeger.Enabled) |
| 68 | + { |
| 69 | + configure.UseJaegerExporter(config => |
| 70 | + { |
| 71 | + config.ServiceName = openTelemetryOptions.Jaeger.ServiceName; |
| 72 | + |
| 73 | + config.AgentHost = openTelemetryOptions.Jaeger.Host; |
| 74 | + config.AgentPort = openTelemetryOptions.Jaeger.Port; |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static void ConfigureInstrumentations(OpenTelemetryOptions openTelemetryOptions, TracerProviderBuilder configure) |
| 80 | + { |
| 81 | + configure.AddAspNetCoreInstrumentation(config => |
| 82 | + { |
| 83 | + config.TextFormat = GetTextFormat(openTelemetryOptions); |
| 84 | + }); |
| 85 | + |
| 86 | + configure.AddHttpClientInstrumentation(config => |
| 87 | + { |
| 88 | + config.TextFormat = GetTextFormat(openTelemetryOptions); |
| 89 | + }); |
| 90 | + |
| 91 | + configure.AddSqlClientDependencyInstrumentation(); |
| 92 | + } |
| 93 | + |
| 94 | + private static ITextFormat GetTextFormat(OpenTelemetryOptions openTelemetryOptions) |
| 95 | + => openTelemetryOptions.Istio |
| 96 | + ? new B3Format() |
| 97 | + : (ITextFormat)new TraceContextFormat(); |
| 98 | + } |
| 99 | +} |
0 commit comments