Skip to content

Commit be46b51

Browse files
committed
enable ASP.NET Core diagnostics observer in the worker process
1 parent ceb51ca commit be46b51

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

tracer/src/Datadog.Trace/ClrProfiler/Instrumentation.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,20 +470,24 @@ private static void StartDiagnosticManager()
470470
{
471471
var observers = new List<DiagnosticObserver>();
472472

473-
// get environment variables directly so we don't access Trace.Instance yet
474-
var functionsExtensionVersion = EnvironmentHelpers.GetEnvironmentVariable(PlatformKeys.AzureFunctions.FunctionsExtensionVersion);
475-
var functionsWorkerRuntime = EnvironmentHelpers.GetEnvironmentVariable(PlatformKeys.AzureFunctions.FunctionsWorkerRuntime);
473+
// For Azure Functions, we need to handle AspNetCoreDiagnosticObserver differently based on the process type.
474+
// Skip AspNetCoreDiagnosticObserver in:
475+
// - In-process functions (due to separate Assembly Load Context issues)
476+
// - Isolated functions host process (to avoid duplicate spans)
477+
// Enable AspNetCoreDiagnosticObserver in:
478+
// - Isolated functions worker process (to create aspnet_core.request spans that azure_functions.invoke can parent to)
479+
// - All other scenarios (non-Azure Functions)
480+
var isInAzureFunctionsHost = EnvironmentHelpers.IsRunningInAzureFunctionsHost();
481+
var shouldSkipAspNetCore = isInAzureFunctionsHost || (EnvironmentHelpers.IsAzureFunctions() && !EnvironmentHelpers.IsAzureFunctionsIsolated());
476482

477-
if (!string.IsNullOrEmpty(functionsExtensionVersion) && !string.IsNullOrEmpty(functionsWorkerRuntime))
483+
if (shouldSkipAspNetCore)
478484
{
479-
// Not adding the `AspNetCoreDiagnosticObserver` is particularly important for in-process Azure Functions.
480-
// The AspNetCoreDiagnosticObserver will be loaded in a separate Assembly Load Context, breaking the connection of AsyncLocal.
481-
// This is because user code is loaded within the functions host in a separate context.
482-
// Even in isolated functions, we don't want the AspNetCore spans to be created.
483-
Log.Debug("Skipping AspNetCoreDiagnosticObserver in Azure Functions.");
485+
// Skip AspNetCoreDiagnosticObserver in Azure Functions host process or in-process functions
486+
Log.Debug("Skipping AspNetCoreDiagnosticObserver in Azure Functions (host process or in-process).");
484487
}
485488
else
486489
{
490+
// Enable AspNetCoreDiagnosticObserver in isolated worker process or other scenarios
487491
observers.Add(new AspNetCoreDiagnosticObserver());
488492
observers.Add(new QuartzDiagnosticObserver());
489493
}

0 commit comments

Comments
 (0)