Skip to content

Commit 7c27126

Browse files
authored
feat: support global context updates (#49)
1 parent 9ac280a commit 7c27126

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ other Cloudwatch aware tools such as Datadog and Splunk.
5959
}
6060
```
6161

62+
## Context Updates
63+
64+
The request logging context can be updated downstream by calling the `updateContext` function. Any duplicate values will overwrite previous values.
65+
66+
_Note: If you provide a custom storage context, you will need to update that storage context directly (this is not typical)_
67+
68+
```
69+
import { GlobalContextStorageProvider } from 'pino-lambda';
70+
GlobalContextStorageProvider.updateContext({ userId: '12' });
71+
```
72+
6273
## Lambda request tracing
6374

6475
With context tracing enabled, all instances of `pino` that use one of the built in formatters will automatically log the request id and other details so you don't need to pass an instance of a logger to all of your functions.

src/context.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22
const CONTEXT_SYMBOL = Symbol.for('aws.lambda.runtime.context');
33

44
export interface ContextMap {
5-
awsRequestId: string,
6-
[key: string]: string
7-
};
5+
awsRequestId: string;
6+
[key: string]: string;
7+
}
88

99
export interface ContextStorageProvider {
1010
getContext: () => ContextMap;
1111
setContext: (map: ContextMap) => void;
12+
updateContext: (values: Record<string, string>) => void;
1213
}
1314

1415
export const GlobalContextStorageProvider: ContextStorageProvider = {
1516
getContext: () => (global as any)[CONTEXT_SYMBOL] as ContextMap,
1617
setContext: (map: ContextMap) => ((global as any)[CONTEXT_SYMBOL] = map),
18+
updateContext: (values: Record<string, string>) => {
19+
const ctx = GlobalContextStorageProvider.getContext();
20+
(global as any)[CONTEXT_SYMBOL] = {
21+
...ctx,
22+
...values,
23+
};
24+
},
1725
};

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// reexport all public types
1+
export * from './context';
22
export * from './destination';
33
export * from './formatters';
44
export * from './request';

src/tests/index.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
LogData,
1414
} from '../';
1515

16+
import { GlobalContextStorageProvider } from '../context';
17+
1618
sinon.useFakeTimers(Date.UTC(2016, 11, 1, 6, 0, 0, 0));
1719

1820
tap.test('should log a simple info message', (t) => {
@@ -85,6 +87,21 @@ tap.test('should log correlation headers', (t) => {
8587
t.end();
8688
});
8789

90+
tap.test('should allow modifying the global context', (t) => {
91+
const { log, output, withRequest } = createLogger();
92+
93+
withRequest(
94+
{ headers: { 'x-correlation-data': 'abbb' } },
95+
{ awsRequestId: '98875' },
96+
);
97+
98+
GlobalContextStorageProvider.updateContext({ userId: '12' });
99+
100+
log.error('Context updates');
101+
t.matchSnapshot(output.buffer);
102+
t.end();
103+
});
104+
88105
tap.test('should set correlation if to trace id if present', (t) => {
89106
const { log, output, withRequest } = createLogger();
90107

tap-snapshots/src/tests/index.spec.ts.test.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ exports[`src/tests/index.spec.ts TAP should allow default pino formatter > must
1717
{"awsRequestId":"431234","x-correlation-id":"431234","level":30,"time":1480572000000,"msg":"Message with pino formatter"}
1818
`
1919

20+
exports[`src/tests/index.spec.ts TAP should allow modifying the global context > must match snapshot 1`] = `
21+
2016-12-01T06:00:00.000Z 98875 ERROR Context updates {"awsRequestId":"98875","x-correlation-data":"abbb","x-correlation-id":"98875","userId":"12","level":50,"time":1480572000000,"msg":"Context updates"}
22+
`
23+
2024
exports[`src/tests/index.spec.ts TAP should allow removing default request data > must match snapshot 1`] = `
2125
2016-12-01T06:00:00.000Z 431234 INFO Message with trace ID {"awsRequestId":"431234","level":30,"time":1480572000000,"msg":"Message with trace ID"}
2226
`

0 commit comments

Comments
 (0)