Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
- Add JitTrace Files for v4.1045
- Throw exception instead of timing out when worker channel exits before initializing gRPC (#10937)
- Adding empty remote message check in the SystemLogger (#11473)
- Fix `webPubSubTrigger`'s for Flex consumption sku (#11489)
- Add support for trace sampling (#11497)
- Fix `webPubSubTrigger`'s for Flex consumption sku (#11489)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Linq;
Expand Down Expand Up @@ -47,7 +48,7 @@ internal static void ConfigureOpenTelemetry(this ILoggingBuilder loggingBuilder,
.ConfigureExporters(context.Configuration, enableOtlp, enableAzureMonitor, azMonConnectionString, telemetryMode)
.ConfigureResource(r => ConfigureResource(r))
.ConfigureMetrics()
.ConfigureTracing()
.ConfigureTracing(context.Configuration)
.ConfigureEventLogLevel(context.Configuration);

// Azure SDK instrumentation is experimental.
Expand Down Expand Up @@ -92,7 +93,7 @@ private static IOpenTelemetryBuilder ConfigureMetrics(this IOpenTelemetryBuilder
});
}

private static IOpenTelemetryBuilder ConfigureTracing(this IOpenTelemetryBuilder builder)
private static IOpenTelemetryBuilder ConfigureTracing(this IOpenTelemetryBuilder builder, IConfiguration configuration)
{
return builder.WithTracing(builder =>
{
Expand All @@ -104,6 +105,7 @@ private static IOpenTelemetryBuilder ConfigureTracing(this IOpenTelemetryBuilder
.AddSource("Microsoft.Azure.WebJobs")
.AddSource("WebJobs.Extensions.DurableTask")
.AddSource("DurableTask.*")
.SetSampler(GetSampler(configuration))
.AddAspNetCoreInstrumentation(o =>
{
o.EnrichWithHttpResponse = (activity, httpResponse) =>
Expand Down Expand Up @@ -167,6 +169,28 @@ private static IOpenTelemetryBuilder ConfigureTracing(this IOpenTelemetryBuilder
});
}

private static Sampler GetSampler(IConfiguration configuration)
{
var otlpTracesSampler = GetConfigurationValue(EnvironmentSettingNames.OtlpTracesSampler, configuration);
var otlpTracesSamplerArg = GetConfigurationValue(EnvironmentSettingNames.OtlpTracesSamplerArg, configuration);
double otlpTracesSamplerArgVal = 1.0;
double.TryParse(otlpTracesSamplerArg, out otlpTracesSamplerArgVal);
var samplerOptions = new Dictionary<string, Sampler> // https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/
{
{"always_on", new AlwaysOnSampler() },
{"always_off", new AlwaysOffSampler() },
{"traceidratio", new TraceIdRatioBasedSampler(otlpTracesSamplerArgVal) },
{"parentbased_always_on", new ParentBasedSampler(new AlwaysOnSampler()) },
{"parentbased_always_off", new ParentBasedSampler(new AlwaysOffSampler()) },
{"parentbased_traceidratio", new ParentBasedSampler(new TraceIdRatioBasedSampler(otlpTracesSamplerArgVal)) },
};
if (!string.IsNullOrEmpty(otlpTracesSampler) && samplerOptions.ContainsKey(otlpTracesSampler))
{
return samplerOptions[otlpTracesSampler];
}
return new AlwaysOnSampler(); // Otel default
}

private static ILoggingBuilder ConfigureLogging(this ILoggingBuilder builder)
{
builder.AddOpenTelemetry(o =>
Expand Down
2 changes: 2 additions & 0 deletions src/WebJobs.Script/Environment/EnvironmentSettingNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public static class EnvironmentSettingNames
public const string AppInsightsQuickPulseAuthApiKey = "APPINSIGHTS_QUICKPULSEAUTHAPIKEY";
public const string OtlpEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT";
public const string OpenTelemetryEventListenerLogLevel = "OTEL_EVENT_LISTENER_LOGLEVEL";
public const string OtlpTracesSampler = "OTEL_TRACES_SAMPLER";
public const string OtlpTracesSamplerArg = "OTEL_TRACES_SAMPLER_ARG";
public const string AppInsightsAgent = "APPLICATIONINSIGHTS_ENABLE_AGENT";
public const string FunctionsExtensionVersion = "FUNCTIONS_EXTENSION_VERSION";
public const string FunctionWorkerRuntime = "FUNCTIONS_WORKER_RUNTIME";
Expand Down