33using Genocs . Core . Builders ;
44using Genocs . Logging . Configurations ;
55using Genocs . Tracing . Jaeger . Configurations ;
6- using Jaeger . Samplers ;
7- using Jaeger . Senders . Thrift ;
86using Microsoft . Extensions . DependencyInjection ;
97using Microsoft . Extensions . Logging ;
108using OpenTelemetry ;
@@ -28,143 +26,113 @@ public static class Extensions
2826 public static IGenocsBuilder AddOpenTelemetry ( this IGenocsBuilder builder )
2927 {
3028
31- var appOptions = builder . GetOptions < AppOptions > ( AppOptions . Position ) ;
29+ AppOptions options = builder . GetOptions < AppOptions > ( AppOptions . Position ) ;
3230
3331 // No OpenTelemetryTracing in case of missing ServiceName
34- if ( string . IsNullOrWhiteSpace ( appOptions . Service ) )
32+ if ( string . IsNullOrWhiteSpace ( options . Service ) )
3533 {
3634 return builder ;
3735 }
3836
37+ //builder.Logging.AddOpenTelemetry(logging =>
38+ //{
39+ // logging.IncludeFormattedMessage = true;
40+ // logging.IncludeScopes = true;
41+ //});
42+
3943 var services = builder . Services ;
4044
4145 // Set Custom Open telemetry
42- services . AddOpenTelemetry ( ) . WithTracing ( x =>
43- {
44- TracerProviderBuilder provider = x . SetResourceBuilder ( ResourceBuilder . CreateDefault ( )
45- . AddService ( appOptions . Service )
46- . AddTelemetrySdk ( )
47- . AddEnvironmentVariableDetector ( ) )
48- . AddSource ( "*" ) ;
49-
50- var loggerOptions = builder . GetOptions < LoggerOptions > ( LoggerOptions . Position ) ;
51-
52- // No OpenTelemetryTracing in case of missing LoggerSettings
53- if ( loggerOptions != null )
46+ services . AddOpenTelemetry ( )
47+ . WithTracing ( x =>
5448 {
55- if ( loggerOptions . Mongo != null && loggerOptions . Mongo . Enabled )
49+ TracerProviderBuilder provider = x . SetResourceBuilder ( ResourceBuilder . CreateDefault ( )
50+ . AddService ( serviceName : options . Service , serviceVersion : options . Version , serviceInstanceId : options . Instance )
51+ . AddTelemetrySdk ( )
52+ . AddEnvironmentVariableDetector ( ) )
53+ . AddAspNetCoreInstrumentation ( )
54+ . AddHttpClientInstrumentation ( )
55+ . AddSource ( "*" ) ;
56+
57+ var loggerOptions = builder . GetOptions < LoggerOptions > ( LoggerOptions . Position ) ;
58+
59+ // No OpenTelemetryTracing in case of missing LoggerSettings
60+ if ( loggerOptions != null )
5661 {
57- // you should add MongoDB.Driver.Core.Extensions.OpenTelemetry NuGet package
58- provider . AddMongoDBInstrumentation ( ) ;
59- }
62+ if ( loggerOptions . Mongo != null && loggerOptions . Mongo . Enabled )
63+ {
64+ // you should add MongoDB.Driver.Core.Extensions.OpenTelemetry NuGet package
65+ provider . AddMongoDBInstrumentation ( ) ;
66+ }
6067
61- // Check for Console config
62- if ( loggerOptions . Console != null && loggerOptions . Console . Enabled )
63- {
64- // you should add OpenTelemetry.Exporter.Console NuGet package
65- provider . AddConsoleExporter ( ) ;
66- }
68+ // Check for Console config
69+ if ( loggerOptions . Console != null && loggerOptions . Console . Enabled )
70+ {
71+ // you should add OpenTelemetry.Exporter.Console NuGet package
72+ // Any OTEL supportable exporter can be used here
73+ provider . AddConsoleExporter ( ) ;
74+ }
6775
68- // Check for Azure ApplicationInsights config
69- if ( loggerOptions . Azure != null && loggerOptions . Azure . Enabled )
70- {
71- provider . AddAzureMonitorTraceExporter ( o =>
76+ // Check for Azure ApplicationInsights config
77+ if ( loggerOptions . Azure != null && loggerOptions . Azure . Enabled )
7278 {
73- o . ConnectionString = loggerOptions . Azure . ConnectionString ;
74- } ) ;
79+ provider . AddAzureMonitorTraceExporter ( o =>
80+ {
81+ o . ConnectionString = loggerOptions . Azure . ConnectionString ;
82+ } ) ;
83+ }
7584 }
76- }
7785
78- var jaegerOptions = builder . GetOptions < JaegerOptions > ( JaegerOptions . Position ) ;
86+ var jaegerOptions = builder . GetOptions < JaegerOptions > ( JaegerOptions . Position ) ;
7987
80- if ( jaegerOptions != null && jaegerOptions . Enabled )
81- {
82-
83- provider . AddJaegerExporter ( o =>
88+ if ( jaegerOptions != null && jaegerOptions . Enabled )
8489 {
85- o . AgentHost = jaegerOptions . UdpHost ;
86- o . AgentPort = jaegerOptions . UdpPort ;
87- o . MaxPayloadSizeInBytes = jaegerOptions . MaxPacketSize ;
88- o . ExportProcessorType = ExportProcessorType . Batch ;
89- o . BatchExportProcessorOptions = new BatchExportProcessorOptions < System . Diagnostics . Activity >
90- {
91- MaxQueueSize = 2048 ,
92- ScheduledDelayMilliseconds = 5000 ,
93- ExporterTimeoutMilliseconds = 30000 ,
94- MaxExportBatchSize = 512 ,
95- } ;
96- } ) ;
97- }
98-
99- /*
100- Action<ResourceBuilder> appResourceBuilder =
101- resource => resource
102- .AddDetector(new ContainerResourceDetector());
103-
104- builder.Services.AddOpenTelemetry()
105- .ConfigureResource(appResourceBuilder)
106- .WithTracing(tracerBuilder => tracerBuilder
107- .AddRedisInstrumentation(
108- cartStore.GetConnection(),
109- options => options.SetVerboseDatabaseStatements = true)
110- .AddAspNetCoreInstrumentation()
111- .AddGrpcClientInstrumentation()
112- .AddHttpClientInstrumentation()
113- .AddOtlpExporter())
114- .WithMetrics(meterBuilder => meterBuilder
115- .AddRuntimeInstrumentation()
116- .AddAspNetCoreInstrumentation()
117- .AddOtlpExporter());
118- */
119- } ) ;
120-
121- return builder ;
122- }
12390
124- private static ISampler GetSampler ( JaegerOptions options )
125- {
126- switch ( options . Sampler )
127- {
128- case "const" : return new ConstSampler ( true ) ;
129- case "rate" : return new RateLimitingSampler ( options . MaxTracesPerSecond ) ;
130- case "probabilistic" : return new ProbabilisticSampler ( options . SamplingRate ) ;
131- default : return new ConstSampler ( true ) ;
132- }
133- }
134-
135- private static HttpSender BuildHttpSender ( JaegerOptions . HttpSenderSettings ? options )
136- {
137- if ( options is null )
138- {
139- throw new Exception ( "Missing Jaeger HTTP sender options." ) ;
140- }
141-
142- if ( string . IsNullOrWhiteSpace ( options . Endpoint ) )
143- {
144- throw new Exception ( "Missing Jaeger HTTP sender endpoint." ) ;
145- }
91+ provider . AddOtlpExporter ( o =>
92+ {
93+ o . Endpoint = new Uri ( jaegerOptions . Endpoint ) ;
94+ o . Protocol = OpenTelemetry . Exporter . OtlpExportProtocol . Grpc ;
95+ o . ExportProcessorType = ExportProcessorType . Batch ;
96+ o . BatchExportProcessorOptions = new BatchExportProcessorOptions < System . Diagnostics . Activity >
97+ {
98+ MaxQueueSize = 2048 ,
99+ ScheduledDelayMilliseconds = 5000 ,
100+ ExporterTimeoutMilliseconds = 30000 ,
101+ MaxExportBatchSize = 512 ,
102+ } ;
103+ } ) ;
104+ }
146105
147- var builder = new HttpSender . Builder ( options . Endpoint ) ;
148- if ( options . MaxPacketSize > 0 )
149- {
150- builder = builder . WithMaxPacketSize ( options . MaxPacketSize ) ;
151- }
106+ /*
107+ Action<ResourceBuilder> appResourceBuilder =
108+ resource => resource
109+ .AddDetector(new ContainerResourceDetector());
110+
111+ builder.Services.AddOpenTelemetry()
112+ .ConfigureResource(appResourceBuilder)
113+ .WithTracing(tracerBuilder => tracerBuilder
114+ .AddRedisInstrumentation(
115+ cartStore.GetConnection(),
116+ options => options.SetVerboseDatabaseStatements = true)
117+ .AddAspNetCoreInstrumentation()
118+ .AddGrpcClientInstrumentation()
119+ .AddHttpClientInstrumentation()
120+ .AddOtlpExporter())
121+
122+ */
123+ } ) . WithMetrics ( x =>
124+ {
125+ MeterProviderBuilder provider = x . SetResourceBuilder ( ResourceBuilder . CreateDefault ( ) ) ;
152126
153- if ( ! string . IsNullOrWhiteSpace ( options . AuthToken ) )
154- {
155- builder = builder . WithAuth ( options . AuthToken ) ;
156- }
127+ provider . AddConsoleExporter ( ) ;
128+ provider . AddAspNetCoreInstrumentation ( ) ;
157129
158- if ( ! string . IsNullOrWhiteSpace ( options . Username ) && ! string . IsNullOrWhiteSpace ( options . Password ) )
159- {
160- builder = builder . WithAuth ( options . Username , options . Password ) ;
161- }
130+ // provider.AddRuntimeInstrumentation();
131+ provider . AddHttpClientInstrumentation ( ) ;
132+ provider . AddOtlpExporter ( ) ;
162133
163- if ( ! string . IsNullOrWhiteSpace ( options . UserAgent ) )
164- {
165- builder = builder . WithUserAgent ( options . Username ) ;
166- }
134+ } ) ;
167135
168- return builder . Build ( ) ;
136+ return builder ;
169137 }
170138}
0 commit comments