Skip to content

Commit 593443a

Browse files
committed
Fixed tracing examples
1 parent e72dfd6 commit 593443a

4 files changed

+38
-29
lines changed

examples/cdk/lib/example-stack.MyFunction.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import {Metrics, MetricUnits} from '@aws-lambda-powertools/metrics';
2-
import {Logger} from '@aws-lambda-powertools/logger';
3-
import {Tracer} from '@aws-lambda-powertools/tracer';
4-
import { Subsegment } from 'aws-xray-sdk-core';
5-
6-
1+
import { Context } from 'aws-lambda';
2+
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
3+
import { Logger } from '@aws-lambda-powertools/logger';
4+
import { Tracer } from '@aws-lambda-powertools/tracer';
75

86
const namespace = 'CDKExample';
97
const serviceName = 'MyFunctionWithStandardHandler';
@@ -12,7 +10,14 @@ const metrics = new Metrics({ namespace: namespace, service: serviceName });
1210
const logger = new Logger({ logLevel: 'INFO', serviceName: serviceName });
1311
const tracer = new Tracer({ serviceName: serviceName });
1412

15-
export const handler = async (event: any, context: any) => {
13+
export const handler = async (_event: unknown, context: Context): Promise<unknown> => {
14+
// 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)
15+
// we do it at the beginning because we want to trace the whole duration of the handler
16+
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by Lambda & that can't be manipulated)
17+
const handlerSegment = segment.addNewSubsegment(`## ${context.functionName}`);
18+
// TODO: expose tracer.annotateColdStart()
19+
tracer.putAnnotation('ColdStart', Tracer.coldStart);
20+
1621
// ### Experiment logger
1722
logger.addPersistentLogAttributes({
1823
testKey: 'testValue',
@@ -36,13 +41,11 @@ export const handler = async (event: any, context: any) => {
3641
metrics.raiseOnEmptyMetrics();
3742

3843
// ### Experiment tracer
44+
3945
tracer.putAnnotation('Myannotation', 'My annotation\'s value');
4046

4147
// Create subsegment & set it as active
42-
const subsegment = new Subsegment('MySubSegment');
43-
tracer.setSegment(subsegment);
44-
// TODO: Add the ColdStart annotation !!! NOT POSSIBLE
45-
// tracer.putAnnotation('ColdStart', tracer);
48+
const subsegment = handlerSegment.addNewSubsegment('MySubSegment');
4649

4750
try {
4851
throw new Error('test');
@@ -54,4 +57,5 @@ export const handler = async (event: any, context: any) => {
5457

5558
// Close subsegment
5659
subsegment.close();
60+
handlerSegment.close();
5761
};

examples/cdk/lib/example-stack.MyFunctionWithDecorator.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
2-
import { Logger, createLogger } from '@aws-lambda-powertools/logger';
31
import { Tracer } from '@aws-lambda-powertools/tracer';
4-
import { Subsegment } from 'aws-xray-sdk-core';
52
import { Callback, Context } from 'aws-lambda';
3+
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
4+
import { Logger } from '@aws-lambda-powertools/logger';
65

76
const namespace = 'CDKExample';
87
const serviceName = 'MyFunctionWithDecorator';
@@ -19,7 +18,7 @@ export class MyFunctionWithDecorator {
1918
raiseOnEmptyMetrics: true,
2019
defaultDimensions: { environment: 'example', type: 'withDecorator' },
2120
})
22-
public handler(_event: any, _context: Context, _callback: Callback<any>): void | Promise<any> {
21+
public handler(_event: unknown, _context: Context, _callback: Callback<unknown>): void | Promise<unknown> {
2322
// ### Experiment logger
2423
logger.addPersistentLogAttributes({
2524
testKey: 'testValue',
@@ -40,7 +39,9 @@ export class MyFunctionWithDecorator {
4039
tracer.putAnnotation('Myannotation', 'My annotation\'s value');
4140

4241
// Create subsegment & set it as active
43-
const subsegment = new Subsegment('MySubSegment');
42+
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by Lambda & that can't be manipulated)
43+
const subsegment = segment.addNewSubsegment('MySubSegment');
44+
4445
tracer.setSegment(subsegment);
4546
// TODO: Add the ColdStart annotation !!! NOT POSSIBLE
4647
// tracer.putAnnotation('ColdStart', tracer);
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
import middy from '@middy/core'
2-
import {Metrics, MetricUnits} from '@aws-lambda-powertools/metrics';
32
import { Callback, Context } from 'aws-lambda';
3+
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
44

5+
const metrics = new Metrics({ namespace: 'CDKExample', service: 'withMiddy' }); // Sets metric namespace, and service as a metric dimension
56

6-
const metrics = new Metrics({namespace:"CDKExample", service:"withMiddy"}); // Sets metric namespace, and service as a metric dimension
7+
type CustomEvent = {
8+
throw: boolean
9+
};
710

811
class MyFunctionWithDecorator {
912

10-
@metrics.logMetrics({captureColdStartMetric: true})
11-
public handler(_event: any, _context: Context, _callback: Callback<any>): void | Promise<any> {
13+
@metrics.logMetrics({ captureColdStartMetric: true })
14+
public handler(_event: CustomEvent, _context: Context, _callback: Callback<unknown>): void | Promise<unknown> {
1215
metrics.addMetric('test-metric', MetricUnits.Count, 10);
13-
if(_event.throw) {
16+
if (_event.throw) {
1417
throw new Error('Test error');
1518
}
1619
}
1720
}
1821

1922
const handler = middy(async (_event, _context) => {
2023

21-
const handlerClass = new MyFunctionWithDecorator()
22-
return handlerClass.handler(_event, _context, () => {});
23-
})
24+
const handlerClass = new MyFunctionWithDecorator();
25+
26+
return handlerClass.handler(_event, _context, () => console.log('Lambda invoked!'));
27+
});
2428

2529
handler.before(async (_request) => {
2630
metrics.addMetric('beforeHandlerCalled', MetricUnits.Count, 1);
27-
})
31+
});
2832

2933
handler.after(async (_request) => {
3034
// Won't be flushed since happens after
3135
metrics.addMetric('afterHandlerCalled', MetricUnits.Count, 1);
3236

33-
})
37+
});
3438

3539
handler.onError(async (_request) => {
3640
metrics.addMetric('onErrorHandlerCalled', MetricUnits.Count, 1);
37-
})
41+
});
3842

39-
module.exports = { handler }
43+
module.exports = { handler };

examples/cdk/lib/example-stack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Stack, StackProps, custom_resources, aws_iam } from 'aws-cdk-lib';
2-
import {Events} from '@aws-lambda-powertools/commons';
2+
import { Events } from '@aws-lambda-powertools/commons';
33
import { Construct } from 'constructs';
44
import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs';
55
import { Tracing } from 'aws-cdk-lib/aws-lambda';

0 commit comments

Comments
 (0)