diff --git a/examples/cdk/functions/get-all-items.ts b/examples/cdk/functions/get-all-items.ts index f4479b6806..cf74cfd2fc 100644 --- a/examples/cdk/functions/get-all-items.ts +++ b/examples/cdk/functions/get-all-items.ts @@ -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 { diff --git a/examples/sam/src/get-all-items.ts b/examples/sam/src/get-all-items.ts index 125da137c9..79160eddfa 100644 --- a/examples/sam/src/get-all-items.ts +++ b/examples/sam/src/get-all-items.ts @@ -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'; diff --git a/packages/tracer/jest.config.js b/packages/tracer/jest.config.cjs similarity index 93% rename from packages/tracer/jest.config.js rename to packages/tracer/jest.config.cjs index f6b014a746..4932f36338 100644 --- a/packages/tracer/jest.config.js +++ b/packages/tracer/jest.config.cjs @@ -5,6 +5,9 @@ module.exports = { }, runner: 'groups', preset: 'ts-jest', + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1', + }, transform: { '^.+\\.ts?$': 'ts-jest', }, diff --git a/packages/tracer/package.json b/packages/tracer/package.json index d1647e7914..db37da1d73 100644 --- a/packages/tracer/package.json +++ b/packages/tracer/package.json @@ -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", @@ -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" ], diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index 71de957ca5..411f5c5fd3 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -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'; /** @@ -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( + options?: CaptureMethodOptions + ): MethodDecorator { 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 diff --git a/packages/tracer/src/TracerInterface.ts b/packages/tracer/src/TracerInterface.ts deleted file mode 100644 index b346af0f73..0000000000 --- a/packages/tracer/src/TracerInterface.ts +++ /dev/null @@ -1,34 +0,0 @@ -import type { - CaptureLambdaHandlerOptions, - CaptureMethodOptions, - HandlerMethodDecorator, - MethodDecorator, -} from './types'; -import type { Segment, Subsegment } from 'aws-xray-sdk-core'; - -interface TracerInterface { - addErrorAsMetadata(error: Error, remote?: boolean): void; - addResponseAsMetadata(data?: unknown, methodName?: string): void; - addServiceNameAnnotation(): void; - annotateColdStart(): void; - captureAWS(aws: T): void | T; - captureAWSv3Client(service: T): void | T; - captureAWSClient(service: T): void | T; - captureLambdaHandler( - options?: CaptureLambdaHandlerOptions - ): HandlerMethodDecorator; - captureMethod(options?: CaptureMethodOptions): MethodDecorator; - 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 { TracerInterface }; diff --git a/packages/tracer/src/config/EnvironmentVariablesService.ts b/packages/tracer/src/config/EnvironmentVariablesService.ts index ecb46b3e23..5b21241352 100644 --- a/packages/tracer/src/config/EnvironmentVariablesService.ts +++ b/packages/tracer/src/config/EnvironmentVariablesService.ts @@ -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 diff --git a/packages/tracer/src/config/index.ts b/packages/tracer/src/config/index.ts deleted file mode 100644 index 11fd37677e..0000000000 --- a/packages/tracer/src/config/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './ConfigServiceInterface'; -export * from './EnvironmentVariablesService'; diff --git a/packages/tracer/src/index.ts b/packages/tracer/src/index.ts index 4d7f09185e..56c5e72163 100644 --- a/packages/tracer/src/index.ts +++ b/packages/tracer/src/index.ts @@ -1,3 +1 @@ -export * from './Tracer'; -export * from './TracerInterface'; -export * from './middleware/middy'; +export { Tracer } from './Tracer.js'; diff --git a/packages/tracer/src/middleware/middy.ts b/packages/tracer/src/middleware/middy.ts index 6bf9c22cfa..5d1fd11433 100644 --- a/packages/tracer/src/middleware/middy.ts +++ b/packages/tracer/src/middleware/middy.ts @@ -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, diff --git a/packages/tracer/src/provider/ProviderService.ts b/packages/tracer/src/provider/ProviderService.ts index 271a820310..40130067b3 100644 --- a/packages/tracer/src/provider/ProviderService.ts +++ b/packages/tracer/src/provider/ProviderService.ts @@ -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, @@ -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 { diff --git a/packages/tracer/src/provider/index.ts b/packages/tracer/src/provider/index.ts deleted file mode 100644 index ef648fd32d..0000000000 --- a/packages/tracer/src/provider/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './ProviderService'; -export * from './ProviderServiceInterface'; diff --git a/packages/tracer/src/config/ConfigServiceInterface.ts b/packages/tracer/src/types/ConfigServiceInterface.ts similarity index 100% rename from packages/tracer/src/config/ConfigServiceInterface.ts rename to packages/tracer/src/types/ConfigServiceInterface.ts diff --git a/packages/tracer/src/provider/ProviderServiceInterface.ts b/packages/tracer/src/types/ProviderServiceInterface.ts similarity index 69% rename from packages/tracer/src/provider/ProviderServiceInterface.ts rename to packages/tracer/src/types/ProviderServiceInterface.ts index a8cd4a4f91..5439b3a27d 100644 --- a/packages/tracer/src/provider/ProviderServiceInterface.ts +++ b/packages/tracer/src/types/ProviderServiceInterface.ts @@ -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; @@ -12,7 +18,7 @@ interface ProviderServiceInterface { setDaemonAddress(address: string): void; - setContextMissingStrategy(strategy: unknown): void; + setContextMissingStrategy(strategy: ContextMissingStrategy): void; captureAWS(awsservice: T): T; @@ -39,4 +45,4 @@ interface ProviderServiceInterface { putMetadata(key: string, value: unknown, namespace?: string): void; } -export { ProviderServiceInterface }; +export { ProviderServiceInterface, ContextMissingStrategy }; diff --git a/packages/tracer/src/types/Tracer.ts b/packages/tracer/src/types/Tracer.ts index 9f15b927ad..abac332cf9 100644 --- a/packages/tracer/src/types/Tracer.ts +++ b/packages/tracer/src/types/Tracer.ts @@ -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. @@ -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 = ( + target: InstanceType, propertyKey: string | symbol, - descriptor: - | TypedPropertyDescriptor> - | TypedPropertyDescriptor> + descriptor: TypedPropertyDescriptor ) => 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 - // 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(aws: T): void | T; + captureAWSv3Client(service: T): void | T; + captureAWSClient(service: T): void | T; + captureLambdaHandler( + options?: CaptureLambdaHandlerOptions + ): HandlerMethodDecorator; + captureMethod( + options?: CaptureMethodOptions + ): MethodDecorator; + 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, }; diff --git a/packages/tracer/src/types/index.ts b/packages/tracer/src/types/index.ts index 8f8f55c173..9c5074d83a 100644 --- a/packages/tracer/src/types/index.ts +++ b/packages/tracer/src/types/index.ts @@ -1 +1,10 @@ -export * from './Tracer'; +export { + TracerOptions, + CaptureLambdaHandlerOptions, + CaptureMethodOptions, + HandlerMethodDecorator, + AnyClass, + AnyClassMethod, + MethodDecorator, + TracerInterface, +} from './Tracer.js'; diff --git a/packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts b/packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts index 90480bc3cd..ca2f6ace93 100644 --- a/packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts +++ b/packages/tracer/tests/e2e/allFeatures.decorator.test.functionCode.ts @@ -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'; @@ -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, @@ -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(); } @@ -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, @@ -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(); } diff --git a/packages/tracer/tests/e2e/allFeatures.decorator.test.ts b/packages/tracer/tests/e2e/allFeatures.decorator.test.ts index 7c9d3fb251..0dfeb15b79 100644 --- a/packages/tracer/tests/e2e/allFeatures.decorator.test.ts +++ b/packages/tracer/tests/e2e/allFeatures.decorator.test.ts @@ -6,25 +6,25 @@ import { TestStack } from '@aws-lambda-powertools/testing-utils'; import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resources/dynamodb'; import { join } from 'node:path'; -import { TracerTestNodejsFunction } from '../helpers/resources'; +import { TracerTestNodejsFunction } from '../helpers/resources.js'; import { assertAnnotation, assertErrorAndFault, -} from '../helpers/traceAssertions'; +} from '../helpers/traceAssertions.js'; import { getFirstSubsegment, getInvocationSubsegment, getTraces, invokeAllTestCases, splitSegmentsByName, -} from '../helpers/tracesUtils'; +} from '../helpers/tracesUtils.js'; import { commonEnvironmentVars, RESOURCE_NAME_PREFIX, SETUP_TIMEOUT, TEARDOWN_TIMEOUT, TEST_CASE_TIMEOUT, -} from './constants'; +} from './constants.js'; /** * The test includes one stack with 4 Lambda functions that correspond to the following test cases: diff --git a/packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts b/packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts index f7bd2cda64..95cdc3fd33 100644 --- a/packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts +++ b/packages/tracer/tests/e2e/allFeatures.manual.test.functionCode.ts @@ -1,4 +1,4 @@ -import { Tracer } from '../../src'; +import { Tracer } from '../../src/index.js'; import type { Context } from 'aws-lambda'; import axios from 'axios'; import AWS from 'aws-sdk'; diff --git a/packages/tracer/tests/e2e/allFeatures.manual.test.ts b/packages/tracer/tests/e2e/allFeatures.manual.test.ts index 98f4767501..280a1c3efd 100644 --- a/packages/tracer/tests/e2e/allFeatures.manual.test.ts +++ b/packages/tracer/tests/e2e/allFeatures.manual.test.ts @@ -5,27 +5,27 @@ */ import { TestStack } from '@aws-lambda-powertools/testing-utils'; import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resources/dynamodb'; -import { join } from 'path'; -import { TracerTestNodejsFunction } from '../helpers/resources'; +import { join } from 'node:path'; +import { TracerTestNodejsFunction } from '../helpers/resources.js'; import { assertAnnotation, assertErrorAndFault, -} from '../helpers/traceAssertions'; +} from '../helpers/traceAssertions.js'; import { getFirstSubsegment, getInvocationSubsegment, getTraces, invokeAllTestCases, splitSegmentsByName, -} from '../helpers/tracesUtils'; -import type { ParsedTrace } from '../helpers/traceUtils.types'; + type ParsedTrace, +} from '../helpers/tracesUtils.js'; import { commonEnvironmentVars, RESOURCE_NAME_PREFIX, SETUP_TIMEOUT, TEARDOWN_TIMEOUT, TEST_CASE_TIMEOUT, -} from './constants'; +} from './constants.js'; describe(`Tracer E2E tests, all features with manual instantiation`, () => { const testStack = new TestStack({ diff --git a/packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts b/packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts index 2dd55050da..f55af0bdd4 100644 --- a/packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts +++ b/packages/tracer/tests/e2e/allFeatures.middy.test.functionCode.ts @@ -1,5 +1,6 @@ import middy from '@middy/core'; -import { captureLambdaHandler, Tracer } from '../../src'; +import { Tracer } from '../../src/index.js'; +import { captureLambdaHandler } from '../../src/middleware/middy.js'; import type { Context } from 'aws-lambda'; import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb'; import axios from 'axios'; diff --git a/packages/tracer/tests/e2e/allFeatures.middy.test.ts b/packages/tracer/tests/e2e/allFeatures.middy.test.ts index cb7603928e..23d2fdfc23 100644 --- a/packages/tracer/tests/e2e/allFeatures.middy.test.ts +++ b/packages/tracer/tests/e2e/allFeatures.middy.test.ts @@ -6,25 +6,25 @@ import { TestStack } from '@aws-lambda-powertools/testing-utils'; import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resources/dynamodb'; import { join } from 'node:path'; -import { TracerTestNodejsFunction } from '../helpers/resources'; +import { TracerTestNodejsFunction } from '../helpers/resources.js'; import { assertAnnotation, assertErrorAndFault, -} from '../helpers/traceAssertions'; +} from '../helpers/traceAssertions.js'; import { getFirstSubsegment, getInvocationSubsegment, getTraces, invokeAllTestCases, splitSegmentsByName, -} from '../helpers/tracesUtils'; +} from '../helpers/tracesUtils.js'; import { commonEnvironmentVars, RESOURCE_NAME_PREFIX, SETUP_TIMEOUT, TEARDOWN_TIMEOUT, TEST_CASE_TIMEOUT, -} from './constants'; +} from './constants.js'; /** * The test includes one stack with 4 Lambda functions that correspond to the following test cases: diff --git a/packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts b/packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts index 98ee23cbf1..6d60a7b53e 100644 --- a/packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts +++ b/packages/tracer/tests/e2e/asyncHandler.decorator.test.functionCode.ts @@ -1,4 +1,4 @@ -import { Tracer } from '../../src'; +import { Tracer } from '../../src/index.js'; import type { Context } from 'aws-lambda'; import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; import { DynamoDBDocumentClient, PutCommand } from '@aws-sdk/lib-dynamodb'; diff --git a/packages/tracer/tests/e2e/asyncHandler.decorator.test.ts b/packages/tracer/tests/e2e/asyncHandler.decorator.test.ts index dcd4982616..557031143a 100644 --- a/packages/tracer/tests/e2e/asyncHandler.decorator.test.ts +++ b/packages/tracer/tests/e2e/asyncHandler.decorator.test.ts @@ -6,25 +6,25 @@ import { TestStack } from '@aws-lambda-powertools/testing-utils'; import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resources/dynamodb'; import { join } from 'node:path'; -import { TracerTestNodejsFunction } from '../helpers/resources'; +import { TracerTestNodejsFunction } from '../helpers/resources.js'; import { assertAnnotation, assertErrorAndFault, -} from '../helpers/traceAssertions'; +} from '../helpers/traceAssertions.js'; import { getFirstSubsegment, getInvocationSubsegment, getTraces, invokeAllTestCases, splitSegmentsByName, -} from '../helpers/tracesUtils'; +} from '../helpers/tracesUtils.js'; import { commonEnvironmentVars, RESOURCE_NAME_PREFIX, SETUP_TIMEOUT, TEARDOWN_TIMEOUT, TEST_CASE_TIMEOUT, -} from './constants'; +} from './constants.js'; describe(`Tracer E2E tests, async handler with decorator instantiation`, () => { const testStack = new TestStack({ diff --git a/packages/tracer/tests/helpers/resources.ts b/packages/tracer/tests/helpers/resources.ts index 4dc61ead4b..edd4faa769 100644 --- a/packages/tracer/tests/helpers/resources.ts +++ b/packages/tracer/tests/helpers/resources.ts @@ -4,7 +4,7 @@ import type { TestNodejsFunctionProps, } from '@aws-lambda-powertools/testing-utils/types'; import { TestNodejsFunction } from '@aws-lambda-powertools/testing-utils/resources/lambda'; -import { commonEnvironmentVars } from '../e2e/constants'; +import { commonEnvironmentVars } from '../e2e/constants.js'; class TracerTestNodejsFunction extends TestNodejsFunction { public constructor( diff --git a/packages/tracer/tests/helpers/traceAssertions.ts b/packages/tracer/tests/helpers/traceAssertions.ts index 8ad7b31ee8..559be2dfa1 100644 --- a/packages/tracer/tests/helpers/traceAssertions.ts +++ b/packages/tracer/tests/helpers/traceAssertions.ts @@ -1,8 +1,8 @@ -import { getFirstSubsegment } from './tracesUtils'; -import type { - AssertAnnotationParams, - ParsedDocument, -} from './traceUtils.types'; +import { + getFirstSubsegment, + type AssertAnnotationParams, + type ParsedDocument, +} from './tracesUtils.js'; export const assertAnnotation = (params: AssertAnnotationParams): void => { const { diff --git a/packages/tracer/tests/helpers/traceUtils.types.ts b/packages/tracer/tests/helpers/traceUtils.types.ts deleted file mode 100644 index fc2c8cac05..0000000000 --- a/packages/tracer/tests/helpers/traceUtils.types.ts +++ /dev/null @@ -1,70 +0,0 @@ -interface ParsedDocument { - name: string; - id: string; - start_time: number; - end_time?: number; - // This flag may be set if the segment hasn't been fully processed - // The trace may have already appeared in the `getTraceSummaries` response - // but a segment may still be in_progress - in_progress?: boolean; - aws?: { - request_id: string; - }; - http?: { - response: { - status: number; - }; - }; - origin?: string; - resource_arn?: string; - trace_id?: string; - subsegments?: ParsedDocument[]; - annotations?: { - [key: string]: string | boolean | number; - }; - metadata?: { - [key: string]: { - [key: string]: unknown; - }; - }; - fault?: boolean; - cause?: { - working_directory: string; - exceptions: { - message: string; - type: string; - remote: boolean; - stack: { - path: string; - line: number; - label: string; - }[]; - }[]; - }; - exception: { - message: string; - }; - error?: boolean; -} - -interface ParsedSegment { - Document: ParsedDocument; - Id: string; -} - -interface ParsedTrace { - Duration: number; - Id: string; - LimitExceeded: boolean; - Segments: ParsedSegment[]; -} - -interface AssertAnnotationParams { - annotations: ParsedDocument['annotations']; - isColdStart: boolean; - expectedServiceName: string; - expectedCustomAnnotationKey: string; - expectedCustomAnnotationValue: string | number | boolean; -} - -export { ParsedDocument, ParsedSegment, ParsedTrace, AssertAnnotationParams }; diff --git a/packages/tracer/tests/helpers/tracesUtils.ts b/packages/tracer/tests/helpers/tracesUtils.ts index 7995474a3b..b12b234175 100644 --- a/packages/tracer/tests/helpers/tracesUtils.ts +++ b/packages/tracer/tests/helpers/tracesUtils.ts @@ -5,12 +5,76 @@ import { GetTraceSummariesCommand, } from '@aws-sdk/client-xray'; import { invokeFunction } from '@aws-lambda-powertools/testing-utils'; -import { FunctionSegmentNotDefinedError } from './FunctionSegmentNotDefinedError'; -import type { - ParsedDocument, - ParsedSegment, - ParsedTrace, -} from './traceUtils.types'; +import { FunctionSegmentNotDefinedError } from './FunctionSegmentNotDefinedError.js'; + +interface ParsedDocument { + name: string; + id: string; + start_time: number; + end_time?: number; + // This flag may be set if the segment hasn't been fully processed + // The trace may have already appeared in the `getTraceSummaries` response + // but a segment may still be in_progress + in_progress?: boolean; + aws?: { + request_id: string; + }; + http?: { + response: { + status: number; + }; + }; + origin?: string; + resource_arn?: string; + trace_id?: string; + subsegments?: ParsedDocument[]; + annotations?: { + [key: string]: string | boolean | number; + }; + metadata?: { + [key: string]: { + [key: string]: unknown; + }; + }; + fault?: boolean; + cause?: { + working_directory: string; + exceptions: { + message: string; + type: string; + remote: boolean; + stack: { + path: string; + line: number; + label: string; + }[]; + }[]; + }; + exception: { + message: string; + }; + error?: boolean; +} + +interface ParsedSegment { + Document: ParsedDocument; + Id: string; +} + +interface ParsedTrace { + Duration: number; + Id: string; + LimitExceeded: boolean; + Segments: ParsedSegment[]; +} + +interface AssertAnnotationParams { + annotations: ParsedDocument['annotations']; + isColdStart: boolean; + expectedServiceName: string; + expectedCustomAnnotationKey: string; + expectedCustomAnnotationValue: string | number | boolean; +} type GetTracesOptions = { startTime: Date; @@ -250,4 +314,8 @@ export { getInvocationSubsegment, splitSegmentsByName, invokeAllTestCases, + ParsedDocument, + ParsedSegment, + ParsedTrace, + AssertAnnotationParams, }; diff --git a/packages/tracer/tests/unit/config/EnvironmentVariablesService.test.ts b/packages/tracer/tests/unit/EnvironmentVariablesService.test.ts similarity index 96% rename from packages/tracer/tests/unit/config/EnvironmentVariablesService.test.ts rename to packages/tracer/tests/unit/EnvironmentVariablesService.test.ts index 9e2adf9eb2..d20692b6dd 100644 --- a/packages/tracer/tests/unit/config/EnvironmentVariablesService.test.ts +++ b/packages/tracer/tests/unit/EnvironmentVariablesService.test.ts @@ -4,7 +4,7 @@ * @group unit/tracer/all */ -import { EnvironmentVariablesService } from '../../../src/config'; +import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js'; describe('Class: EnvironmentVariablesService', () => { const ENVIRONMENT_VARIABLES = process.env; diff --git a/packages/tracer/tests/unit/ProviderService.test.ts b/packages/tracer/tests/unit/ProviderService.test.ts index 1b74701b26..f7fe81ab62 100644 --- a/packages/tracer/tests/unit/ProviderService.test.ts +++ b/packages/tracer/tests/unit/ProviderService.test.ts @@ -3,8 +3,7 @@ * * @group unit/tracer/providerservice */ - -import { ProviderService } from '../../src/provider'; +import { ProviderService } from '../../src/provider/ProviderService.js'; import { captureAsyncFunc, captureAWS, @@ -21,8 +20,8 @@ import { setSegment, Subsegment, } from 'aws-xray-sdk-core'; -import http from 'http'; -import https from 'https'; +import http from 'node:http'; +import https from 'node:https'; import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons'; diff --git a/packages/tracer/tests/unit/Tracer.test.ts b/packages/tracer/tests/unit/Tracer.test.ts index a6381d4fac..784403daa5 100644 --- a/packages/tracer/tests/unit/Tracer.test.ts +++ b/packages/tracer/tests/unit/Tracer.test.ts @@ -5,15 +5,15 @@ */ import context from '@aws-lambda-powertools/testing-utils/context'; import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; -import { Tracer } from './../../src'; -import type { Callback, Context } from 'aws-lambda/handler'; +import { Tracer } from './../../src/index.js'; +import type { Callback, Context } from 'aws-lambda'; import { Segment, setContextMissingStrategy, Subsegment, } from 'aws-xray-sdk-core'; -import { ProviderServiceInterface } from '../../src/provider'; -import { ConfigServiceInterface } from 'packages/tracer/src/config'; +import type { ProviderServiceInterface } from '../../src/types/ProviderServiceInterface.js'; +import type { ConfigServiceInterface } from '../../src/types/ConfigServiceInterface.js'; type CaptureAsyncFuncMock = jest.SpyInstance< unknown, diff --git a/packages/tracer/tests/unit/middy.test.ts b/packages/tracer/tests/unit/middy.test.ts index 8195d89392..572843c284 100644 --- a/packages/tracer/tests/unit/middy.test.ts +++ b/packages/tracer/tests/unit/middy.test.ts @@ -3,10 +3,10 @@ * * @group unit/tracer/all */ -import { captureLambdaHandler } from '../../src/middleware/middy'; +import { captureLambdaHandler } from '../../src/middleware/middy.js'; import middy from '@middy/core'; -import { Tracer } from './../../src'; -import type { Context, Handler } from 'aws-lambda/handler'; +import { Tracer } from './../../src/index.js'; +import type { Context, Handler } from 'aws-lambda'; import { Segment, setContextMissingStrategy, diff --git a/packages/tracer/tsconfig.esm.json b/packages/tracer/tsconfig.esm.json new file mode 100644 index 0000000000..9bed8e4da4 --- /dev/null +++ b/packages/tracer/tsconfig.esm.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.esm.json", + "compilerOptions": { + "baseUrl": ".", + "outDir": "./lib/esm", + "rootDir": "./src" + }, + "include": [ + "./src/**/*" + ] +} \ No newline at end of file diff --git a/packages/tracer/tsconfig.json b/packages/tracer/tsconfig.json index 1cb9d72773..a30fdead42 100644 --- a/packages/tracer/tsconfig.json +++ b/packages/tracer/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "outDir": "./lib", + "outDir": "./lib/cjs", "rootDir": "./src", }, "include": [