-
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
Changes from 17 commits
e0e6e9e
d7381b3
7ffa71e
98b702f
5e442b0
630b56d
7d633cf
dadf44e
45f7b60
ac8e578
682d62c
9a4fc51
196db23
640a431
5349347
7caeb28
1320cec
92e4ee3
93c19e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: what about provisioning env variables in CDK? I think this is what developers would do in prod There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW happy to address this as part of the prod release milestone, in case you also agree There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const tracer = new Tracer({ serviceName: 'tracerCaptureErrorDisabledFn' }); | ||
|
||
// 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)); |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 7caeb28, see this comment. |
||
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)); |
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; |
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)); |
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; | ||
}; |
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)); |
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.