Skip to content

Commit 2ab1077

Browse files
Avoiding exporter set-up in the placeholder mode. (#11090)
* Avoiding exporter set-up in the placeholder mode.
1 parent a700490 commit 2ab1077

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
- Added support for collecting cross-platform perf traces and generating PGO JIT traces (#11062)
1212
- Memory allocation optimizations in `DependencyHelper.GetExtensionRequirements` (#11022)
1313
- Fix Instance Manager for CV1 Migration (#11072)
14+
- Avoid setting up OTel and AzMon exporter in the placeholder mode. (#11090)
1415
- Update Java Worker Version to [2.19.1](https://github.com/Azure/azure-functions-java-worker/releases/tag/2.19.1)

src/WebJobs.Script/Diagnostics/OpenTelemetry/OpenTelemetryConfigurationExtensions.cs

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,34 @@ internal static class OpenTelemetryConfigurationExtensions
2828
{
2929
internal static void ConfigureOpenTelemetry(this ILoggingBuilder loggingBuilder, HostBuilderContext context, TelemetryMode telemetryMode)
3030
{
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)
5041
{
5142
return;
5243
}
5344

54-
loggingBuilder
55-
.ConfigureLogging(enableOtlp, enableAzureMonitor, azMonConnectionString, credential).Services
45+
loggingBuilder.ConfigureLogging(enableOtlp, enableAzureMonitor, azMonConnectionString, credential, telemetryMode);
46+
47+
loggingBuilder.Services
5648
.AddOpenTelemetry()
5749
.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)
6052
.ConfigureEventLogLevel(context.Configuration);
6153

6254
// Azure SDK instrumentation is experimental.
6355
AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);
6456
}
6557

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)
6759
{
6860
return builder.WithMetrics(builder =>
6961
{
@@ -76,18 +68,22 @@ private static IOpenTelemetryBuilder ConfigureMetrics(this IOpenTelemetryBuilder
7668
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 }
7769
});
7870

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)
8073
{
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+
}
8682
}
8783
});
8884
}
8985

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)
9187
{
9288
return builder.WithTracing(builder =>
9389
{
@@ -113,34 +109,44 @@ private static IOpenTelemetryBuilder ConfigureTracing(this IOpenTelemetryBuilder
113109
};
114110
});
115111

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)
117114
{
118-
builder.AddOtlpExporter();
119-
}
115+
if (enableOtlp)
116+
{
117+
builder.AddOtlpExporter();
118+
}
120119

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+
}
125125
}
126126

127127
builder.AddProcessor(ActivitySanitizingProcessor.Instance);
128128
});
129129
}
130130

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)
132132
{
133133
builder.AddOpenTelemetry(o =>
134134
{
135135
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)
141139
{
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+
}
143148
}
149+
144150
o.IncludeFormattedMessage = true;
145151
o.IncludeScopes = false;
146152
});

0 commit comments

Comments
 (0)