Skip to content

refactor: fix readme and cleanup codebase #1

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# aws-lambda-powertools-correlation
Correlation Ids for aws-lambda-powertools
<h3>Installation</h3>

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

<h3>Features</h3>

1. Middleware to enable injecting correlation ids from the request into the <i>AsyncLocalStorage</i>
```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';

Expand All @@ -19,7 +23,7 @@ export const handler = (_handler: Lambda.APIGatewayProxyHandlerV2) => {
2. Utility to extend the powertools logger with correlation ids: <i>injectCorrelationIds(logger)</i>

```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();
Expand All @@ -33,7 +37,7 @@ export const handler = (_handler: Lambda.APIGatewayProxyHandlerV2) => {

3. Utility to get correlation ids from the AsyncLocalStorage: <i>getCorrelationIds()</i>
```typescript
import { useCorrelationIds } from '@aws-lambda-powertools-correlation';
import { useCorrelationIds } from 'aws-lambda-powertools-correlation';

const correlationIds = useCorrelationIds();

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions src/correlation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export type CorrelationProps = {
awsDefaults?: boolean;
};

const asyncLocalStorage = new AsyncLocalStorage();
type CorrelationContext = {
[CORRELATION_KEY]: CorrelationIds;
}

const asyncLocalStorage = new AsyncLocalStorage<CorrelationContext>();

const CORRELATION_KEY = '__X_POWERTOOLS_CORRELATION_IDS__';

Expand All @@ -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',
},
});
Expand All @@ -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');

Expand Down