1- // Copyright (c) .NET Foundation. All rights reserved.
1+ // Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the MIT License. See License.txt in the project root for license information.
33
44using System ;
88using Azure . Core ;
99using Azure . Identity ;
1010using Azure . Monitor . OpenTelemetry . Exporter ;
11- using Azure . Monitor . OpenTelemetry . LiveMetrics ;
1211using Microsoft . AspNetCore . Routing ;
1312using Microsoft . Azure . WebJobs . Script . Metrics ;
1413using Microsoft . Extensions . Configuration ;
@@ -34,122 +33,106 @@ internal static void ConfigureOpenTelemetry(this ILoggingBuilder loggingBuilder,
3433 bool enableOtlp = ! string . IsNullOrWhiteSpace ( otlpEndpoint ) ;
3534 bool enableAzureMonitor = ! string . IsNullOrWhiteSpace ( azMonConnectionString ) ;
3635
37- TokenCredential credential = enableAzureMonitor ? GetTokenCredential ( context . Configuration ) : null ;
38-
3936 // If placeholder mode is disabled and both OTLP and Azure Monitor are not enabled, avoid configuring OpenTelemetry.
4037 if ( ! enableOtlp && ! enableAzureMonitor && telemetryMode != TelemetryMode . Placeholder )
4138 {
4239 return ;
4340 }
4441
45- loggingBuilder . ConfigureLogging ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode ) ;
42+ loggingBuilder . ConfigureLogging ( ) ;
4643
4744 loggingBuilder . Services
4845 . AddOpenTelemetry ( )
46+ . ConfigureExporters ( context . Configuration , enableOtlp , enableAzureMonitor , azMonConnectionString , telemetryMode )
4947 . ConfigureResource ( r => ConfigureResource ( r ) )
50- . ConfigureMetrics ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode )
51- . ConfigureTracing ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode )
48+ . ConfigureMetrics ( )
49+ . ConfigureTracing ( )
5250 . ConfigureEventLogLevel ( context . Configuration ) ;
5351
5452 // Azure SDK instrumentation is experimental.
5553 AppContext . SetSwitch ( "Azure.Experimental.EnableActivitySource" , true ) ;
5654 }
5755
58- private static IOpenTelemetryBuilder ConfigureMetrics ( this IOpenTelemetryBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential , TelemetryMode telemetryMode )
56+ private static IOpenTelemetryBuilder ConfigureExporters ( this IOpenTelemetryBuilder builder , IConfiguration configuration , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TelemetryMode telemetryMode )
57+ {
58+ // Avoid configuring the exporter in placeholder mode, as it will default to sending telemetry to the predefined endpoint. These transmissions will be unsuccessful and create unnecessary noise.
59+ if ( telemetryMode == TelemetryMode . Placeholder )
60+ {
61+ return builder ;
62+ }
63+
64+ if ( enableOtlp )
65+ {
66+ builder . UseOtlpExporter ( ) ;
67+ }
68+
69+ if ( enableAzureMonitor )
70+ {
71+ TokenCredential credential = GetTokenCredential ( configuration ) ;
72+ builder . UseAzureMonitorExporter ( options => ConfigureAzureMonitorOptions ( options , azMonConnectionString , credential ) ) ;
73+ }
74+
75+ return builder ;
76+ }
77+
78+ private static IOpenTelemetryBuilder ConfigureMetrics ( this IOpenTelemetryBuilder builder )
5979 {
6080 return builder . WithMetrics ( builder =>
6181 {
6282 builder . AddAspNetCoreInstrumentation ( )
63- . AddRuntimeInstrumentation ( )
64- . AddProcessInstrumentation ( )
65- . AddMeter ( HostMetrics . FaasMeterName )
66- . AddView ( HostMetrics . FaasInvokeDuration , new ExplicitBucketHistogramConfiguration
67- {
68- Boundaries = new double [ ] { 0.005 , 0.01 , 0.025 , 0.05 , 0.075 , 0.1 , 0.25 , 0.5 , 0.75 , 1 , 2.5 , 5 , 7.5 , 10 }
69- } ) ;
70-
71- // Avoid configuring the exporter in placeholder mode, as it will default to sending telemetry to the predefined endpoint. These transmissions will be unsuccessful and create unnecessary noise.
72- if ( telemetryMode != TelemetryMode . Placeholder )
73- {
74- if ( enableOtlp )
75- {
76- builder . AddOtlpExporter ( ) ;
77- }
78- if ( enableAzureMonitor )
83+ . AddRuntimeInstrumentation ( )
84+ . AddProcessInstrumentation ( )
85+ . AddMeter ( HostMetrics . FaasMeterName )
86+ . AddView ( HostMetrics . FaasInvokeDuration , new ExplicitBucketHistogramConfiguration
7987 {
80- builder . AddAzureMonitorMetricExporter ( opt => ConfigureAzureMonitorOptions ( opt , azMonConnectionString , credential ) ) ;
81- }
82- }
88+ Boundaries = new double [ ] { 0.005 , 0.01 , 0.025 , 0.05 , 0.075 , 0.1 , 0.25 , 0.5 , 0.75 , 1 , 2.5 , 5 , 7.5 , 10 } ,
89+ RecordMinMax = true
90+ } ) ;
8391 } ) ;
8492 }
8593
86- private static IOpenTelemetryBuilder ConfigureTracing ( this IOpenTelemetryBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential , TelemetryMode telemetryMode )
94+ private static IOpenTelemetryBuilder ConfigureTracing ( this IOpenTelemetryBuilder builder )
8795 {
8896 return builder . WithTracing ( builder =>
8997 {
9098 builder
91- . AddSource ( "Azure.*" )
92- . AddSource ( "Microsoft.Azure.Webjobs.Extensions.*" )
93- . AddSource ( "WebJobs.Extensions.DurableTask" )
94- . AddSource ( "DurableTask.*" )
95- . AddAspNetCoreInstrumentation ( o =>
96- {
97- o . EnrichWithHttpResponse = ( activity , httpResponse ) =>
99+ . AddSource ( "Azure.Messaging.ServiceBus.ServiceBusProcessor" )
100+ . AddSource ( "Azure.Messaging.EventHubs.EventProcessor" )
101+ . AddSource ( "Azure.Functions.Extensions.Mcp" )
102+ . AddSource ( "Microsoft.Azure.WebJobs.Extensions.*" )
103+ . AddSource ( "Microsoft.Azure.WebJobs" )
104+ . AddSource ( "WebJobs.Extensions.DurableTask" )
105+ . AddSource ( "DurableTask.*" )
106+ . AddAspNetCoreInstrumentation ( o =>
98107 {
99- if ( Activity . Current is not null )
108+ o . EnrichWithHttpResponse = ( activity , httpResponse ) =>
100109 {
101- Activity . Current . AddTag ( ResourceSemanticConventions . FaaSTrigger , OpenTelemetryConstants . HttpTriggerType ) ;
102-
103- var routingFeature = httpResponse . HttpContext . Features . Get < IRoutingFeature > ( ) ;
104- if ( routingFeature is null )
110+ if ( Activity . Current is not null )
105111 {
106- return ;
107- }
108-
109- var template = routingFeature . RouteData . Routers . FirstOrDefault ( r => r is Route ) as Route ;
110- Activity . Current . DisplayName = $ "{ Activity . Current . DisplayName } { template ? . RouteTemplate } ";
111- Activity . Current . AddTag ( ResourceSemanticConventions . HttpRoute , template ? . RouteTemplate ) ;
112- }
113- } ;
114- } ) ;
115-
116- // Avoid configuring the exporter in placeholder mode, as it will default to sending telemetry to the predefined endpoint. These transmissions will be unsuccessful and create unnecessary noise.
117- if ( telemetryMode != TelemetryMode . Placeholder )
118- {
119- if ( enableOtlp )
120- {
121- builder . AddOtlpExporter ( ) ;
122- }
112+ Activity . Current . AddTag ( ResourceSemanticConventions . FaaSTrigger , OpenTelemetryConstants . HttpTriggerType ) ;
123113
124- if ( enableAzureMonitor )
125- {
126- builder . AddAzureMonitorTraceExporter ( opt => ConfigureAzureMonitorOptions ( opt , azMonConnectionString , credential ) ) ;
127- }
128- }
114+ var routingFeature = httpResponse . HttpContext . Features . Get < IRoutingFeature > ( ) ;
115+ if ( routingFeature is null )
116+ {
117+ return ;
118+ }
129119
130- builder . AddProcessor ( ActivitySanitizingProcessor . Instance ) ;
120+ var template = routingFeature . RouteData . Routers . FirstOrDefault ( r => r is Route ) as Route ;
121+ Activity . Current . DisplayName = $ "{ Activity . Current . DisplayName } { template ? . RouteTemplate } ";
122+ Activity . Current . AddTag ( ResourceSemanticConventions . HttpRoute , template ? . RouteTemplate ) ;
123+ }
124+ } ;
125+ o . Filter = context => ! context . Request . Host . Host . Equals ( "127.0.0.1" , StringComparison . OrdinalIgnoreCase ) ;
126+ } )
127+ . AddProcessor ( ActivitySanitizingProcessor . Instance ) ;
131128 } ) ;
132129 }
133130
134- private static ILoggingBuilder ConfigureLogging ( this ILoggingBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential , TelemetryMode telemetryMode )
131+ private static ILoggingBuilder ConfigureLogging ( this ILoggingBuilder builder )
135132 {
136133 builder . AddOpenTelemetry ( o =>
137134 {
138135 o . SetResourceBuilder ( ConfigureResource ( ResourceBuilder . CreateDefault ( ) ) ) ;
139-
140- // Avoid configuring the exporter in placeholder mode, as it will default to sending telemetry to the predefined endpoint. These transmissions will be unsuccessful and create unnecessary noise.
141- if ( telemetryMode != TelemetryMode . Placeholder )
142- {
143- if ( enableOtlp )
144- {
145- o . AddOtlpExporter ( ) ;
146- }
147- if ( enableAzureMonitor )
148- {
149- o . AddAzureMonitorLogExporter ( options => ConfigureAzureMonitorOptions ( options , azMonConnectionString , credential ) ) ;
150- }
151- }
152-
153136 o . IncludeFormattedMessage = true ;
154137 o . IncludeScopes = false ;
155138 } ) ;
0 commit comments