Commit d60d0a4
authored
fix(libdd-traceutils): Update cloud environment detection logic for Serverless [SVLS-8799] (#1857)
# What does this PR do?
- Updates cloud environment detection logic to be more specific and aligned with the Serverless Compatibility Layers ([example](https://github.com/DataDog/datadog-serverless-compat-js/blob/7e68fce247490f721995c26def258b539a6d788c/src/index.ts#L18-L45)) by requiring multiple cloud-specific environment variables to identify each platform
- Adds structured logging: debug when one environment is detected, error when zero or multiple are detected, and error when a platform is detected but its name variable is missing
- Return None when multiple conflicting environments are detected instead of silently returning the first match
- Adds unit tests covering all detection paths, missing name vars, single-var-only cases, multiple environments, and no environment
Platform | Before | After
-- | -- | --
AWS Lambda | AWS_LAMBDA_FUNCTION_NAME | AWS_LAMBDA_INITIALIZATION_TYPE (detect) + AWS_LAMBDA_FUNCTION_NAME (name)
Azure Functions | WEBSITE_SITE_NAME | FUNCTIONS_EXTENSION_VERSION + FUNCTIONS_WORKER_RUNTIME (detect) + WEBSITE_SITE_NAME (name)
GCP Cloud Functions Gen 1 | FUNCTION_NAME | FUNCTION_NAME + GCP_PROJECT
GCP Cloud Run Functions Gen 2 | K_SERVICE | K_SERVICE + FUNCTION_TARGET
Azure Spring Apps | ASCSVCRT_SPRING__APPLICATION__NAME | unchanged
# Motivation
A customer instrumenting a Java Azure Function had set `FUNCTION_NAME` as an environment variable. This led the current libdatadog logic to return `EnvironmentType::CloudFunction`, which led to [this](https://github.com/DataDog/serverless-components/blob/96ab942a20979aeb8640214f027cb94e5da7b6fe/crates/datadog-trace-agent/src/env_verifier.rs#L96) being called in serverless-components which returned an error after running a Google-specific metadata check.
As a result, we want the environment detection logic to check multiple cloud-specific env vars at once.
# Additional Notes
Once this PR is merged, we plan to make a new PR in serverless-components to update the libdatadog commit hash.
# How to test the change?
Unit tests: Run `cargo test -p libdd-trace-utils config_utils`
1. Use git log to find this PR's most recent commit hash
2. Clone [serverless-components](https://github.com/DataDog/serverless-components/tree/main) and update the commit hash in [datadog-trace-agent/Cargo.toml](https://github.com/DataDog/serverless-components/blob/main/crates/datadog-trace-agent/Cargo.toml)
everywhere that libdatadog is used
3. Follow the instructions in the [Serverless Compatibility Layer docs](https://datadoghq.atlassian.net/wiki/spaces/SLS/pages/2977497119/Serverless+Compatibility+Layer) to deploy sample apps
4. Turn `DD_LOG_LEVEL=DEBUG` in GCP/Azure functions to make sure the correct environment is detected
5. Set `FUNCTION_NAME` as an env var in Azure Functions to make sure it isn't identified as a Google Cloud Function
6. To test with Google Cloud Gen 1, deploy a gen1 app and use the .whl from `serverless-compat-self-monitoring/azure_functions/code/compat/python/datadog_serverless_compat-0.0.0-py3-none-any.whl` and drop it in the root of the app. Replace `datadog-serverless-compat` in `requirements.txt` to this file name.
Deployed a Java Azure Function without this change with `FUNCTION_NAME=myfunction` and got the same error:
<img width="591" height="224" alt="image" src="https://github.com/user-attachments/assets/69a7db4a-7fc3-4532-94c8-f6d8d8a853f4" />
Redeployed the Azure Function with this change. The error disappeared and we're getting traces:
<img width="687" height="270" alt="image" src="https://github.com/user-attachments/assets/5e087889-3345-4fb4-a514-124194965613" />
Also deployed a Python Google Cloud Function Gen 1 with this change. Interestingly, it's getting detected as Gen 2.
<img width="706" height="279" alt="image" src="https://github.com/user-attachments/assets/c0db4123-683e-4234-bda7-5a8efbc36685" />
I printed out the env vars and `FUNCTION_TARGET` and `K_SERVICE` are being set, but not `FUNCTION_NAME` and `GCP_PROJECT`:
```
DEFAULT 2026-04-09T21:52:43.186346Z [resource.labels.functionName: gen1-func-envvars] [labels.executionId: j3djq5xx9qqk] FUNCTION_NAME=NOT SET
DEFAULT 2026-04-09T21:52:43.186349Z [resource.labels.functionName: gen1-func-envvars] [labels.executionId: j3djq5xx9qqk] GCP_PROJECT=NOT SET
DEFAULT 2026-04-09T21:52:43.186393Z [resource.labels.functionName: gen1-func-envvars] [labels.executionId: j3djq5xx9qqk] FUNCTION_TARGET=main
DEFAULT 2026-04-09T21:52:43.186399Z [resource.labels.functionName: gen1-func-envvars] [labels.executionId: j3djq5xx9qqk] K_SERVICE=gen1-func-envvars
```
Serverless compatibility layers that don't check `FUNCTION_TARGET` or `K_SERVICE` should be updated (e.g. [serverless-compat-java](https://github.com/DataDog/datadog-serverless-compat-java/blob/a1bceb25ce55cd811938e89d750b4f10319c4336/src/main/java/com/datadog/ServerlessCompatAgent.java#L64)) to have the same logic as other runtimes ([serverless-compat-py](https://github.com/DataDog/datadog-serverless-compat-py/blob/71b0cb39ceff4bb6fc5ed822c1545ecdd2fd47fa/datadog_serverless_compat/main.py#L34))
To update:
- serverless-compat-java
- serverless-compat-dotnet
Co-authored-by: kathie.huang <kathie.huang@datadoghq.com>1 parent 3c34f14 commit d60d0a4
1 file changed
Lines changed: 244 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
14 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
15 | 23 | | |
16 | | - | |
17 | | - | |
18 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
19 | 34 | | |
20 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
21 | 40 | | |
22 | | - | |
| 41 | + | |
23 | 42 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 43 | + | |
| 44 | + | |
29 | 45 | | |
30 | | - | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
31 | 70 | | |
32 | | - | |
33 | 71 | | |
34 | 72 | | |
35 | 73 | | |
| |||
51 | 89 | | |
52 | 90 | | |
53 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
0 commit comments