Skip to content

Commit ac4184c

Browse files
lucaspimentelclaude
andcommitted
Enable AspNetCoreDiagnosticObserver for Azure Functions isolated mode
Previously, the AspNetCoreDiagnosticObserver was disabled for all Azure Functions when FUNCTIONS_EXTENSION_VERSION and FUNCTIONS_WORKER_RUNTIME were present. This was correct for in-process functions (to avoid AsyncLocal context issues with separate Assembly Load Contexts), but incorrect for isolated functions. In isolated mode, the worker process receives actual HTTP requests via gRPC proxying for HTTP triggers. These requests should be instrumented by the AspNetCore observer to properly capture HTTP spans. Changes: - Add logic to detect in-process vs isolated functions - In-process: worker runtime is empty or "dotnet" (skip AspNetCore observer) - Isolated: worker runtime is "dotnet-isolated" (enable AspNetCore observer) - Update log message to clarify in-process vs isolated behavior 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent d83f7d1 commit ac4184c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,16 +469,25 @@ private static void StartDiagnosticManager()
469469
var functionsExtensionVersion = EnvironmentHelpers.GetEnvironmentVariable(Datadog.Trace.Configuration.ConfigurationKeys.AzureFunctions.FunctionsExtensionVersion);
470470
var functionsWorkerRuntime = EnvironmentHelpers.GetEnvironmentVariable(Datadog.Trace.Configuration.ConfigurationKeys.AzureFunctions.FunctionsWorkerRuntime);
471471

472-
if (!string.IsNullOrEmpty(functionsExtensionVersion) && !string.IsNullOrEmpty(functionsWorkerRuntime))
472+
// Check if this is an in-process Azure Function (not isolated)
473+
// In-process: worker runtime is empty or "dotnet" (the host process itself)
474+
// Isolated: worker runtime is "dotnet-isolated"
475+
var isInProcessFunction = !string.IsNullOrEmpty(functionsExtensionVersion)
476+
&& !string.IsNullOrEmpty(functionsWorkerRuntime)
477+
&& !functionsWorkerRuntime.Equals("dotnet-isolated", StringComparison.OrdinalIgnoreCase);
478+
479+
if (isInProcessFunction)
473480
{
474481
// Not adding the `AspNetCoreDiagnosticObserver` is particularly important for in-process Azure Functions.
475482
// The AspNetCoreDiagnosticObserver will be loaded in a separate Assembly Load Context, breaking the connection of AsyncLocal.
476483
// This is because user code is loaded within the functions host in a separate context.
477-
// Even in isolated functions, we don't want the AspNetCore spans to be created.
478-
Log.Debug("Skipping AspNetCoreDiagnosticObserver in Azure Functions.");
484+
Log.Debug("Skipping AspNetCoreDiagnosticObserver in in-process Azure Functions.");
479485
}
480486
else
481487
{
488+
// For isolated functions, enable AspNetCoreDiagnosticObserver to support ASP.NET Core integration
489+
// which uses HTTP proxying for HTTP triggers. In this scenario, the worker receives actual HTTP
490+
// requests that should be instrumented by the ASP.NET Core observer.
482491
observers.Add(new AspNetCoreDiagnosticObserver());
483492
observers.Add(new QuartzDiagnosticObserver());
484493
}

0 commit comments

Comments
 (0)