Skip to content

Conversation

@dcabib
Copy link

@dcabib dcabib commented Sep 4, 2025

Summary

Fixes #2113 - Provisioned concurrency not considered in Logging and Metrics

Problem

When Lambda functions use Provisioned Concurrency, AWS sets AWS_LAMBDA_INITIALIZATION_TYPE=provisioned-concurrency to indicate pre-warmed environments. However, Powertools incorrectly reported these as cold starts across all utilities:

  • Logging: cold_start: true in JSON logs
  • Metrics: Emitted ColdStart metric with value 1
  • Tracing: ColdStart: true annotation in X-Ray segments

Solution

Updated the centralized cold start detection logic in LambdaHandlerProcessor.isColdStart() to check for the AWS_LAMBDA_INITIALIZATION_TYPE environment variable.

Changes

  • LambdaConstants.java: Added constants for AWS environment variables
  • LambdaHandlerProcessor.java: Enhanced isColdStart() method to detect provisioned concurrency

Evidence

Before Fix (Bug):

{"level":"INFO","message":"Handler Event","cold_start":true,"service":"triage"}

ColdStart metric emitted: "ColdStart":1.0

After Fix (Resolved):

{"level":"INFO","message":"Handler Event","cold_start":false,"service":"triage"}

No ColdStart metric emitted

Testing

  • ✅ Deployed to AWS with provisioned concurrency
  • ✅ Reproduced original bug in CloudWatch logs
  • ✅ Applied fix and verified resolution
  • ✅ All three utilities (Logging, Metrics, Tracing) now work correctly

Impact

  • Centralized Fix: Single change fixes all utilities
  • Backward Compatible: Preserves existing behavior
  • AWS Standard: Uses official AWS environment variable

…alization

- Add CRaC dependency and generate-classesloaded-file profile to both modules
- Implement Resource interface in TracingUtils and JsonConfig classes
- Add classesloaded.txt files for automatic class preloading
- Add comprehensive CRaC tests for both modules
- Update documentation with SnapStart priming guidance
- Update spotbugs-exclude.xml for beforeCheckpoint methods

Addresses issues aws-powertools#2004 and aws-powertools#2003
- Add TracingUtils.prime() method with no side-effects for public API
- Move ClassPreLoader.preloadClasses() to top of beforeCheckpoint methods
- Remove unnecessary exception catching in CRaC hooks
- Update JsonConfig to use direct imports instead of reflection for AWS Lambda events
- Fix CRaC tests to not use reflection for accessing private fields
- Update documentation examples to use TracingUtils.prime()
- Consolidate SpotBugs exclusions into single Or structure

All CRaC tests passing (4 tests, 0 failures)
- Add CRaC dependency and generate-classesloaded-file profile to powertools-tracing
- Implement Resource interface in TracingUtils class with CRaC hooks
- Add classesloaded.txt file for automatic class preloading
- Add TracingUtils.prime() method for public API with no side-effects
- Add comprehensive CRaC tests for tracing module
- Update documentation with SnapStart priming guidance
- Update spotbugs-exclude.xml for beforeCheckpoint method

Addresses issue aws-powertools#2004
- Add AWS_LAMBDA_INITIALIZATION_TYPE environment variable check
- Prevent false cold start reporting when using provisioned concurrency
- Fixes issue aws-powertools#2113 across Logging, Metrics, and Tracing utilities

Before: cold_start: true (incorrect with provisioned concurrency)
After: cold_start: false (correct with provisioned concurrency)
@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 4, 2025

@dcabib
Copy link
Author

dcabib commented Sep 4, 2025

Closing this PR as it contained unintended CRAC-related changes. Created clean PR #2124 with only the provisioned concurrency fix.

@dcabib dcabib closed this Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Support correct cold start detection for non-on-demand invocation types

1 participant