-
Notifications
You must be signed in to change notification settings - Fork 155
chore(cicd): cdk examples and e2e tests for metrics #326
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*.js | ||
!jest.config.js | ||
*.d.ts | ||
node_modules | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
cdk.out |
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,6 @@ | ||
*.ts | ||
!*.d.ts | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
cdk.out |
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,14 @@ | ||
# Welcome to your CDK TypeScript project! | ||
|
||
This is a blank project for TypeScript development with CDK. | ||
|
||
The `cdk.json` file tells the CDK Toolkit how to execute your app. | ||
|
||
## Useful commands | ||
|
||
* `npm run build` compile typescript to js | ||
* `npm run watch` watch for changes and compile | ||
* `npm run test` perform the jest unit tests | ||
* `cdk deploy` deploy this stack to your default AWS account/region | ||
* `cdk diff` compare deployed stack with current state | ||
* `cdk synth` emits the synthesized CloudFormation template |
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,21 @@ | ||
#!/usr/bin/env node | ||
import 'source-map-support/register'; | ||
import * as cdk from 'aws-cdk-lib'; | ||
import { CdkAppStack } from '../lib/example-stack'; | ||
|
||
const app = new cdk.App(); | ||
new CdkAppStack(app, 'CdkAppStack', { | ||
/* If you don't specify 'env', this stack will be environment-agnostic. | ||
* Account/Region-dependent features and context lookups will not work, | ||
* but a single synthesized template can be deployed anywhere. */ | ||
|
||
/* Uncomment the next line to specialize this stack for the AWS Account | ||
* and Region that are implied by the current CLI configuration. */ | ||
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }, | ||
|
||
/* Uncomment the next line if you know exactly what Account and Region you | ||
* want to deploy the stack to. */ | ||
// env: { account: '123456789012', region: 'us-east-1' }, | ||
|
||
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */ | ||
}); |
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,26 @@ | ||
{ | ||
"app": "npx ts-node --prefer-ts-exts bin/cdk-app.ts", | ||
"watch": { | ||
"include": [ | ||
"**" | ||
], | ||
"exclude": [ | ||
"README.md", | ||
"cdk*.json", | ||
"**/*.d.ts", | ||
"**/*.js", | ||
"tsconfig.json", | ||
"package*.json", | ||
"yarn.lock", | ||
"node_modules", | ||
"test" | ||
] | ||
}, | ||
"context": { | ||
"@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true, | ||
"@aws-cdk/core:stackRelativeExports": true, | ||
"@aws-cdk/aws-rds:lowercaseDbIdentifier": true, | ||
"@aws-cdk/aws-lambda:recognizeVersionProps": true, | ||
"@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true | ||
} | ||
} |
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,8 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
roots: ['<rootDir>/test'], | ||
testMatch: ['**/*.test.ts'], | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest' | ||
} | ||
}; |
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,61 @@ | ||
import { Context } from 'aws-lambda'; | ||
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
import { Tracer } from '@aws-lambda-powertools/tracer'; | ||
|
||
const namespace = 'CDKExample'; | ||
const serviceName = 'MyFunctionWithStandardHandler'; | ||
|
||
const metrics = new Metrics({ namespace: namespace, service: serviceName }); | ||
const logger = new Logger({ logLevel: 'INFO', serviceName: serviceName }); | ||
const tracer = new Tracer({ serviceName: serviceName }); | ||
|
||
export const handler = async (_event: unknown, context: Context): Promise<unknown> => { | ||
// Since we are in manual mode we need to create the handler segment (the 4 lines below would be done for you by decorator/middleware) | ||
// we do it at the beginning because we want to trace the whole duration of the handler | ||
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by Lambda & that can't be manipulated) | ||
const handlerSegment = segment.addNewSubsegment(`## ${context.functionName}`); | ||
// TODO: expose tracer.annotateColdStart() | ||
tracer.putAnnotation('ColdStart', Tracer.coldStart); | ||
|
||
// ### Experiment logger | ||
logger.addPersistentLogAttributes({ | ||
testKey: 'testValue', | ||
}); | ||
logger.debug('This is an DEBUG log'); // Won't show by default | ||
logger.info('This is an INFO log'); | ||
logger.warn('This is an WARN log'); | ||
logger.error('This is an ERROR log'); | ||
|
||
// ### Experiment metrics | ||
metrics.captureColdStartMetric(); | ||
metrics.raiseOnEmptyMetrics(); | ||
metrics.setDefaultDimensions({ environment: 'example', type: 'standardFunction' }); | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
|
||
const metricWithItsOwnDimensions = metrics.singleMetric(); | ||
metricWithItsOwnDimensions.addDimension('InnerDimension', 'true'); | ||
metricWithItsOwnDimensions.addMetric('single-metric', MetricUnits.Percent, 50); | ||
|
||
metrics.purgeStoredMetrics(); | ||
metrics.raiseOnEmptyMetrics(); | ||
|
||
// ### Experiment tracer | ||
|
||
tracer.putAnnotation('Myannotation', 'My annotation\'s value'); | ||
|
||
// Create subsegment & set it as active | ||
const subsegment = handlerSegment.addNewSubsegment('MySubSegment'); | ||
|
||
try { | ||
throw new Error('test'); | ||
// Add the response as metadata | ||
} catch (err) { | ||
// Add the error as metadata | ||
subsegment.addError(err as Error, false); | ||
} | ||
|
||
// Close subsegment | ||
subsegment.close(); | ||
handlerSegment.close(); | ||
}; |
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,63 @@ | ||
import { Tracer } from '@aws-lambda-powertools/tracer'; | ||
import { Callback, Context } from 'aws-lambda'; | ||
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; | ||
import { Logger } from '@aws-lambda-powertools/logger'; | ||
|
||
const namespace = 'CDKExample'; | ||
const serviceName = 'MyFunctionWithDecorator'; | ||
|
||
const metrics = new Metrics({ namespace: namespace, service: serviceName }); | ||
const logger = new Logger({ logLevel: 'INFO', serviceName: serviceName }); | ||
const tracer = new Tracer({ serviceName: serviceName }); | ||
|
||
export class MyFunctionWithDecorator { | ||
@tracer.captureLambdaHanlder() | ||
@logger.injectLambdaContext() | ||
@metrics.logMetrics({ | ||
captureColdStartMetric: true, | ||
raiseOnEmptyMetrics: true, | ||
defaultDimensions: { environment: 'example', type: 'withDecorator' }, | ||
}) | ||
public handler(_event: unknown, _context: Context, _callback: Callback<unknown>): void | Promise<unknown> { | ||
// ### Experiment logger | ||
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. Why is there a word "Experiment"? |
||
logger.addPersistentLogAttributes({ | ||
testKey: 'testValue', | ||
}); | ||
logger.debug('This is an DEBUG log'); // Won't show by default | ||
logger.info('This is an INFO log'); | ||
logger.warn('This is an WARN log'); | ||
logger.error('This is an ERROR log'); | ||
|
||
// ### Experiment metrics | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
|
||
const metricWithItsOwnDimensions = metrics.singleMetric(); | ||
metricWithItsOwnDimensions.addDimension('InnerDimension', 'true'); | ||
metricWithItsOwnDimensions.addMetric('single-metric', MetricUnits.Percent, 50); | ||
|
||
// ### Experiment tracer | ||
tracer.putAnnotation('Myannotation', 'My annotation\'s value'); | ||
|
||
// Create subsegment & set it as active | ||
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by Lambda & that can't be manipulated) | ||
const subsegment = segment.addNewSubsegment('MySubSegment'); | ||
|
||
tracer.setSegment(subsegment); | ||
// TODO: Add the ColdStart annotation !!! NOT POSSIBLE | ||
// tracer.putAnnotation('ColdStart', tracer); | ||
|
||
try { | ||
throw new Error('test'); | ||
// Add the response as metadata | ||
} catch (err) { | ||
// Add the error as metadata | ||
subsegment.addError(err as Error, false); | ||
} | ||
|
||
// Close subsegment | ||
subsegment.close(); | ||
} | ||
} | ||
|
||
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,43 @@ | ||
import middy from '@middy/core' | ||
import { Callback, Context } from 'aws-lambda'; | ||
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; | ||
|
||
const metrics = new Metrics({ namespace: 'CDKExample', service: 'withMiddy' }); // Sets metric namespace, and service as a metric dimension | ||
|
||
type CustomEvent = { | ||
throw: boolean | ||
}; | ||
|
||
class MyFunctionWithDecorator { | ||
|
||
@metrics.logMetrics({ captureColdStartMetric: true }) | ||
public handler(_event: CustomEvent, _context: Context, _callback: Callback<unknown>): void | Promise<unknown> { | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
if (_event.throw) { | ||
throw new Error('Test error'); | ||
} | ||
} | ||
} | ||
|
||
const handler = middy(async (_event, _context) => { | ||
|
||
const handlerClass = new MyFunctionWithDecorator(); | ||
|
||
return handlerClass.handler(_event, _context, () => console.log('Lambda invoked!')); | ||
}); | ||
|
||
handler.before(async (_request) => { | ||
metrics.addMetric('beforeHandlerCalled', MetricUnits.Count, 1); | ||
}); | ||
|
||
handler.after(async (_request) => { | ||
// Won't be flushed since happens after | ||
metrics.addMetric('afterHandlerCalled', MetricUnits.Count, 1); | ||
|
||
}); | ||
|
||
handler.onError(async (_request) => { | ||
metrics.addMetric('onErrorHandlerCalled', MetricUnits.Count, 1); | ||
}); | ||
|
||
module.exports = { 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,86 @@ | ||
import { Stack, StackProps, custom_resources, aws_iam } from 'aws-cdk-lib'; | ||
import { Events } from '@aws-lambda-powertools/commons'; | ||
import { Construct } from 'constructs'; | ||
import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs'; | ||
import { Tracing } from 'aws-cdk-lib/aws-lambda'; | ||
|
||
export class CdkAppStack extends Stack { | ||
constructor(scope: Construct, id: string, props?: StackProps) { | ||
super(scope, id, props); | ||
|
||
const myFunctionWithStandardFunctions = new lambda.NodejsFunction(this, 'MyFunction', { tracing: Tracing.ACTIVE }); | ||
const myFunctionWithDecorator = new lambda.NodejsFunction(this, 'MyFunctionWithDecorator', { | ||
tracing: Tracing.ACTIVE, | ||
}); | ||
const myFunctionWithWithMiddleware = new lambda.NodejsFunction(this, 'MyFunctionWithMiddy', { | ||
tracing: Tracing.ACTIVE, | ||
}); | ||
|
||
// Invoke all functions twice | ||
for (let i = 0; i < 2; i++) { | ||
new custom_resources.AwsCustomResource(this, `Invoke-std-func-${i}`, { | ||
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. Should we wrap these 3 resource creations in to a function? They are almost the same except the passed function. |
||
onUpdate: { | ||
service: 'Lambda', | ||
action: 'invoke', | ||
physicalResourceId: custom_resources.PhysicalResourceId.of(new Date().toISOString()), | ||
parameters: { | ||
FunctionName: myFunctionWithStandardFunctions.functionName, | ||
InvocationType: 'RequestResponse', | ||
Payload: JSON.stringify(Events.Custom.CustomEvent), | ||
} | ||
}, | ||
policy: custom_resources.AwsCustomResourcePolicy.fromStatements([ | ||
new aws_iam.PolicyStatement({ | ||
effect: aws_iam.Effect.ALLOW, | ||
resources: [ | ||
myFunctionWithStandardFunctions.functionArn, | ||
], | ||
actions: ['lambda:InvokeFunction'], | ||
}), | ||
]), | ||
}); | ||
new custom_resources.AwsCustomResource(this, `Invoke-dec-func-${i}`, { | ||
onUpdate: { | ||
service: 'Lambda', | ||
action: 'invoke', | ||
physicalResourceId: custom_resources.PhysicalResourceId.of(new Date().toISOString()), | ||
parameters: { | ||
FunctionName: myFunctionWithDecorator.functionName, | ||
InvocationType: 'RequestResponse', | ||
Payload: JSON.stringify(Events.Custom.CustomEvent), | ||
} | ||
}, | ||
policy: custom_resources.AwsCustomResourcePolicy.fromStatements([ | ||
new aws_iam.PolicyStatement({ | ||
effect: aws_iam.Effect.ALLOW, | ||
resources: [ | ||
myFunctionWithDecorator.functionArn, | ||
], | ||
actions: ['lambda:InvokeFunction'], | ||
}), | ||
]), | ||
}); | ||
new custom_resources.AwsCustomResource(this, `Invoke-middy-func-${i}`, { | ||
onUpdate: { | ||
service: 'Lambda', | ||
action: 'invoke', | ||
physicalResourceId: custom_resources.PhysicalResourceId.of(new Date().toISOString()), | ||
parameters: { | ||
FunctionName: myFunctionWithWithMiddleware.functionName, | ||
InvocationType: 'RequestResponse', | ||
Payload: JSON.stringify(Events.Custom.CustomEvent), | ||
} | ||
}, | ||
policy: custom_resources.AwsCustomResourcePolicy.fromStatements([ | ||
new aws_iam.PolicyStatement({ | ||
effect: aws_iam.Effect.ALLOW, | ||
resources: [ | ||
myFunctionWithWithMiddleware.functionArn, | ||
], | ||
actions: ['lambda:InvokeFunction'], | ||
}), | ||
]), | ||
}); | ||
} | ||
} | ||
} |
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.
Can we remove this?