Skip to content

Commit dadf44e

Browse files
committed
Updated examples + added unit test groups
1 parent 7d633cf commit dadf44e

16 files changed

+268
-966
lines changed

examples/cdk/lib/example-function.MyFunctionWithMiddy.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import middy from '@middy/core'
1+
import middy from '@middy/core';
22
import { Callback, Context } from 'aws-lambda';
33
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
44

examples/cdk/lib/example-function.Tracer.Decorator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const tracer = new Tracer({ serviceName: 'tracerDecoratorFn' });
77

88
export class MyFunctionWithDecorator {
99
// We instrument the handler with the decorator and the tracer will automatically create a subsegment and capture relevant annotations and metadata
10-
@tracer.captureLambdaHanlder()
10+
@tracer.captureLambdaHandler()
1111
public handler(event: typeof Events.Custom.CustomEvent, context: Context, _callback: Callback<unknown>): void | Promise<unknown> {
1212
// Add custom annotation & metadata
1313
tracer.putAnnotation('awsRequestId', context.awsRequestId);

examples/cdk/lib/example-function.Tracer.Manual.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Tracer } from '@aws-lambda-powertools/tracer';
55
// process.env.POWERTOOLS_SERVICE_NAME = 'tracerManualFn'; // Alternative to setting the service name in the constructor
66
const tracer = new Tracer({ serviceName: 'tracerManualFn' });
77

8-
export const handler = async (event: typeof Events.Custom.CustomEvent, context: Context) => {
8+
export const handler = async (event: typeof Events.Custom.CustomEvent, context: Context): Promise<unknown> => {
99
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
1010
// Create subsegment for the function & set it as active
1111
const subsegment = segment.addNewSubsegment(`## ${process.env._HANDLER}`);
@@ -21,19 +21,19 @@ export const handler = async (event: typeof Events.Custom.CustomEvent, context:
2121

2222
let res;
2323
try {
24-
res = { foo: 'bar' };
25-
// Add the response as metadata
26-
tracer.addResponseAsMetadata(res, process.env._HANDLER);
24+
res = { foo: 'bar' };
25+
// Add the response as metadata
26+
tracer.addResponseAsMetadata(res, process.env._HANDLER);
2727
} catch (err) {
28-
// Add the error as metadata
29-
tracer.addErrorAsMetadata(err as Error);
30-
throw err;
28+
// Add the error as metadata
29+
tracer.addErrorAsMetadata(err as Error);
30+
throw err;
31+
} finally {
32+
// Close subsegment (the AWS Lambda one is closed automatically)
33+
subsegment.close();
34+
// Set the facade segment as active again
35+
tracer.setSegment(segment);
3136
}
3237

33-
// Close subsegment (the AWS Lambda one is closed automatically)
34-
subsegment.close();
35-
// Set the facade segment as active again
36-
tracer.setSegment(segment);
37-
3838
return res;
3939
};

examples/cdk/lib/example-function.Tracer.Middleware.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import middy from '@middy/core'
1+
import middy from '@middy/core';
22
import { Context } from 'aws-lambda';
33
import { Events } from '@aws-lambda-powertools/commons';
44
import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer';

examples/cdk/lib/example-function.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { NodejsFunction, NodejsFunctionProps } from 'aws-cdk-lib/aws-lambda-node
55
import { Tracing } from 'aws-cdk-lib/aws-lambda';
66

77
interface ExampleFunctionProps {
8-
readonly functionName: string;
9-
readonly tracingActive?: boolean;
10-
readonly invocations?: number;
11-
readonly fnProps?: Partial<NodejsFunctionProps>;
8+
readonly functionName: string
9+
readonly tracingActive?: boolean
10+
readonly invocations?: number
11+
readonly fnProps?: Partial<NodejsFunctionProps>
1212
}
1313

1414
class ExampleFunction extends Construct {
15-
function: NodejsFunction;
15+
public function: NodejsFunction;
1616

17-
constructor(scope: Construct, id: string, props: ExampleFunctionProps) {
17+
public constructor(scope: Construct, id: string, props: ExampleFunctionProps) {
1818
super(scope, id);
1919

2020
const { functionName, tracingActive, invocations, fnProps } = Object.assign({
@@ -54,5 +54,5 @@ class ExampleFunction extends Construct {
5454
}
5555

5656
export {
57-
ExampleFunction
57+
ExampleFunction
5858
};

examples/cdk/lib/example-stack.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Construct } from 'constructs';
33
import { ExampleFunction } from './example-function';
44

55
export class CdkAppStack extends Stack {
6-
constructor(scope: Construct, id: string, props?: StackProps) {
6+
public constructor(scope: Construct, id: string, props?: StackProps) {
77
super(scope, id, props);
88

99
new ExampleFunction(this, 'MyFunction', {
@@ -22,7 +22,7 @@ export class CdkAppStack extends Stack {
2222
});
2323

2424
new ExampleFunction(this, 'Tracer.Disabled', {
25-
functionName: 'Tracer.CaptureDisabled',
25+
functionName: 'Tracer.Disabled',
2626
tracingActive: true,
2727
});
2828

packages/tracing/jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
name: 'AWS Lambda Powertools utility: TRACER',
44
color: 'cyan',
55
},
6+
'runner': 'groups',
67
'preset': 'ts-jest',
78
'transform': {
89
'^.+\\.ts?$': 'ts-jest',
@@ -23,7 +24,6 @@ module.exports = {
2324
'testEnvironment': 'node',
2425
'coveragePathIgnorePatterns': [
2526
'/node_modules/',
26-
'/types/',
2727
],
2828
'coverageThreshold': {
2929
'global': {

0 commit comments

Comments
 (0)