Skip to content
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

fix: relax ContextMap value constraint #61

Merged
merged 1 commit into from
Oct 23, 2024
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
6 changes: 3 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ const CONTEXT_SYMBOL = Symbol.for('aws.lambda.runtime.context');

export interface ContextMap {
awsRequestId: string;
[key: string]: string;
[key: string]: unknown;
}

export interface ContextStorageProvider {
getContext: () => ContextMap;
setContext: (map: ContextMap) => void;
updateContext: (values: Record<string, string>) => void;
updateContext: (values: Record<string, unknown>) => void;
}

export const GlobalContextStorageProvider: ContextStorageProvider = {
getContext: () => (global as any)[CONTEXT_SYMBOL] as ContextMap,
setContext: (map: ContextMap) => ((global as any)[CONTEXT_SYMBOL] = map),
updateContext: (values: Record<string, string>) => {
updateContext: (values: Record<string, unknown>) => {
const ctx = GlobalContextStorageProvider.getContext();
(global as any)[CONTEXT_SYMBOL] = {
...ctx,
Expand Down
12 changes: 5 additions & 7 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ export const lambdaRequestTracker = (

// capture any correlation headers sent from upstream callers
if (event.headers) {
Object.keys(event.headers).forEach((header) => {
for (const [header, value] of Object.entries(event.headers)) {
if (header.toLowerCase().startsWith(CORRELATION_HEADER)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
ctx[header] = event.headers![header] as string;
ctx[header] = value;
}
});
}
}

// capture the xray trace id if its enabled
if (process.env[AMAZON_TRACE_ID]) {
ctx[CORRELATION_TRACE_ID] = process.env[AMAZON_TRACE_ID] as string;
ctx[CORRELATION_TRACE_ID] = process.env[AMAZON_TRACE_ID];
}

// set the correlation id if not already set by upstream callers
Expand All @@ -49,9 +48,8 @@ export const lambdaRequestTracker = (
if (options.requestMixin) {
const result = options.requestMixin(event, context);
for (const key in result) {
// Cast this to string for typescript
// when the JSON serializer runs, by default it omits undefined properties
ctx[key] = result[key] as string;
ctx[key] = result[key];
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ tap.test('should allow removing default request data', (t) => {
t.end();
});

tap.test('should allow different types of context values', (t) => {
const { log, output, withRequest } = createLogger(undefined, {
requestMixin: () => ({
'number': 12,
'boolean': true,
'object': {
foo: "bar"
}
}),
});

withRequest({}, { awsRequestId: '431234' });
log.info('Message with trace ID');
t.matchSnapshot(output.buffer);
t.end();
});

tap.test('should allow default pino formatter', (t) => {
const { log, output, withRequest } = createLogger(undefined, {
formatter: new PinoLogFormatter(),
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface LambdaRequestTrackerOptions {
requestMixin?: (
event: LambdaEvent,
context: LambdaContext,
) => { [key: string]: string | undefined };
) => { [key: string]: unknown };
/**
* Custom storage provider.
* If not supplied, defaults to global context storage.
Expand Down
4 changes: 4 additions & 0 deletions tap-snapshots/src/tests/index.spec.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ exports[`src/tests/index.spec.ts TAP should allow default pino formatter > must
{"awsRequestId":"431234","x-correlation-id":"431234","level":30,"time":1480572000000,"msg":"Message with pino formatter"}
`

exports[`src/tests/index.spec.ts TAP should allow different types of context values > must match snapshot 1`] = `
2016-12-01T06:00:00.000Z 431234 INFO Message with trace ID {"awsRequestId":"431234","x-correlation-id":"431234","number":12,"boolean":true,"object":{"foo":"bar"},"level":30,"time":1480572000000,"msg":"Message with trace ID"}
`

exports[`src/tests/index.spec.ts TAP should allow modifying the global context > must match snapshot 1`] = `
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"}
`
Expand Down