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

- 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)
@sdangol
Copy link
Collaborator

sdangol commented Sep 5, 2025

@dcabib There's an issue in the SonarQube finding, can you look into that.

- Refactor if-then-else logic to single return statement
- Maintains same functionality for provisioned concurrency detection
- Addresses SonarQube code smell flagged by @sdangol
- All tests passing: 21 tests, 0 failures
@pull-request-size pull-request-size bot added size/XS and removed size/S labels Sep 5, 2025
@dcabib
Copy link
Author

dcabib commented Sep 5, 2025

@sdangol Issue fixed.

@sonarqubecloud
Copy link

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

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

Right now we're explicitly comparing the AWS_LAMBDA_INITIALIZATION_TYPE to this value provisioned-concurrency and returning false in the isColdStart() method.

As documented here, there are other types of initialization types during which isColdStart() should return false (hint snap-start).

A better and more future proof approach would be to instead check if the initialization type is on-demand and if it is, maintain a logic similar to the existing one, if it's not, always return false.

dcabib added a commit to dcabib/powertools-lambda-java that referenced this pull request Sep 22, 2025
- Remove .sonarcloud.properties changes (keep SonarCloud rules strict)
- Remove LambdaHandlerProcessor.java changes (belong in PR aws-powertools#2124)
- Remove LambdaConstants.java changes (belong in PR aws-powertools#2124)
- Keep PR focused only on CRaC tracing functionality
- Maintain GraalVM compatibility fixes for CRaC tracing

SpotBugs exclusions are justified for CRaC beforeCheckpoint methods which
intentionally ignore return values during priming operations.
@dcabib
Copy link
Author

dcabib commented Sep 22, 2025

Hey @dreamorosi, thanks for the feedback! I've cleaned up the PR - removed the SonarCloud changes, took out the unrelated provisioned concurrency stuff that belongs in #2124, fixed the license header, and kept it focused just on CRaC tracing. The SpotBugs exclusions are there because CRaC beforeCheckpoint methods intentionally ignore return values during priming. Should be good to go now!

@dreamorosi
Copy link
Contributor

Hi, I see you've missed/ignored the review feedback that I left here and instead left a comment for a completely different PR.

While we're open to contributors, we also need to mindful of our team's time and misusing reviewers time is not appropriate.

Thank you, but I'll close this PR.

@dreamorosi dreamorosi closed this Sep 23, 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

3 participants