Skip to content

fix(common): graceful fallback when LambdaTraceProvider is unavailable#1176

Merged
hjgraca merged 3 commits intodevelopfrom
fix/enforce-minimum-lambda-core-version
Apr 11, 2026
Merged

fix(common): graceful fallback when LambdaTraceProvider is unavailable#1176
hjgraca merged 3 commits intodevelopfrom
fix/enforce-minimum-lambda-core-version

Conversation

@hjgraca
Copy link
Copy Markdown
Contributor

@hjgraca hjgraca commented Apr 10, 2026

fixes #1173

Summary

Changes

Fixes TypeLoadException for customers on Amazon.Lambda.Core < 2.8.0 who upgrade Powertools Logging to 3.1.0+.

XRayTraceId now probes for LambdaTraceProvider.CurrentTraceId on first access via a [MethodImpl(NoInlining)] isolated method, caches the result, and falls back to the _X_AMZN_TRACE_ID environment variable when the type is unavailable. No reflection — AOT-compatible.

User experience

Before: Customers on Amazon.Lambda.Core < 2.8.0 get a TypeLoadException at runtime when the logger tries to resolve LambdaTraceProvider.

After: The logger gracefully falls back to reading the _X_AMZN_TRACE_ID environment variable (matching pre-3.1.0 behaviour). Customers on Amazon.Lambda.Core >= 2.8.0 continue to get correct per-invocation trace ID isolation for LMI.

Checklist

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

#1173)

XRayTraceId now probes for LambdaTraceProvider.CurrentTraceId on first
access and caches the result. When the type is not available (customers
on Amazon.Lambda.Core < 2.8.0), it falls back to reading the
_X_AMZN_TRACE_ID environment variable — matching pre-3.1.0 behaviour —
instead of throwing a TypeLoadException at runtime.

Closes #1173
Copilot AI review requested due to automatic review settings April 10, 2026 12:10
@boring-cyborg boring-cyborg bot added area/common Core Powertools utility tests labels Apr 10, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 10, 2026
@github-actions github-actions bot added the bug Unexpected, reproducible and unintended software behaviour label Apr 10, 2026
Instance method was setting a static field. Refactored to accept a
Func<string> fallback so the method can be fully static.
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 10, 2026
Copy link
Copy Markdown
Contributor

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 prevents runtime TypeLoadException for customers running Amazon.Lambda.Core < 2.8.0 by making PowertoolsConfigurations.XRayTraceId gracefully fall back to the _X_AMZN_TRACE_ID environment variable when LambdaTraceProvider is unavailable, while preserving the LambdaTraceProvider.CurrentTraceId path when present.

Changes:

  • Add a first-access probe + cached availability flag for LambdaTraceProvider, with env-var fallback on TypeLoadException.
  • Isolate the LambdaTraceProvider.CurrentTraceId call behind a [MethodImpl(NoInlining)] method to avoid eager type loading.
  • Add unit tests covering provider-available, fallback, and cached behavior paths.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
libraries/src/AWS.Lambda.Powertools.Common/Core/PowertoolsConfigurations.cs Introduces a cached probe for LambdaTraceProvider availability and env-var fallback for XRayTraceId.
libraries/tests/AWS.Lambda.Powertools.Common.Tests/Core/PowertoolsConfigurationsTest.cs Adds unit tests for XRayTraceId provider vs fallback behavior and caching behavior.

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

- Thread safety: replace bool? with int + Volatile.Read/Write for
  atomic access to _traceProviderState across concurrent invocations
- Tests: add try/finally with ResetTraceProviderState to prevent
  cross-test contamination of static state
- Tests: Assert.NotNull on reflection field lookups so tests fail
  loudly if the field is renamed
- Tests: rename caching test to clarify it covers cached-unavailable
  path, not the initial probe
- Tests: remove invalid assertion on return value (LambdaTraceProvider
  returns null in test env with no active trace)
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 10, 2026
@sonarqubecloud
Copy link
Copy Markdown

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 10, 2026

Codecov Report

❌ Patch coverage is 78.94737% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.42%. Comparing base (3b2bbb6) to head (1f245d6).
⚠️ Report is 8 commits behind head on develop.

Files with missing lines Patch % Lines
...Powertools.Common/Core/PowertoolsConfigurations.cs 78.94% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1176      +/-   ##
===========================================
- Coverage    79.43%   79.42%   -0.01%     
===========================================
  Files          302      302              
  Lines        12636    12654      +18     
  Branches      1505     1507       +2     
===========================================
+ Hits         10037    10051      +14     
- Misses        2141     2145       +4     
  Partials       458      458              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hjgraca hjgraca merged commit 7754dd4 into develop Apr 11, 2026
11 of 13 checks passed
@hjgraca hjgraca deleted the fix/enforce-minimum-lambda-core-version branch April 11, 2026 08:30
@github-actions
Copy link
Copy Markdown

@aws-powertools/powertools-lambda-net No related issues found. Please ensure 'pending-release' label is applied before releasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/common Core Powertools utility bug Unexpected, reproducible and unintended software behaviour size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeLoadException for LambdaTraceProvider when upgrading Powertools.Logging from 3.0.2 to 3.1.0/3.2.0

3 participants