1
- // Copyright (c) .NET Foundation. All rights reserved.
1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
4
using System ;
8
8
using Azure . Core ;
9
9
using Azure . Identity ;
10
10
using Azure . Monitor . OpenTelemetry . Exporter ;
11
- using Azure . Monitor . OpenTelemetry . LiveMetrics ;
12
11
using Microsoft . AspNetCore . Routing ;
13
12
using Microsoft . Azure . WebJobs . Script . Metrics ;
14
13
using Microsoft . Extensions . Configuration ;
@@ -34,122 +33,106 @@ internal static void ConfigureOpenTelemetry(this ILoggingBuilder loggingBuilder,
34
33
bool enableOtlp = ! string . IsNullOrWhiteSpace ( otlpEndpoint ) ;
35
34
bool enableAzureMonitor = ! string . IsNullOrWhiteSpace ( azMonConnectionString ) ;
36
35
37
- TokenCredential credential = enableAzureMonitor ? GetTokenCredential ( context . Configuration ) : null ;
38
-
39
36
// If placeholder mode is disabled and both OTLP and Azure Monitor are not enabled, avoid configuring OpenTelemetry.
40
37
if ( ! enableOtlp && ! enableAzureMonitor && telemetryMode != TelemetryMode . Placeholder )
41
38
{
42
39
return ;
43
40
}
44
41
45
- loggingBuilder . ConfigureLogging ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode ) ;
42
+ loggingBuilder . ConfigureLogging ( ) ;
46
43
47
44
loggingBuilder . Services
48
45
. AddOpenTelemetry ( )
46
+ . ConfigureExporters ( context . Configuration , enableOtlp , enableAzureMonitor , azMonConnectionString , telemetryMode )
49
47
. ConfigureResource ( r => ConfigureResource ( r ) )
50
- . ConfigureMetrics ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode )
51
- . ConfigureTracing ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode )
48
+ . ConfigureMetrics ( )
49
+ . ConfigureTracing ( )
52
50
. ConfigureEventLogLevel ( context . Configuration ) ;
53
51
54
52
// Azure SDK instrumentation is experimental.
55
53
AppContext . SetSwitch ( "Azure.Experimental.EnableActivitySource" , true ) ;
56
54
}
57
55
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 )
59
79
{
60
80
return builder . WithMetrics ( builder =>
61
81
{
62
82
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
79
87
{
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
+ } ) ;
83
91
} ) ;
84
92
}
85
93
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 )
87
95
{
88
96
return builder . WithTracing ( builder =>
89
97
{
90
98
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 =>
98
107
{
99
- if ( Activity . Current is not null )
108
+ o . EnrichWithHttpResponse = ( activity , httpResponse ) =>
100
109
{
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 )
105
111
{
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 ) ;
123
113
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
+ }
129
119
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 ) ;
131
128
} ) ;
132
129
}
133
130
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 )
135
132
{
136
133
builder . AddOpenTelemetry ( o =>
137
134
{
138
135
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
-
153
136
o . IncludeFormattedMessage = true ;
154
137
o . IncludeScopes = false ;
155
138
} ) ;
0 commit comments