-
Notifications
You must be signed in to change notification settings - Fork 154
chore(tracer): cdk examples + e2e tests #347
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 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e0e6e9e
Simplified base CDK examples
dreamorosi d7381b3
Added Tracer examples
dreamorosi 7ffa71e
Merge branch 'main' into chore/tracer_examples
dreamorosi 98b702f
Added back LambdaInterface in Tracer.test
dreamorosi 5e442b0
Updated package.json
dreamorosi 630b56d
Updates to examples & docs
dreamorosi 7d633cf
Updated docs example
dreamorosi dadf44e
Updated examples + added unit test groups
dreamorosi 45f7b60
Added base e2e tests
dreamorosi ac8e578
Added back folder to jest config
dreamorosi 682d62c
Added disabled tracer e2e test
dreamorosi 9a4fc51
Added patch AWS SDK clients to manual e2e test
dreamorosi 196db23
Reduced test timeouts
dreamorosi 640a431
Added captureAWS feature to e2e
dreamorosi 5349347
Added additional e2e test cases
dreamorosi 7caeb28
Removed redundant e2e test files
dreamorosi 1320cec
Relaxed dependency version on cdk examples
dreamorosi 92e4ee3
Resolving merge conflicts
dreamorosi 93c19e3
Resolving merge conflicts
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
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
.../lib/example-stack.MyFunctionWithMiddy.ts → ...b/example-function.MyFunctionWithMiddy.ts
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
27 changes: 27 additions & 0 deletions
27
examples/cdk/lib/example-function.Tracer.CaptureErrorDisabled.ts
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,27 @@ | ||
import middy from '@middy/core' | ||
import { Context } from 'aws-lambda'; | ||
import { Events } from '@aws-lambda-powertools/commons'; | ||
import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer'; | ||
|
||
// Set environment variable to disable capture response | ||
process.env.POWERTOOLS_TRACER_ERROR_RESPONSE = "false"; | ||
const tracer = new Tracer({ serviceName: 'tracerCaptureResponseDisabledFn' }); | ||
|
||
// In this example we are using the middleware pattern but you could use also the captureLambdaHandler decorator | ||
export const handler = middy(async (event: typeof Events.Custom.CustomEvent, context: Context) => { | ||
tracer.putAnnotation('awsRequestId', context.awsRequestId); | ||
tracer.putMetadata('eventPayload', event); | ||
|
||
let res; | ||
try { | ||
res = { foo: 'bar' }; | ||
|
||
// We are throwing an error only for testing purposes to make sure the error response is not captured in the subsegment metadata | ||
throw new Error('An error occurred.'); | ||
} catch (err) { | ||
// The error won't be in the subsegment metadata | ||
throw err; | ||
} | ||
|
||
return res; | ||
}).use(captureLambdaHandler(tracer)); |
24 changes: 24 additions & 0 deletions
24
examples/cdk/lib/example-function.Tracer.CaptureResponseDisabled.ts
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,24 @@ | ||
import middy from '@middy/core' | ||
import { Context } from 'aws-lambda'; | ||
import { Events } from '@aws-lambda-powertools/commons'; | ||
import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer'; | ||
|
||
// Set environment variable to disable capture response | ||
process.env.POWERTOOLS_TRACER_CAPTURE_RESPONSE = "false"; | ||
const tracer = new Tracer({ serviceName: 'tracerCaptureResponseDisabledFn' }); | ||
|
||
// In this example we are using the middleware pattern but you could use also the captureLambdaHandler decorator | ||
export const handler = middy(async (event: typeof Events.Custom.CustomEvent, context: Context) => { | ||
tracer.putAnnotation('awsRequestId', context.awsRequestId); | ||
tracer.putMetadata('eventPayload', event); | ||
|
||
let res; | ||
try { | ||
res = { foo: 'bar' }; | ||
} catch (err) { | ||
throw err; | ||
} | ||
|
||
// The response won't be captured in the subsegment metadata | ||
return res; | ||
}).use(captureLambdaHandler(tracer)); |
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,34 @@ | ||
import { Callback, Context } from 'aws-lambda'; | ||
import { Events } from '@aws-lambda-powertools/commons'; | ||
import { Tracer } from '@aws-lambda-powertools/tracer'; | ||
|
||
// process.env.POWERTOOLS_SERVICE_NAME = 'tracerManualFn'; // Alternative to setting the service name in the constructor | ||
const tracer = new Tracer({ serviceName: 'tracerDecoratorFn' }); | ||
|
||
export class MyFunctionWithDecorator { | ||
// We instrument the handler with the decorator and the tracer will automatically create a subsegment and capture relevant annotations and metadata | ||
@tracer.captureLambdaHandler() | ||
public handler(event: typeof Events.Custom.CustomEvent, context: Context, _callback: Callback<unknown>): void | Promise<unknown> { | ||
// Add custom annotation & metadata | ||
tracer.putAnnotation('awsRequestId', context.awsRequestId); | ||
tracer.putMetadata('eventPayload', event); | ||
|
||
let res: { foo: string }; | ||
try { | ||
res = { foo: this.myMethod() }; | ||
} catch (err) { | ||
throw err; | ||
} | ||
|
||
return new Promise((resolve, _reject) => resolve(res as unknown)); | ||
} | ||
|
||
// We can also use decorators to create a subsegment for the method & capture response and errors as metadata | ||
@tracer.captureMethod() | ||
public myMethod(): string { | ||
return 'bar'; | ||
} | ||
} | ||
|
||
export const handlerClass = new MyFunctionWithDecorator(); | ||
export const handler = handlerClass.handler; |
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,23 @@ | ||
import middy from '@middy/core'; | ||
import { Context } from 'aws-lambda'; | ||
import { Events } from '@aws-lambda-powertools/commons'; | ||
import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer'; | ||
|
||
// process.env.POWERTOOLS_TRACE_ENABLED = 'false'; // Alternative to disabling tracing in the constructor | ||
const tracer = new Tracer({ serviceName: 'tracerDisabledFn', enabled: false }); | ||
|
||
// In this example we are using the middleware pattern but you could use also the captureLambdaHandler decorator or the manual mode | ||
export const handler = middy(async (event: typeof Events.Custom.CustomEvent, context: Context) => { | ||
// No tracing will be done and the commands will be ignored, this is useful for testing | ||
tracer.putAnnotation('awsRequestId', context.awsRequestId); | ||
tracer.putMetadata('eventPayload', event); | ||
|
||
let res; | ||
try { | ||
res = { foo: 'bar' }; | ||
} catch (err) { | ||
throw err; | ||
} | ||
|
||
return res; | ||
}).use(captureLambdaHandler(tracer)); |
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,39 @@ | ||
import { Context } from 'aws-lambda'; | ||
import { Events } from '@aws-lambda-powertools/commons'; | ||
import { Tracer } from '@aws-lambda-powertools/tracer'; | ||
|
||
// process.env.POWERTOOLS_SERVICE_NAME = 'tracerManualFn'; // Alternative to setting the service name in the constructor | ||
const tracer = new Tracer({ serviceName: 'tracerManualFn' }); | ||
|
||
export const handler = async (event: typeof Events.Custom.CustomEvent, context: Context): Promise<unknown> => { | ||
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda) | ||
// Create subsegment for the function & set it as active | ||
const subsegment = segment.addNewSubsegment(`## ${process.env._HANDLER}`); | ||
tracer.setSegment(subsegment); | ||
|
||
// Annotate the subsegment with the cold start & serviceName | ||
tracer.annotateColdStart(); | ||
tracer.addServiceNameAnnotation(); | ||
|
||
// Add custom annotation & metadata | ||
tracer.putAnnotation('awsRequestId', context.awsRequestId); | ||
tracer.putMetadata('eventPayload', event); | ||
|
||
let res; | ||
try { | ||
res = { foo: 'bar' }; | ||
// Add the response as metadata | ||
tracer.addResponseAsMetadata(res, process.env._HANDLER); | ||
} catch (err) { | ||
// Add the error as metadata | ||
tracer.addErrorAsMetadata(err as Error); | ||
throw err; | ||
} finally { | ||
// Close subsegment (the AWS Lambda one is closed automatically) | ||
subsegment.close(); | ||
// Set the facade segment as active again | ||
tracer.setSegment(segment); | ||
} | ||
|
||
return res; | ||
}; |
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,23 @@ | ||
import middy from '@middy/core'; | ||
import { Context } from 'aws-lambda'; | ||
import { Events } from '@aws-lambda-powertools/commons'; | ||
import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer'; | ||
|
||
// process.env.POWERTOOLS_SERVICE_NAME = 'tracerManualFn'; // Alternative to setting the service name in the constructor | ||
const tracer = new Tracer({ serviceName: 'tracerMiddlewareFn' }); | ||
|
||
// We instrument the handler with the middy middleware and the tracer will automatically create a subsegment and capture relevant annotations and metadata | ||
export const handler = middy(async (event: typeof Events.Custom.CustomEvent, context: Context) => { | ||
// Add custom annotation & metadata | ||
tracer.putAnnotation('awsRequestId', context.awsRequestId); | ||
tracer.putMetadata('eventPayload', event); | ||
|
||
let res; | ||
try { | ||
res = { foo: 'bar' }; | ||
} catch (err) { | ||
throw err; | ||
} | ||
|
||
return res; | ||
}).use(captureLambdaHandler(tracer)); |
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.
So this bit has been changed in main, getting started section + settings. How will it look like once you merge?
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.
I will resolve the conflict & accept the version on
main
since I made this change several days ago before your PR was merged.