-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: updated CDK examples to remove old references & improve comments (…
…#439) * fix: updated CDK examples to correct errors & improved comments/structure * fix: updated comment * fix: Update examples/cdk/lib/example-function.Tracer.CaptureErrorDisabled.ts Co-authored-by: ijemmy <ijemmy@users.noreply.github.com> * fix: Update examples/cdk/lib/example-function.MyFunctionWithMiddy.ts Co-authored-by: ijemmy <ijemmy@users.noreply.github.com> * fix: added missing throw error statement Co-authored-by: ijemmy <ijemmy@users.noreply.github.com>
- Loading branch information
1 parent
cf43568
commit 4cdaaea
Showing
12 changed files
with
189 additions
and
110 deletions.
There are no files selected for viewing
This file contains 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 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 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,43 +1,73 @@ | ||
import middy from '@middy/core'; | ||
import { Callback, Context } from 'aws-lambda'; | ||
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; | ||
import { Context } from 'aws-lambda'; | ||
import { Events } from '@aws-lambda-powertools/commons'; | ||
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics'; | ||
import { Tracer, captureLambdaHandler } from '@aws-lambda-powertools/tracer'; | ||
import { Logger, injectLambdaContext } from '@aws-lambda-powertools/logger'; | ||
|
||
const metrics = new Metrics({ namespace: 'CDKExample', service: 'withMiddy' }); // Sets metric namespace, and service as a metric dimension | ||
const namespace = 'CDKExample'; | ||
const serviceName = 'MyFunctionWithMiddyMiddleware'; | ||
|
||
type CustomEvent = { | ||
throw: boolean | ||
}; | ||
const metrics = new Metrics({ namespace: namespace, serviceName: serviceName }); | ||
const logger = new Logger({ logLevel: 'INFO', serviceName: serviceName }); | ||
const tracer = new Tracer({ serviceName: serviceName }); | ||
|
||
class MyFunctionWithDecorator { | ||
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context) => { | ||
// ### Experiment with Logger | ||
// AWS Lambda context is automatically injected by the middleware | ||
|
||
@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'); | ||
} | ||
} | ||
} | ||
logger.addPersistentLogAttributes({ | ||
testKey: 'testValue', | ||
}); | ||
logger.debug('This is an DEBUG log'); // Won't show because we pass logLevel: 'INFO' in the constructor. | ||
logger.info('This is an INFO log'); | ||
logger.warn('This is an WARN log'); | ||
logger.error('This is an ERROR log'); | ||
|
||
const handler = middy(async (_event, _context) => { | ||
// ### Experiment with Metrics | ||
// Default metrics, cold start, and throwOnEmptyMetrics are enabled by the middleware | ||
|
||
const handlerClass = new MyFunctionWithDecorator(); | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
|
||
return handlerClass.handler(_event, _context, () => console.log('Lambda invoked!')); | ||
}); | ||
const metricWithItsOwnDimensions = metrics.singleMetric(); | ||
metricWithItsOwnDimensions.addDimension('InnerDimension', 'true'); | ||
metricWithItsOwnDimensions.addMetric('single-metric', MetricUnits.Percent, 50); | ||
|
||
// ### Experiment with Tracer | ||
|
||
handler.before(async (_request) => { | ||
metrics.addMetric('beforeHandlerCalled', MetricUnits.Count, 1); | ||
}); | ||
// Service & Cold Start annotations will be added for you by the decorator/middleware | ||
|
||
handler.after(async (_request) => { | ||
// Won't be flushed since happens after | ||
metrics.addMetric('afterHandlerCalled', MetricUnits.Count, 1); | ||
// These traces will be added to the main segment (## index.handler) | ||
tracer.putAnnotation('awsRequestId', context.awsRequestId); | ||
tracer.putMetadata('eventPayload', event); | ||
|
||
}); | ||
// Create another subsegment & set it as active | ||
const handlerSegment = tracer.getSegment(); // This is the custom segment created by Tracer for you (## index.handler) | ||
const subsegment = handlerSegment.addNewSubsegment('### MySubSegment'); | ||
tracer.setSegment(subsegment); | ||
|
||
handler.onError(async (_request) => { | ||
metrics.addMetric('onErrorHandlerCalled', MetricUnits.Count, 1); | ||
}); | ||
let res; | ||
try { | ||
res = { foo: 'bar' }; | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
// Close the subsegment you created (### MySubSegment) | ||
subsegment.close(); | ||
// Set back the original segment as active (## index.handler) | ||
tracer.setSegment(handlerSegment); | ||
// The main segment (facade) will be closed for you at the end by the decorator/middleware | ||
} | ||
|
||
return res; | ||
} | ||
|
||
module.exports = { handler }; | ||
// We instrument the handler with the various Middy middlewares | ||
export const handler = middy(lambdaHandler) | ||
.use(captureLambdaHandler(tracer)) | ||
.use(logMetrics(metrics, { | ||
captureColdStartMetric: true, | ||
throwOnEmptyMetrics: true, | ||
defaultDimensions: { environment: 'example', type: 'withDecorator' }, | ||
})) | ||
.use(injectLambdaContext(logger)); |
This file contains 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 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 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 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 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 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.