Skip to content

Commit b5168f2

Browse files
committed
document Azure Functions process detection
1 parent 44e08b7 commit b5168f2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/development/AzureFunctions.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,46 @@ 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](for-ai/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 any isolated functions process (host or worker)
73+
if (EnvironmentHelpers.IsAzureFunctionsIsolated())
74+
{
75+
// This code runs in both host and worker
76+
}
77+
78+
// Detect host process
79+
if (EnvironmentHelpers.IsRunningInAzureFunctionsHost())
80+
{
81+
// This code runs only in the host process
82+
}
83+
84+
// Detect worker process
85+
if (EnvironmentHelpers.IsAzureFunctionsIsolated() && !EnvironmentHelpers.IsRunningInAzureFunctionsHost())
86+
{
87+
// This code runs only in the worker process
88+
}
89+
90+
```
91+
5292
`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.
5393

5494
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)