-
Notifications
You must be signed in to change notification settings - Fork 6
feat: emit X-Ray subsegment on every lambda:// invoke #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { Lambda } from '@aws-sdk/client-lambda'; | ||
| import { captureAWSv3Client, setContextMissingStrategy } from 'aws-xray-sdk-core'; | ||
|
|
||
| /** | ||
| * Whether X-Ray tracing is active in the current runtime. | ||
| * | ||
| * AWS injects `_X_AMZN_TRACE_ID` into any Lambda execution environment that has | ||
| * active tracing, so its presence is a reliable signal that there is a live | ||
| * segment to attach to. Consumers can also force-enable instrumentation via | ||
| * `ALPHA_XRAY_TRACING=true` (e.g. for a non-Lambda runtime that manages its own | ||
| * segments). | ||
| * | ||
| * Outside of these cases (local dev, tests, untraced runtimes) we deliberately | ||
| * leave alpha's Lambda client untouched: alpha is a published library used | ||
| * everywhere, and wrapping an untraced client would emit context-missing errors | ||
| * on every invoke. The default behavior for an untraced caller must be | ||
| * unchanged. | ||
| */ | ||
| const isXRayTracingActive = (): boolean => | ||
| Boolean(process.env._X_AMZN_TRACE_ID) || process.env.ALPHA_XRAY_TRACING === 'true'; | ||
|
|
||
| // Only soften the context-missing strategy once, and never more than once. | ||
| let contextMissingConfigured = false; | ||
|
|
||
| /** | ||
| * Wrap alpha's Lambda client with X-Ray so that each `lambda://` invoke emits a | ||
| * subsegment naming the downstream Lambda (the target service), with trace | ||
| * header propagation. This lights up internal service -> service edges in the | ||
| * X-Ray service graph. | ||
| * | ||
| * `captureAWSv3Client` adds middleware to and returns the *same* client | ||
| * instance, so `lambda.destroy()` and the `config.Lambda` injection escape | ||
| * hatch keep working transparently against the returned client. | ||
| * | ||
| * Safety for untraced consumers is the priority here: | ||
| * - We only wrap when X-Ray is actually active (see `isXRayTracingActive`). | ||
| * - If `AWS_XRAY_CONTEXT_MISSING` is unset, we set the strategy to `LOG_ERROR` | ||
| * so a missing segment logs rather than throws. Consumers that need a | ||
| * different strategy should set that env var in the runtime environment. | ||
| * - Any failure while instrumenting falls back to the original, unwrapped | ||
| * client so tracing can never break a real invoke. | ||
| */ | ||
| export const captureLambdaClient = (lambda: Lambda): Lambda => { | ||
| if (!isXRayTracingActive()) { | ||
| return lambda; | ||
| } | ||
|
|
||
| try { | ||
| if (!contextMissingConfigured && !process.env.AWS_XRAY_CONTEXT_MISSING) { | ||
| setContextMissingStrategy('LOG_ERROR'); | ||
| contextMissingConfigured = true; | ||
| } | ||
| return captureAWSv3Client(lambda); | ||
| } catch { | ||
| // Never let tracing instrumentation break a real invoke. | ||
| return lambda; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overwrites prior context strategy
Low Severity
The first traced
captureLambdaClientcall always invokessetContextMissingStrategy('LOG_ERROR')whenAWS_XRAY_CONTEXT_MISSINGis unset, even if the host app already configured a different missing-context strategy in code. That contradicts the stated guarantee not to override an earliersetContextMissingStrategychoice.Reviewed by Cursor Bugbot for commit e5e7187. Configure here.