| | 1 | | using OpenTelemetry.Logs; |
| | 2 | | using OpenTelemetry.Metrics; |
| | 3 | | using OpenTelemetry.Resources; |
| | 4 | | using OpenTelemetry.Trace; |
| | 5 | |
|
| | 6 | | namespace Api.ServiceExtension; |
| | 7 | |
|
| | 8 | | [Flags] |
| | 9 | | public enum TelemetryExporters { |
| | 10 | | None = 0, |
| | 11 | | Jaeger = 1, |
| | 12 | | Zipkin = 2 |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public static class OpenTelemetryCollectionExtensions { |
| | 16 | | public static IServiceCollection AddOpenTelemetryWithExporters( |
| | 17 | | this IServiceCollection services, |
| | 18 | | IConfiguration configuration, |
| 1 | 19 | | TelemetryExporters exporters) { |
| 1 | 20 | | var serviceName = configuration.GetValue<string>("OpenTelemetry:ServiceName") ?? "DefaultApi"; |
| | 21 | |
|
| 1 | 22 | | services.AddOpenTelemetry() |
| 1 | 23 | | .WithTracing(tracer => { |
| 1 | 24 | | tracer |
| 1 | 25 | | .SetSampler<AlwaysOnSampler>() |
| 1 | 26 | | .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName)) |
| 1 | 27 | | .AddAspNetCoreInstrumentation() |
| 1 | 28 | | .AddHttpClientInstrumentation(); |
| 1 | 29 | |
|
| 1 | 30 | | if (exporters.HasFlag(TelemetryExporters.Jaeger)) |
| 1 | 31 | | tracer.AddJaeger(configuration); |
| 1 | 32 | | if (exporters.HasFlag(TelemetryExporters.Zipkin)) |
| 1 | 33 | | tracer.AddZipkin(configuration); |
| 1 | 34 | | }) |
| 1 | 35 | | .WithMetrics(metrics => { |
| 1 | 36 | | metrics |
| 1 | 37 | | .AddAspNetCoreInstrumentation() |
| 1 | 38 | | .AddHttpClientInstrumentation() |
| 1 | 39 | | .AddRuntimeInstrumentation() |
| 1 | 40 | | .AddMeter( |
| 1 | 41 | | "Microsoft.AspNetCore.Hosting", |
| 1 | 42 | | "Microsoft.AspNetCore.Server.Kestrel", |
| 1 | 43 | | "Microsoft.AspNetCore.Http.Connections", |
| 1 | 44 | | "System.Net.Http", |
| 1 | 45 | | serviceName, |
| 1 | 46 | | "Microsoft.AspNetCore.Routing", |
| 1 | 47 | | "Microsoft.AspNetCore.Diagnostics", |
| 1 | 48 | | "Microsoft.AspNetCore.RateLimiting"); |
| 2 | 49 | | }); |
| | 50 | |
|
| 1 | 51 | | services.Configure<OpenTelemetryLoggerOptions>(logging => |
| 1 | 52 | | logging.AddOtlpExporter()); |
| | 53 | |
|
| 1 | 54 | | return services; |
| 1 | 55 | | } |
| | 56 | |
|
| 1 | 57 | | public static TracerProviderBuilder AddJaeger(this TracerProviderBuilder builder, IConfiguration configuration) { |
| 1 | 58 | | var port = configuration.GetValue("Ports:JAEGER_PORT", "4317"); |
| 2 | 59 | | builder.AddOtlpExporter(opt => { |
| 1 | 60 | | opt.Endpoint = new Uri($"http://jaeger:{port}"); |
| 2 | 61 | | }); |
| 1 | 62 | | return builder; |
| 1 | 63 | | } |
| | 64 | |
|
| 1 | 65 | | public static TracerProviderBuilder AddZipkin(this TracerProviderBuilder builder, IConfiguration configuration) { |
| 1 | 66 | | var port = configuration.GetValue("Ports:ZIPKIN_PORT", "9411"); |
| 2 | 67 | | builder.AddZipkinExporter(opt => { |
| 1 | 68 | | opt.Endpoint = new Uri($"http://zipkin:{port}/api/v2/spans"); |
| 2 | 69 | | }); |
| 1 | 70 | | return builder; |
| 1 | 71 | | } |
| | 72 | | } |