Skip to content

Commit b9f9add

Browse files
lucaspimentelclaude
andcommitted
docs: Add documentation for detecting host vs worker process in Azure Functions
Add comprehensive documentation to AzureFunctions.md explaining how to detect whether code is running in the host or worker process for isolated Azure Functions. Includes: - EnvironmentHelpers.IsRunningInAzureFunctionsHost() method documentation - EnvironmentHelpers.IsAzureFunctionsIsolated() method documentation - Usage examples for detecting host-only, worker-only, and both processes - Implementation details about command-line flag detection - Common use cases for process detection 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent d5bdb35 commit b9f9add

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

docs/development/AzureFunctions.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,56 @@ Isolated functions are the only supported model for Azure Functions going forwar
4949

5050
For detailed information about the isolated worker architecture, gRPC protocol, and middleware model, see [Azure Functions Architecture Deep Dive](AzureFunctions-Architecture.md).
5151

52+
### Detecting Host vs Worker Process
53+
54+
The tracer provides utility methods in `EnvironmentHelpers` (`tracer/src/Datadog.Trace/Util/EnvironmentHelpers.cs`) to detect whether code is running in the host or worker process:
55+
56+
**`EnvironmentHelpers.IsRunningInAzureFunctionsHost()`** (line 144)
57+
- Returns `true` when running in the **host process** (`func.exe` or `Microsoft.Azure.WebJobs.Script.WebHost`)
58+
- Checks:
59+
1. Environment is Azure Functions (`IsAzureFunctions()`)
60+
2. `FUNCTIONS_WORKER_RUNTIME` is set to `"dotnet-isolated"`
61+
3. Command line does NOT contain `--functions-worker-id` or `--workerId` flags
62+
63+
**`EnvironmentHelpers.IsAzureFunctionsIsolated()`** (line 170)
64+
- Returns `true` for **both host and worker processes** in isolated functions
65+
- Only checks:
66+
1. Environment is Azure Functions (`IsAzureFunctions()`)
67+
2. `FUNCTIONS_WORKER_RUNTIME` is set to `"dotnet-isolated"`
68+
69+
**Usage examples:**
70+
71+
```csharp
72+
// Detect host process
73+
if (EnvironmentHelpers.IsRunningInAzureFunctionsHost())
74+
{
75+
// This code runs only in func.exe
76+
}
77+
78+
// Detect worker process
79+
if (EnvironmentHelpers.IsAzureFunctionsIsolated() && !EnvironmentHelpers.IsRunningInAzureFunctionsHost())
80+
{
81+
// This code runs only in the worker process
82+
}
83+
84+
// Detect any isolated functions process (host or worker)
85+
if (EnvironmentHelpers.IsAzureFunctionsIsolated())
86+
{
87+
// This code runs in both host and worker
88+
}
89+
```
90+
91+
**Implementation details:**
92+
93+
The detection relies on command-line arguments that differ between host and worker:
94+
- **Host process**: `Microsoft.Azure.WebJobs.Script.WebHost.dll` (no worker flags)
95+
- **Worker process**: `MyApp.dll --workerId <GUID> --functions-worker-id <GUID>`
96+
97+
This mechanism is used throughout the tracer to:
98+
- Apply process-specific behavior (e.g., filtering spans by process)
99+
- Tag spans with `aas.function.process: host` or `aas.function.process: worker`
100+
- Enable/disable integrations based on the process type
101+
52102
`func.exe` sets up an in-process Azure Function for every function in the customer's app. Each of the functions in `func.exe` are simple calls that proxy the request to the customer app, and then return the response.
53103

54104
When an HTTP request is received by `func.exe`, it runs the in-process function as normal. As part of the in-process function execution, it creates a GRPC message (by serializing the incoming HTTP requests to a GRPC message), and forwards the request over GRPC to the customer app. The customer's app runs the _real_ Azure function, and returns the response back over GRPC, where it is deserialized and turned into an HTTP response.

0 commit comments

Comments
 (0)