@@ -28,42 +28,34 @@ internal static class OpenTelemetryConfigurationExtensions
28
28
{
29
29
internal static void ConfigureOpenTelemetry ( this ILoggingBuilder loggingBuilder , HostBuilderContext context , TelemetryMode telemetryMode )
30
30
{
31
- var connectionString = GetConfigurationValue ( EnvironmentSettingNames . AppInsightsConnectionString , context . Configuration ) ;
32
- var ( azMonConnectionString , credential , enableOtlp , enableAzureMonitor ) = telemetryMode switch
33
- {
34
- // Initializing OTel services during placeholder mode as well to avoid the cost of JITting these objects during specialization.
35
- // Azure Monitor Exporter requires a connection string to be initialized. Use placeholder connection string if in placeholder mode.
36
- TelemetryMode . Placeholder => (
37
- "InstrumentationKey=00000000-0000-0000-0000-000000000000;" ,
38
- null ,
39
- true ,
40
- true ) ,
41
- _ => (
42
- connectionString ,
43
- GetTokenCredential ( context . Configuration ) ,
44
- ! string . IsNullOrEmpty ( GetConfigurationValue ( EnvironmentSettingNames . OtlpEndpoint , context . Configuration ) ) ,
45
- ! string . IsNullOrEmpty ( connectionString ) )
46
- } ;
47
-
48
- // If neither OTLP nor Azure Monitor is enabled, don't configure OpenTelemetry.
49
- if ( ! enableOtlp && ! enableAzureMonitor )
31
+ var otlpEndpoint = GetConfigurationValue ( EnvironmentSettingNames . OtlpEndpoint , context . Configuration ) ;
32
+ var azMonConnectionString = GetConfigurationValue ( EnvironmentSettingNames . AppInsightsConnectionString , context . Configuration ) ;
33
+
34
+ bool enableOtlp = ! string . IsNullOrWhiteSpace ( otlpEndpoint ) ;
35
+ bool enableAzureMonitor = ! string . IsNullOrWhiteSpace ( azMonConnectionString ) ;
36
+
37
+ TokenCredential credential = enableAzureMonitor ? GetTokenCredential ( context . Configuration ) : null ;
38
+
39
+ // If placeholder mode is disabled and both OTLP and Azure Monitor are not enabled, avoid configuring OpenTelemetry.
40
+ if ( ! enableOtlp && ! enableAzureMonitor && telemetryMode != TelemetryMode . Placeholder )
50
41
{
51
42
return ;
52
43
}
53
44
54
- loggingBuilder
55
- . ConfigureLogging ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential ) . Services
45
+ loggingBuilder . ConfigureLogging ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode ) ;
46
+
47
+ loggingBuilder . Services
56
48
. AddOpenTelemetry ( )
57
49
. ConfigureResource ( r => ConfigureResource ( r ) )
58
- . ConfigureMetrics ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential )
59
- . ConfigureTracing ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential )
50
+ . ConfigureMetrics ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode )
51
+ . ConfigureTracing ( enableOtlp , enableAzureMonitor , azMonConnectionString , credential , telemetryMode )
60
52
. ConfigureEventLogLevel ( context . Configuration ) ;
61
53
62
54
// Azure SDK instrumentation is experimental.
63
55
AppContext . SetSwitch ( "Azure.Experimental.EnableActivitySource" , true ) ;
64
56
}
65
57
66
- private static IOpenTelemetryBuilder ConfigureMetrics ( this IOpenTelemetryBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential )
58
+ private static IOpenTelemetryBuilder ConfigureMetrics ( this IOpenTelemetryBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential , TelemetryMode telemetryMode )
67
59
{
68
60
return builder . WithMetrics ( builder =>
69
61
{
@@ -76,18 +68,22 @@ private static IOpenTelemetryBuilder ConfigureMetrics(this IOpenTelemetryBuilder
76
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 }
77
69
} ) ;
78
70
79
- if ( enableOtlp )
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 )
80
73
{
81
- builder . AddOtlpExporter ( ) ;
82
- }
83
- if ( enableAzureMonitor )
84
- {
85
- builder . AddAzureMonitorMetricExporter ( opt => ConfigureAzureMonitorOptions ( opt , azMonConnectionString , credential ) ) ;
74
+ if ( enableOtlp )
75
+ {
76
+ builder . AddOtlpExporter ( ) ;
77
+ }
78
+ if ( enableAzureMonitor )
79
+ {
80
+ builder . AddAzureMonitorMetricExporter ( opt => ConfigureAzureMonitorOptions ( opt , azMonConnectionString , credential ) ) ;
81
+ }
86
82
}
87
83
} ) ;
88
84
}
89
85
90
- private static IOpenTelemetryBuilder ConfigureTracing ( this IOpenTelemetryBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential )
86
+ private static IOpenTelemetryBuilder ConfigureTracing ( this IOpenTelemetryBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential , TelemetryMode telemetryMode )
91
87
{
92
88
return builder . WithTracing ( builder =>
93
89
{
@@ -113,34 +109,44 @@ private static IOpenTelemetryBuilder ConfigureTracing(this IOpenTelemetryBuilder
113
109
} ;
114
110
} ) ;
115
111
116
- if ( enableOtlp )
112
+ // 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.
113
+ if ( telemetryMode != TelemetryMode . Placeholder )
117
114
{
118
- builder . AddOtlpExporter ( ) ;
119
- }
115
+ if ( enableOtlp )
116
+ {
117
+ builder . AddOtlpExporter ( ) ;
118
+ }
120
119
121
- if ( enableAzureMonitor )
122
- {
123
- builder . AddAzureMonitorTraceExporter ( opt => ConfigureAzureMonitorOptions ( opt , azMonConnectionString , credential ) ) ;
124
- builder . AddLiveMetrics ( opt => ConfigureAzureMonitorOptions ( opt , azMonConnectionString , credential ) ) ;
120
+ if ( enableAzureMonitor )
121
+ {
122
+ builder . AddAzureMonitorTraceExporter ( opt => ConfigureAzureMonitorOptions ( opt , azMonConnectionString , credential ) ) ;
123
+ builder . AddLiveMetrics ( opt => ConfigureAzureMonitorOptions ( opt , azMonConnectionString , credential ) ) ;
124
+ }
125
125
}
126
126
127
127
builder . AddProcessor ( ActivitySanitizingProcessor . Instance ) ;
128
128
} ) ;
129
129
}
130
130
131
- private static ILoggingBuilder ConfigureLogging ( this ILoggingBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential )
131
+ private static ILoggingBuilder ConfigureLogging ( this ILoggingBuilder builder , bool enableOtlp , bool enableAzureMonitor , string azMonConnectionString , TokenCredential credential , TelemetryMode telemetryMode )
132
132
{
133
133
builder . AddOpenTelemetry ( o =>
134
134
{
135
135
o . SetResourceBuilder ( ConfigureResource ( ResourceBuilder . CreateDefault ( ) ) ) ;
136
- if ( enableOtlp )
137
- {
138
- o . AddOtlpExporter ( ) ;
139
- }
140
- if ( enableAzureMonitor )
136
+
137
+ // 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.
138
+ if ( telemetryMode != TelemetryMode . Placeholder )
141
139
{
142
- o . AddAzureMonitorLogExporter ( options => ConfigureAzureMonitorOptions ( options , azMonConnectionString , credential ) ) ;
140
+ if ( enableOtlp )
141
+ {
142
+ o . AddOtlpExporter ( ) ;
143
+ }
144
+ if ( enableAzureMonitor )
145
+ {
146
+ o . AddAzureMonitorLogExporter ( options => ConfigureAzureMonitorOptions ( options , azMonConnectionString , credential ) ) ;
147
+ }
143
148
}
149
+
144
150
o . IncludeFormattedMessage = true ;
145
151
o . IncludeScopes = false ;
146
152
} ) ;
0 commit comments