diff --git a/README.md b/README.md index e0abfc0..243b285 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,16 @@ # aws-lambda-powertools-correlation Correlation Ids for aws-lambda-powertools +

Installation

+``` +npm|yarn|pnpm i aws-lambda-powertools-correlation +```

Features

1. Middleware to enable injecting correlation ids from the request into the AsyncLocalStorage ```typescript -import { enableCorrelationIds } from '@aws-lambda-powertools-correlation'; +import { enableCorrelationIds } from 'aws-lambda-powertools-correlation'; import middy from '@middy/core'; import * as Lambda from 'aws-lambda'; @@ -19,7 +23,7 @@ export const handler = (_handler: Lambda.APIGatewayProxyHandlerV2) => { 2. Utility to extend the powertools logger with correlation ids: injectCorrelationIds(logger) ```typescript -import { injectCorrelationIds } from '@aws-lambda-powertools-correlation'; +import { injectCorrelationIds } from 'aws-lambda-powertools-correlation'; import { Logger } from '@aws-lambda-powertools/logger' const logger = new Logger(); @@ -33,7 +37,7 @@ export const handler = (_handler: Lambda.APIGatewayProxyHandlerV2) => { 3. Utility to get correlation ids from the AsyncLocalStorage: getCorrelationIds() ```typescript -import { useCorrelationIds } from '@aws-lambda-powertools-correlation'; +import { useCorrelationIds } from 'aws-lambda-powertools-correlation'; const correlationIds = useCorrelationIds(); diff --git a/package-lock.json b/package-lock.json index fed79a9..5514ad7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "@aws-lambda-powertools-correlation", + "name": "aws-lambda-powertools-correlation", "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "@aws-lambda-powertools-correlation", + "name": "aws-lambda-powertools-correlation", "version": "0.1.0", "license": "GNU", "dependencies": { diff --git a/package.json b/package.json index 4104591..2f17deb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aws-lambda-powertools-correlation", - "version": "0.1.0", + "version": "0.1.2", "description": "Correlation Ids to be used with powertools-lambda", "scripts": { "test": "npm run test:unit", @@ -52,16 +52,13 @@ "husky": "^8.0.2", "jest": "^29.3.1", "jest-runner-groups": "^2.2.0", - "lerna": "^6.6.2", "lint-staged": "^13.1.2", "prettier": "^2.8.8", "rimraf": "^5.0.1", "ts-jest": "^29.0.3", "ts-node": "^10.9.1", "typedoc": "^0.24.7", - "typedoc-plugin-missing-exports": "^2.0.0", - "typescript": "^4.9.4", - "uuid": "^9.0.0" + "typescript": "^4.9.4" }, "files": [ "lib" diff --git a/src/correlation.ts b/src/correlation.ts index 3422893..364b297 100644 --- a/src/correlation.ts +++ b/src/correlation.ts @@ -11,7 +11,11 @@ export type CorrelationProps = { awsDefaults?: boolean; }; -const asyncLocalStorage = new AsyncLocalStorage(); +type CorrelationContext = { + [CORRELATION_KEY]: CorrelationIds; +} + +const asyncLocalStorage = new AsyncLocalStorage(); const CORRELATION_KEY = '__X_POWERTOOLS_CORRELATION_IDS__'; @@ -30,7 +34,7 @@ export const enableCorrelationIds = (props?: CorrelationProps) => { return { } else { // no need to track default correlation ids -> initialize asyncLocalStorage with empty object asyncLocalStorage.enterWith({ - CORRELATION_KEY: { + [CORRELATION_KEY]: { 'call-chain-length': '1', }, }); @@ -43,8 +47,8 @@ export const injectCorrelationIds = (logger: PowertoolsLogger) => { logger.addPersistentLogAttributes({ ...useCorrelationIds() }); }; -export const useCorrelationIds = (): CorrelationIds => { - const store = asyncLocalStorage.getStore() as { [CORRELATION_KEY]: CorrelationIds }; +export const useCorrelationIds = () => { + const store = asyncLocalStorage.getStore(); if (!store) { console.warn('No asyncLocalStorage store found');