Skip to content

feat(tracer): add esmodule support #1741

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 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cdk/functions/get-all-items.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
import middy from '@middy/core';
import {
Expand Down
2 changes: 1 addition & 1 deletion examples/sam/src/get-all-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { tableName } from './common/constants';
import { logger, tracer, metrics } from './common/powertools';
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
import { docClient } from './common/dynamodb-client';
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
import { getUuid } from './common/getUuid';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = {
},
runner: 'groups',
preset: 'ts-jest',
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.ts?$': 'ts-jest',
},
Expand Down
43 changes: 39 additions & 4 deletions packages/tracer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
"test:e2e:nodejs18x": "RUNTIME=nodejs18x jest --group=e2e",
"test:e2e": "jest --group=e2e",
"watch": "jest --watch",
"build": "tsc --build --force",
"build:cjs": "tsc --build --force && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
"build:esm": "tsc --project tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
"build": "npm run build:esm & npm run build:cjs",
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
"prebuild": "rimraf ./lib",
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
"prepack": "rimraf ./lib/*.tsbuildinfo && node ../../.github/scripts/release_patch_package_json.js ."
},
"lint-staged": {
"*.{js,ts}": "npm run lint-fix"
},
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/tracer#readme",
"license": "MIT-0",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"devDependencies": {
"@aws-lambda-powertools/testing-utils": "file:../testing",
"@aws-sdk/client-dynamodb": "^3.413.0",
Expand All @@ -46,6 +46,41 @@
"optional": true
}
},
"type": "module",
"exports": {
".": {
"require": {
"types": "./lib/cjs/index.d.ts",
"default": "./lib/cjs/index.js"
},
"import": {
"types": "./lib/esm/index.d.ts",
"default": "./lib/esm/index.js"
}
},
"./middleware": {
"import": "./lib/esm/middleware/middy.js",
"require": "./lib/cjs/middleware/middy.js"
},
"./types": {
"import": "./lib/esm/types/index.js",
"require": "./lib/cjs/types/index.js"
}
},
"typesVersions": {
"*": {
"middleware": [
"lib/cjs/middleware/middy.d.ts",
"lib/esm/middleware/middy.d.ts"
],
"types": [
"lib/cjs/types/index.d.ts",
"lib/esm/types/index.d.ts"
]
}
},
"types": "./lib/cjs/index.d.ts",
"main": "./lib/cjs/index.js",
"files": [
"lib"
],
Expand Down
20 changes: 11 additions & 9 deletions packages/tracer/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { Utility } from '@aws-lambda-powertools/commons';
import type {
AsyncHandler,
SyncHandler,
HandlerMethodDecorator,
} from '@aws-lambda-powertools/commons/types';
import type { TracerInterface } from '.';
import {
type ConfigServiceInterface,
EnvironmentVariablesService,
} from './config';
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
import type {
HandlerMethodDecorator,
TracerInterface,
TracerOptions,
AnyClass,
MethodDecorator,
CaptureLambdaHandlerOptions,
CaptureMethodOptions,
} from './types';
import { ProviderService, type ProviderServiceInterface } from './provider';
} from './types/Tracer.js';
import { ProviderService } from './provider/ProviderService.js';
import type { ProviderServiceInterface } from './types/ProviderServiceInterface.js';
import { type Segment, Subsegment } from 'aws-xray-sdk-core';

/**
Expand Down Expand Up @@ -461,7 +461,9 @@ class Tracer extends Utility implements TracerInterface {
* @decorator Class
* @param options - (_optional_) Options for the decorator
*/
public captureMethod(options?: CaptureMethodOptions): MethodDecorator {
public captureMethod<T extends AnyClass>(
options?: CaptureMethodOptions
): MethodDecorator<T> {
return (_target, propertyKey, descriptor) => {
// The descriptor.value is the method this decorator decorates, it cannot be undefined.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down
34 changes: 0 additions & 34 deletions packages/tracer/src/TracerInterface.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/tracer/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigServiceInterface } from './ConfigServiceInterface';
import { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';

class EnvironmentVariablesService
Expand Down
2 changes: 0 additions & 2 deletions packages/tracer/src/config/index.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/tracer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './Tracer';
export * from './TracerInterface';
export * from './middleware/middy';
export { Tracer } from './Tracer.js';
4 changes: 2 additions & 2 deletions packages/tracer/src/middleware/middy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TRACER_KEY } from '@aws-lambda-powertools/commons';
import type { Tracer } from '../Tracer';
import type { Tracer } from '../Tracer.js';
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
import type { CaptureLambdaHandlerOptions } from '../types';
import type { CaptureLambdaHandlerOptions } from '../types/Tracer.js';
import type {
MiddlewareLikeObj,
MiddyLikeRequest,
Expand Down
10 changes: 6 additions & 4 deletions packages/tracer/src/provider/ProviderService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ContextMissingStrategy } from 'aws-xray-sdk-core/dist/lib/context_utils';
import { Namespace } from 'cls-hooked';
import { ProviderServiceInterface } from '.';
import type {
ProviderServiceInterface,
ContextMissingStrategy,
} from '../types/ProviderServiceInterface.js';
import {
captureAWS,
captureAWSClient,
Expand Down Expand Up @@ -107,8 +109,8 @@ class ProviderService implements ProviderServiceInterface {
segment.addMetadata(key, value, namespace);
}

public setContextMissingStrategy(strategy: unknown): void {
setContextMissingStrategy(strategy as ContextMissingStrategy);
public setContextMissingStrategy(strategy: ContextMissingStrategy): void {
setContextMissingStrategy(strategy);
}

public setDaemonAddress(address: string): void {
Expand Down
2 changes: 0 additions & 2 deletions packages/tracer/src/provider/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Namespace } from 'cls-hooked';
import { Segment, Subsegment } from 'aws-xray-sdk-core';
import type { Namespace } from 'cls-hooked';
import type { Segment, Subsegment } from 'aws-xray-sdk-core';

type ContextMissingStrategy =
| 'LOG_ERROR'
| 'RUNTIME_ERROR'
| 'IGNORE_ERROR'
| ((msg: string) => void);

interface ProviderServiceInterface {
getNamespace(): Namespace;
Expand All @@ -12,7 +18,7 @@ interface ProviderServiceInterface {

setDaemonAddress(address: string): void;

setContextMissingStrategy(strategy: unknown): void;
setContextMissingStrategy(strategy: ContextMissingStrategy): void;

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

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

export { ProviderServiceInterface };
export { ProviderServiceInterface, ContextMissingStrategy };
61 changes: 40 additions & 21 deletions packages/tracer/src/types/Tracer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { ConfigServiceInterface } from '../config';
import type { Handler } from 'aws-lambda';
import type {
AsyncHandler,
LambdaInterface,
SyncHandler,
} from '@aws-lambda-powertools/commons/types';
import type { ConfigServiceInterface } from './ConfigServiceInterface.js';
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
import type { Segment, Subsegment } from 'aws-xray-sdk-core';

/**
* Options for the tracer class to be used during initialization.
Expand Down Expand Up @@ -100,28 +96,51 @@ type CaptureMethodOptions = {
captureResponse?: boolean;
};

type HandlerMethodDecorator = (
target: LambdaInterface,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyClassMethod = (...args: any[]) => any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnyClass = new (...args: any[]) => any;

type MethodDecorator<T extends AnyClass> = (
target: InstanceType<T>,
propertyKey: string | symbol,
descriptor:
| TypedPropertyDescriptor<SyncHandler<Handler>>
| TypedPropertyDescriptor<AsyncHandler<Handler>>
descriptor: TypedPropertyDescriptor<AnyClassMethod>
) => void;

// TODO: Revisit type below & make it more specific
type MethodDecorator = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
target: any,
propertyKey: string | symbol,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
descriptor: TypedPropertyDescriptor<any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => any;
interface TracerInterface {
addErrorAsMetadata(error: Error, remote?: boolean): void;
addResponseAsMetadata(data?: unknown, methodName?: string): void;
addServiceNameAnnotation(): void;
annotateColdStart(): void;
captureAWS<T>(aws: T): void | T;
captureAWSv3Client<T>(service: T): void | T;
captureAWSClient<T>(service: T): void | T;
captureLambdaHandler(
options?: CaptureLambdaHandlerOptions
): HandlerMethodDecorator;
captureMethod<T extends AnyClass>(
options?: CaptureMethodOptions
): MethodDecorator<T>;
getSegment(): Segment | Subsegment | undefined;
getRootXrayTraceId(): string | undefined;
isTraceSampled(): boolean;
isTracingEnabled(): boolean;
putAnnotation: (key: string, value: string | number | boolean) => void;
putMetadata: (
key: string,
value: unknown,
namespace?: string | undefined
) => void;
setSegment(segment: Segment | Subsegment): void;
}

export {
TracerOptions,
CaptureLambdaHandlerOptions,
CaptureMethodOptions,
HandlerMethodDecorator,
AnyClass,
AnyClassMethod,
MethodDecorator,
TracerInterface,
};
11 changes: 10 additions & 1 deletion packages/tracer/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export * from './Tracer';
export {
TracerOptions,
CaptureLambdaHandlerOptions,
CaptureMethodOptions,
HandlerMethodDecorator,
AnyClass,
AnyClassMethod,
MethodDecorator,
TracerInterface,
} from './Tracer.js';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tracer } from '../../src';
import { Tracer } from '../../src/Tracer.js';
import type { Callback, Context } from 'aws-lambda';
import AWS from 'aws-sdk';
import axios from 'axios';
Expand Down Expand Up @@ -79,8 +79,6 @@ export class MyFunctionBase {

class MyFunctionWithDecorator extends MyFunctionBase {
@tracer.captureLambdaHandler()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public handler(
event: CustomEvent,
_context: Context,
Expand All @@ -90,8 +88,6 @@ class MyFunctionWithDecorator extends MyFunctionBase {
}

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

class MyFunctionWithDecoratorCaptureResponseFalse extends MyFunctionBase {
@tracer.captureLambdaHandler({ captureResponse: false })
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public handler(
event: CustomEvent,
_context: Context,
Expand All @@ -113,8 +107,6 @@ class MyFunctionWithDecoratorCaptureResponseFalse extends MyFunctionBase {
}

@tracer.captureMethod({ captureResponse: false })
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
public myMethod(): string {
return super.myMethod();
}
Expand Down
Loading