Skip to content

Commit 6838cc2

Browse files
committed
feat(tracer): add esmodule support (#1741)
* feat(tracer): add esmodule support * chore(docs): update imports
1 parent 4916ab6 commit 6838cc2

34 files changed

+251
-214
lines changed

Diff for: examples/cdk/functions/get-all-items.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
22
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
3-
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
3+
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
44
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
55
import middy from '@middy/core';
66
import {

Diff for: examples/sam/src/get-all-items.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { tableName } from './common/constants';
88
import { logger, tracer, metrics } from './common/powertools';
99
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
1010
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
11-
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
11+
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
1212
import { docClient } from './common/dynamodb-client';
1313
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
1414
import { getUuid } from './common/getUuid';

Diff for: packages/tracer/jest.config.js renamed to packages/tracer/jest.config.cjs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module.exports = {
55
},
66
runner: 'groups',
77
preset: 'ts-jest',
8+
moduleNameMapper: {
9+
'^(\\.{1,2}/.*)\\.js$': '$1',
10+
},
811
transform: {
912
'^.+\\.ts?$': 'ts-jest',
1013
},

Diff for: packages/tracer/package.json

+39-4
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
"test:e2e:nodejs20x": "RUNTIME=nodejs20x jest --group=e2e",
1818
"test:e2e": "jest --group=e2e",
1919
"watch": "jest --watch",
20-
"build": "tsc --build --force",
20+
"build:cjs": "tsc --build --force && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
21+
"build:esm": "tsc --project tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
22+
"build": "npm run build:esm & npm run build:cjs",
2123
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
2224
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
2325
"prebuild": "rimraf ./lib",
24-
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
26+
"prepack": "rimraf ./lib/*.tsbuildinfo && node ../../.github/scripts/release_patch_package_json.js ."
2527
},
2628
"lint-staged": {
2729
"*.{js,ts}": "npm run lint-fix"
2830
},
2931
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/tracer#readme",
3032
"license": "MIT-0",
31-
"main": "./lib/index.js",
32-
"types": "./lib/index.d.ts",
3333
"devDependencies": {
3434
"@aws-lambda-powertools/testing-utils": "file:../testing",
3535
"@aws-sdk/client-dynamodb": "^3.506.0",
@@ -47,6 +47,41 @@
4747
"optional": true
4848
}
4949
},
50+
"type": "module",
51+
"exports": {
52+
".": {
53+
"require": {
54+
"types": "./lib/cjs/index.d.ts",
55+
"default": "./lib/cjs/index.js"
56+
},
57+
"import": {
58+
"types": "./lib/esm/index.d.ts",
59+
"default": "./lib/esm/index.js"
60+
}
61+
},
62+
"./middleware": {
63+
"import": "./lib/esm/middleware/middy.js",
64+
"require": "./lib/cjs/middleware/middy.js"
65+
},
66+
"./types": {
67+
"import": "./lib/esm/types/index.js",
68+
"require": "./lib/cjs/types/index.js"
69+
}
70+
},
71+
"typesVersions": {
72+
"*": {
73+
"middleware": [
74+
"lib/cjs/middleware/middy.d.ts",
75+
"lib/esm/middleware/middy.d.ts"
76+
],
77+
"types": [
78+
"lib/cjs/types/index.d.ts",
79+
"lib/esm/types/index.d.ts"
80+
]
81+
}
82+
},
83+
"types": "./lib/cjs/index.d.ts",
84+
"main": "./lib/cjs/index.js",
5085
"files": [
5186
"lib"
5287
],

Diff for: packages/tracer/src/Tracer.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import { Utility } from '@aws-lambda-powertools/commons';
33
import type {
44
AsyncHandler,
55
SyncHandler,
6+
HandlerMethodDecorator,
67
} from '@aws-lambda-powertools/commons/types';
7-
import type { TracerInterface } from '.';
8-
import {
9-
type ConfigServiceInterface,
10-
EnvironmentVariablesService,
11-
} from './config';
8+
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
9+
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
1210
import type {
13-
HandlerMethodDecorator,
11+
TracerInterface,
1412
TracerOptions,
13+
AnyClass,
1514
MethodDecorator,
1615
CaptureLambdaHandlerOptions,
1716
CaptureMethodOptions,
18-
} from './types';
19-
import { ProviderService, type ProviderServiceInterface } from './provider';
17+
} from './types/Tracer.js';
18+
import { ProviderService } from './provider/ProviderService.js';
19+
import type { ProviderServiceInterface } from './types/ProviderServiceInterface.js';
2020
import { type Segment, Subsegment } from 'aws-xray-sdk-core';
2121

2222
/**
@@ -460,7 +460,9 @@ class Tracer extends Utility implements TracerInterface {
460460
* @decorator Class
461461
* @param options - (_optional_) Options for the decorator
462462
*/
463-
public captureMethod(options?: CaptureMethodOptions): MethodDecorator {
463+
public captureMethod<T extends AnyClass>(
464+
options?: CaptureMethodOptions
465+
): MethodDecorator<T> {
464466
return (_target, propertyKey, descriptor) => {
465467
// The descriptor.value is the method this decorator decorates, it cannot be undefined.
466468
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

Diff for: packages/tracer/src/TracerInterface.ts

-34
This file was deleted.

Diff for: packages/tracer/src/config/EnvironmentVariablesService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConfigServiceInterface } from './ConfigServiceInterface';
1+
import { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
22
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';
33

44
class EnvironmentVariablesService

Diff for: packages/tracer/src/config/index.ts

-2
This file was deleted.

Diff for: packages/tracer/src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export * from './Tracer';
2-
export * from './TracerInterface';
3-
export * from './middleware/middy';
1+
export { Tracer } from './Tracer.js';

Diff for: packages/tracer/src/middleware/middy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TRACER_KEY } from '@aws-lambda-powertools/commons';
2-
import type { Tracer } from '../Tracer';
2+
import type { Tracer } from '../Tracer.js';
33
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
4-
import type { CaptureLambdaHandlerOptions } from '../types';
4+
import type { CaptureLambdaHandlerOptions } from '../types/Tracer.js';
55
import type {
66
MiddlewareLikeObj,
77
MiddyLikeRequest,

Diff for: packages/tracer/src/provider/ProviderService.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { ContextMissingStrategy } from 'aws-xray-sdk-core/dist/lib/context_utils';
21
import { Namespace } from 'cls-hooked';
3-
import { ProviderServiceInterface } from '.';
2+
import type {
3+
ProviderServiceInterface,
4+
ContextMissingStrategy,
5+
} from '../types/ProviderServiceInterface.js';
46
import {
57
captureAWS,
68
captureAWSClient,
@@ -107,8 +109,8 @@ class ProviderService implements ProviderServiceInterface {
107109
segment.addMetadata(key, value, namespace);
108110
}
109111

110-
public setContextMissingStrategy(strategy: unknown): void {
111-
setContextMissingStrategy(strategy as ContextMissingStrategy);
112+
public setContextMissingStrategy(strategy: ContextMissingStrategy): void {
113+
setContextMissingStrategy(strategy);
112114
}
113115

114116
public setDaemonAddress(address: string): void {

Diff for: packages/tracer/src/provider/index.ts

-2
This file was deleted.

Diff for: packages/tracer/src/provider/ProviderServiceInterface.ts renamed to packages/tracer/src/types/ProviderServiceInterface.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { Namespace } from 'cls-hooked';
2-
import { Segment, Subsegment } from 'aws-xray-sdk-core';
1+
import type { Namespace } from 'cls-hooked';
2+
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
3+
4+
type ContextMissingStrategy =
5+
| 'LOG_ERROR'
6+
| 'RUNTIME_ERROR'
7+
| 'IGNORE_ERROR'
8+
| ((msg: string) => void);
39

410
interface ProviderServiceInterface {
511
getNamespace(): Namespace;
@@ -12,7 +18,7 @@ interface ProviderServiceInterface {
1218

1319
setDaemonAddress(address: string): void;
1420

15-
setContextMissingStrategy(strategy: unknown): void;
21+
setContextMissingStrategy(strategy: ContextMissingStrategy): void;
1622

1723
captureAWS<T>(awsservice: T): T;
1824

@@ -39,4 +45,4 @@ interface ProviderServiceInterface {
3945
putMetadata(key: string, value: unknown, namespace?: string): void;
4046
}
4147

42-
export { ProviderServiceInterface };
48+
export { ProviderServiceInterface, ContextMissingStrategy };

Diff for: packages/tracer/src/types/Tracer.ts

+40-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import type { ConfigServiceInterface } from '../config';
2-
import type { Handler } from 'aws-lambda';
3-
import type {
4-
AsyncHandler,
5-
LambdaInterface,
6-
SyncHandler,
7-
} from '@aws-lambda-powertools/commons/types';
1+
import type { ConfigServiceInterface } from './ConfigServiceInterface.js';
2+
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
3+
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
84

95
/**
106
* Options for the tracer class to be used during initialization.
@@ -100,28 +96,51 @@ type CaptureMethodOptions = {
10096
captureResponse?: boolean;
10197
};
10298

103-
type HandlerMethodDecorator = (
104-
target: LambdaInterface,
99+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
100+
type AnyClassMethod = (...args: any[]) => any;
101+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
102+
type AnyClass = new (...args: any[]) => any;
103+
104+
type MethodDecorator<T extends AnyClass> = (
105+
target: InstanceType<T>,
105106
propertyKey: string | symbol,
106-
descriptor:
107-
| TypedPropertyDescriptor<SyncHandler<Handler>>
108-
| TypedPropertyDescriptor<AsyncHandler<Handler>>
107+
descriptor: TypedPropertyDescriptor<AnyClassMethod>
109108
) => void;
110109

111-
// TODO: Revisit type below & make it more specific
112-
type MethodDecorator = (
113-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
114-
target: any,
115-
propertyKey: string | symbol,
116-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
117-
descriptor: TypedPropertyDescriptor<any>
118-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
119-
) => any;
110+
interface TracerInterface {
111+
addErrorAsMetadata(error: Error, remote?: boolean): void;
112+
addResponseAsMetadata(data?: unknown, methodName?: string): void;
113+
addServiceNameAnnotation(): void;
114+
annotateColdStart(): void;
115+
captureAWS<T>(aws: T): void | T;
116+
captureAWSv3Client<T>(service: T): void | T;
117+
captureAWSClient<T>(service: T): void | T;
118+
captureLambdaHandler(
119+
options?: CaptureLambdaHandlerOptions
120+
): HandlerMethodDecorator;
121+
captureMethod<T extends AnyClass>(
122+
options?: CaptureMethodOptions
123+
): MethodDecorator<T>;
124+
getSegment(): Segment | Subsegment | undefined;
125+
getRootXrayTraceId(): string | undefined;
126+
isTraceSampled(): boolean;
127+
isTracingEnabled(): boolean;
128+
putAnnotation: (key: string, value: string | number | boolean) => void;
129+
putMetadata: (
130+
key: string,
131+
value: unknown,
132+
namespace?: string | undefined
133+
) => void;
134+
setSegment(segment: Segment | Subsegment): void;
135+
}
120136

121137
export {
122138
TracerOptions,
123139
CaptureLambdaHandlerOptions,
124140
CaptureMethodOptions,
125141
HandlerMethodDecorator,
142+
AnyClass,
143+
AnyClassMethod,
126144
MethodDecorator,
145+
TracerInterface,
127146
};

Diff for: packages/tracer/src/types/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
export * from './Tracer';
1+
export {
2+
TracerOptions,
3+
CaptureLambdaHandlerOptions,
4+
CaptureMethodOptions,
5+
HandlerMethodDecorator,
6+
AnyClass,
7+
AnyClassMethod,
8+
MethodDecorator,
9+
TracerInterface,
10+
} from './Tracer.js';

Diff for: packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Tracer } from '../../src';
1+
import { Tracer } from '../../src/Tracer.js';
22
import type { Callback, Context } from 'aws-lambda';
33
import AWS from 'aws-sdk';
44
import axios from 'axios';
@@ -79,8 +79,6 @@ export class MyFunctionBase {
7979

8080
class MyFunctionWithDecorator extends MyFunctionBase {
8181
@tracer.captureLambdaHandler()
82-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
83-
// @ts-ignore
8482
public handler(
8583
event: CustomEvent,
8684
_context: Context,
@@ -90,8 +88,6 @@ class MyFunctionWithDecorator extends MyFunctionBase {
9088
}
9189

9290
@tracer.captureMethod()
93-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
94-
// @ts-ignore
9591
public myMethod(): string {
9692
return super.myMethod();
9793
}
@@ -102,8 +98,6 @@ export const handler = handlerClass.handler.bind(handlerClass);
10298

10399
class MyFunctionWithDecoratorCaptureResponseFalse extends MyFunctionBase {
104100
@tracer.captureLambdaHandler({ captureResponse: false })
105-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
106-
// @ts-ignore
107101
public handler(
108102
event: CustomEvent,
109103
_context: Context,
@@ -113,8 +107,6 @@ class MyFunctionWithDecoratorCaptureResponseFalse extends MyFunctionBase {
113107
}
114108

115109
@tracer.captureMethod({ captureResponse: false })
116-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
117-
// @ts-ignore
118110
public myMethod(): string {
119111
return super.myMethod();
120112
}

0 commit comments

Comments
 (0)