Skip to content

chore(libdd-traceutils): Update cloud environment detection logic for Serverless [SVLS-8799]#1857

Draft
kathiehuang wants to merge 7 commits intomainfrom
kathie.huang/update-cloud-env-detection-logic
Draft

chore(libdd-traceutils): Update cloud environment detection logic for Serverless [SVLS-8799]#1857
kathiehuang wants to merge 7 commits intomainfrom
kathie.huang/update-cloud-env-detection-logic

Conversation

@kathiehuang
Copy link
Copy Markdown
Contributor

@kathiehuang kathiehuang commented Apr 8, 2026

What does this PR do?

  • Updates cloud environment detection logic to be more specific and aligned with the Serverless Compatibility Layers (example) by requiring multiple cloud-specific environment variables to identify each platform
  • Add 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
  • Added 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 who 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 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 and update the commit hash in datadog-trace-agent/Cargo.toml
    everywhere that libdatadog is used
  3. Follow the instructions in the Serverless Compatibility Layer docs 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:
image

Redeployed the Azure Function with this change. The error disappeared and we're getting traces:
image

Also deployed a Python Google Cloud Function Gen 1 with this change. Interestingly, it's getting detected as Gen 2.

image

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) to have the same logic as other runtimes (serverless-compat-py)

To update:

  • serverless-compat-java
  • serverless-compat-dotnet

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

📚 Documentation Check Results

⚠️ 561 documentation warning(s) found

📦 libdd-trace-utils - 561 warning(s)


Updated: 2026-04-09 21:23:13 UTC | Commit: 2bcd623 | missing-docs job results

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

Clippy Allow Annotation Report

Comparing clippy allow annotations between branches:

  • Base Branch: origin/main
  • PR Branch: origin/kathie.huang/update-cloud-env-detection-logic

Summary by Rule

Rule Base Branch PR Branch Change

Annotation Counts by File

File Base Branch PR Branch Change

Annotation Stats by Crate

Crate Base Branch PR Branch Change
clippy-annotation-reporter 5 5 No change (0%)
datadog-ffe-ffi 1 1 No change (0%)
datadog-ipc 21 21 No change (0%)
datadog-live-debugger 6 6 No change (0%)
datadog-live-debugger-ffi 10 10 No change (0%)
datadog-profiling-replayer 4 4 No change (0%)
datadog-remote-config 3 3 No change (0%)
datadog-sidecar 55 55 No change (0%)
libdd-common 10 10 No change (0%)
libdd-common-ffi 12 12 No change (0%)
libdd-data-pipeline 5 5 No change (0%)
libdd-ddsketch 2 2 No change (0%)
libdd-dogstatsd-client 1 1 No change (0%)
libdd-profiling 13 13 No change (0%)
libdd-telemetry 19 19 No change (0%)
libdd-tinybytes 4 4 No change (0%)
libdd-trace-normalization 2 2 No change (0%)
libdd-trace-obfuscation 8 8 No change (0%)
libdd-trace-utils 15 15 No change (0%)
Total 196 196 No change (0%)

About This Report

This report tracks Clippy allow annotations for specific rules, showing how they've changed in this PR. Decreasing the number of these annotations generally improves code quality.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

🔒 Cargo Deny Results

No issues found!

📦 libdd-trace-utils - ✅ No issues


Updated: 2026-04-09 21:23:08 UTC | Commit: 2bcd623 | dependency-check job results

@dd-octo-sts
Copy link
Copy Markdown
Contributor

dd-octo-sts bot commented Apr 8, 2026

Artifact Size Benchmark Report

aarch64-alpine-linux-musl
Artifact Baseline Commit Change
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.a 83.34 MB 83.34 MB 0% (0 B) 👌
/aarch64-alpine-linux-musl/lib/libdatadog_profiling.so 7.63 MB 7.63 MB 0% (0 B) 👌
aarch64-unknown-linux-gnu
Artifact Baseline Commit Change
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.a 99.40 MB 99.40 MB 0% (0 B) 👌
/aarch64-unknown-linux-gnu/lib/libdatadog_profiling.so 10.17 MB 10.17 MB 0% (0 B) 👌
libdatadog-x64-windows
Artifact Baseline Commit Change
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.dll 25.16 MB 25.16 MB 0% (0 B) 👌
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.lib 78.21 KB 78.21 KB 0% (0 B) 👌
/libdatadog-x64-windows/debug/dynamic/datadog_profiling_ffi.pdb 184.19 MB 184.21 MB +0% (+16.00 KB) 👌
/libdatadog-x64-windows/debug/static/datadog_profiling_ffi.lib 917.34 MB 917.62 MB +.02% (+279.28 KB) 🔍
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.dll 7.88 MB 7.88 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.lib 78.21 KB 78.21 KB 0% (0 B) 👌
/libdatadog-x64-windows/release/dynamic/datadog_profiling_ffi.pdb 23.64 MB 23.64 MB 0% (0 B) 👌
/libdatadog-x64-windows/release/static/datadog_profiling_ffi.lib 46.13 MB 46.13 MB 0% (0 B) 👌
libdatadog-x86-windows
Artifact Baseline Commit Change
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.dll 21.63 MB 21.63 MB 0% (0 B) 👌
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.lib 79.42 KB 79.42 KB 0% (0 B) 👌
/libdatadog-x86-windows/debug/dynamic/datadog_profiling_ffi.pdb 188.30 MB 188.33 MB +.01% (+32.00 KB) 🔍
/libdatadog-x86-windows/debug/static/datadog_profiling_ffi.lib 902.08 MB 902.35 MB +.02% (+273.52 KB) 🔍
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.dll 6.12 MB 6.12 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.lib 79.42 KB 79.42 KB 0% (0 B) 👌
/libdatadog-x86-windows/release/dynamic/datadog_profiling_ffi.pdb 25.31 MB 25.31 MB 0% (0 B) 👌
/libdatadog-x86-windows/release/static/datadog_profiling_ffi.lib 43.61 MB 43.61 MB 0% (0 B) 👌
x86_64-alpine-linux-musl
Artifact Baseline Commit Change
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.a 74.47 MB 74.47 MB 0% (0 B) 👌
/x86_64-alpine-linux-musl/lib/libdatadog_profiling.so 8.52 MB 8.52 MB 0% (0 B) 👌
x86_64-unknown-linux-gnu
Artifact Baseline Commit Change
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.a 91.77 MB 91.77 MB +0% (+128 B) 👌
/x86_64-unknown-linux-gnu/lib/libdatadog_profiling.so 10.20 MB 10.20 MB 0% (0 B) 👌

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 98.60140% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.78%. Comparing base (33896de) to head (70a4ee8).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1857      +/-   ##
==========================================
+ Coverage   71.69%   71.78%   +0.09%     
==========================================
  Files         429      429              
  Lines       67886    68019     +133     
==========================================
+ Hits        48672    48830     +158     
+ Misses      19214    19189      -25     
Components Coverage Δ
libdd-crashtracker 66.03% <ø> (+0.04%) ⬆️
libdd-crashtracker-ffi 34.47% <ø> (+0.38%) ⬆️
libdd-alloc 98.77% <ø> (ø)
libdd-data-pipeline 86.27% <ø> (-0.03%) ⬇️
libdd-data-pipeline-ffi 73.17% <ø> (-0.12%) ⬇️
libdd-common 79.16% <ø> (ø)
libdd-common-ffi 73.87% <ø> (ø)
libdd-telemetry 65.98% <ø> (ø)
libdd-telemetry-ffi 16.75% <ø> (ø)
libdd-dogstatsd-client 82.64% <ø> (ø)
datadog-ipc 73.10% <ø> (ø)
libdd-profiling 82.13% <ø> (+<0.01%) ⬆️
libdd-profiling-ffi 64.94% <ø> (ø)
datadog-sidecar 30.83% <ø> (+0.36%) ⬆️
datdog-sidecar-ffi 10.19% <ø> (+1.72%) ⬆️
spawn-worker 54.69% <ø> (ø)
libdd-tinybytes 93.16% <ø> (ø)
libdd-trace-normalization 81.71% <ø> (ø)
libdd-trace-obfuscation 87.24% <ø> (ø)
libdd-trace-protobuf 68.25% <ø> (ø)
libdd-trace-utils 89.18% <98.60%> (+0.45%) ⬆️
datadog-tracer-flare 86.88% <ø> (ø)
libdd-log 74.69% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@datadog-datadog-prod-us1
Copy link
Copy Markdown
Contributor

datadog-datadog-prod-us1 bot commented Apr 8, 2026

✅ Tests

🎉 All green!

❄️ No new flaky tests detected
🧪 All tests passed

🎯 Code Coverage (details)
Patch Coverage: 98.60%
Overall Coverage: 71.79% (+0.09%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 70a4ee8 | Docs | Datadog PR Page | Was this helpful? React with 👍/👎 or give us feedback!

@kathiehuang kathiehuang changed the title [SVLS-8799] Update cloud environment detection logic for Serverless chore(libdd-traceutils): Update cloud environment detection logic for Serverless [SVLS-8799] Apr 8, 2026
@kathiehuang kathiehuang requested a review from Copilot April 8, 2026 17:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the cloud environment detection logic in libdd-trace-utils to be more restrictive and aligned with the Serverless Compatibility Layers by requiring multiple cloud-specific environment variables to identify each platform, rather than relying on a single environment variable. This prevents misidentification when environment variables like FUNCTION_NAME are manually set in environments where they don't naturally occur.

Changes:

  • Modified read_cloud_env() to require multiple detection environment variables per platform instead of single variables
  • Refactored to collect all detected environments and handle conflicts (returning None when multiple platforms are detected)
  • Added structured logging using the tracing crate for debug messages when one environment is detected and error messages for zero, multiple, or incomplete environment detections
  • Added comprehensive unit tests covering all detection paths, missing name variables, single-var-only cases, multiple conflicting environments, and no environment scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kathiehuang kathiehuang requested a review from Copilot April 8, 2026 18:23
@kathiehuang
Copy link
Copy Markdown
Contributor Author

@codex review

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 89fcc56d6f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

impl EnvGuard {
fn set(key: &'static str, value: &str) -> Self {
let saved = env::var(key).ok();
unsafe { env::set_var(key, value) };
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Serialize tests before mutating process env

These tests mutate global environment state via unsafe env::set_var/remove_var without any synchronization, but Rust’s test harness runs tests in parallel by default. That creates cross-test interference (and unsound concurrent env access) where one test can clear variables while another is reading them, which already reproduces as nondeterministic failures with cargo test -p libdd-trace-utils config_utils (without --test-threads=1). Please guard env mutation with a global lock or mark this module’s tests as serial.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to serialize the unit tests rather than requiring the tests to be run with --test-threads=1 a1b3fea

@github-actions github-actions bot removed the ci-build label Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants