-
Notifications
You must be signed in to change notification settings - Fork 156
feat(tracer): middy middleware #324
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
6 commits
Select commit
Hold shift + click to select a range
df2fc25
WIP: captureLambdaHandler middleware
dreamorosi ccba5ef
Completed captureLambdaHandler middleware
dreamorosi 6addea6
Documentation
dreamorosi 5341c47
Improved error catching in tests
dreamorosi c954226
Typos in docs
dreamorosi abbe421
Merge branch 'main' into feat/tracer-middleware
dreamorosi 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './helpers'; | ||
export * from './Tracer'; | ||
export * from './TracerInterface'; | ||
export * from './TracerInterface'; | ||
export * from './middleware/middy'; |
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,76 @@ | ||
import middy from '@middy/core'; | ||
import { Subsegment } from 'aws-xray-sdk-core'; | ||
import { Tracer } from '../Tracer'; | ||
|
||
/** | ||
* A middy middleware automating capture of metadata and annotations on segments or subsegments ofr a Lambda Handler. | ||
* | ||
* Using this middleware on your handler function will automatically: | ||
* * handle the subsegment lifecycle | ||
* * add the `ColdStart` annotation | ||
* * add the function response as metadata | ||
* * add the function error as metadata (if any) | ||
* | ||
* @example | ||
* ```typescript | ||
* import { Tracer, captureLambdaHandler } from '@aws-lambda-powertools/tracer'; | ||
* import middy from '@middy/core'; | ||
* | ||
* const tracer = new Tracer({ serviceName: 'my-service' }); | ||
* | ||
* export const handler = middy(async (_event: any, _context: any) => { | ||
* ... | ||
* }).use(captureLambdaHandler(tracer)); | ||
* ``` | ||
* | ||
* @param tracer - The Tracer instance to use for tracing | ||
* @returns middleware object - The middy middleware object | ||
*/ | ||
const captureLambdaHandler = (target: Tracer): middy.MiddlewareObj => { | ||
const captureLambdaHandlerBefore = async (request: middy.Request): Promise<void> => { | ||
if (target.isTracingEnabled()) { | ||
const subsegment = new Subsegment(`## ${request.context.functionName}`); | ||
target.setSegment(subsegment); | ||
|
||
if (Tracer.isColdStart()) { | ||
target.putAnnotation('ColdStart', true); | ||
} | ||
} | ||
}; | ||
|
||
const captureLambdaHandlerAfter = async (request: middy.Request): Promise<void> => { | ||
if (target.isTracingEnabled()) { | ||
const subsegment = target.getSegment(); | ||
if (request.response !== undefined && target.isCaptureResponseEnabled() === true) { | ||
target.putMetadata(`${request.context.functionName} response`, request.response); | ||
} | ||
|
||
subsegment?.close(); | ||
} | ||
}; | ||
|
||
const captureLambdaHandlerError = async (request: middy.Request): Promise<void> => { | ||
if (target.isTracingEnabled()) { | ||
const subsegment = target.getSegment(); | ||
if (target.isCaptureErrorEnabled() === false) { | ||
subsegment?.addErrorFlag(); | ||
} else { | ||
subsegment?.addError(request.error as Error, false); | ||
} | ||
// TODO: should this error be thrown?? I.e. should we stop the event flow & return? | ||
// throw request.error; | ||
flochaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
subsegment?.close(); | ||
} | ||
}; | ||
|
||
return { | ||
before: captureLambdaHandlerBefore, | ||
after: captureLambdaHandlerAfter, | ||
onError: captureLambdaHandlerError | ||
}; | ||
}; | ||
|
||
export { | ||
captureLambdaHandler, | ||
}; |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.