Skip to content

Commit

Permalink
feat: Omit default headers from req.headers prop
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradLang committed Oct 4, 2023
1 parent fe11019 commit e4de83c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 28 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ Use the following standardised logging fields to benefit from customised seriali

- `req` for HTTP requests.

The request object is trimmed to a set of essential fields.
The request object is trimmed to a set of essential fields.
Certain headers are omitted by default (e.g. `x-envoy-peer-metadata`).
To opt out of this behavior, set the `omitHeaderNames` logger option to an empty list `[]`.

- `res` for HTTP responses.

Expand Down
27 changes: 27 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,25 @@ testLog(
...objectWithDefaultOmitHeaderNameKeys,
['x-request-id']: 'some-uuid',
},
req: {
headers: {
['authorization']: bearerToken,
...objectWithDefaultOmitHeaderNameKeys,
['x-request-id']: 'some-uuid',
},
},
},
{
headers: {
['authorization']: redactedBearer,
['x-request-id']: 'some-uuid',
},
req: {
headers: {
['authorization']: redactedBearer,
['x-request-id']: 'some-uuid',
},
},
},
'info',
);
Expand All @@ -592,13 +605,27 @@ testLog(
...objectWithDefaultOmitHeaderNameKeys,
['x-request-id']: 'some-uuid',
},
req: {
headers: {
['authorization']: bearerToken,
...objectWithDefaultOmitHeaderNameKeys,
['x-request-id']: 'some-uuid',
},
},
},
{
headers: {
['authorization']: redactedBearer,
...objectWithDefaultOmitHeaderNameKeys,
['x-request-id']: 'some-uuid',
},
req: {
headers: {
['authorization']: redactedBearer,
...objectWithDefaultOmitHeaderNameKeys,
['x-request-id']: 'some-uuid',
},
},
},
'info',
{ omitHeaderNames: [] },
Expand Down
15 changes: 2 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import base from './base';
import { withRedaction } from './destination';
import { type FormatterOptions, createFormatters } from './formatters';
import * as redact from './redact';
import defaultSerializers, {
type SerializerOptions,
defaultOmitHeaderNames,
} from './serializers';
import { createOmitPropertiesSerializer } from './serializers/omitPropertiesSerializer';
import { type SerializerOptions, createSerializers } from './serializers';

export { pino };

Expand All @@ -31,14 +27,7 @@ export default (
}),
): Logger => {
opts.redact = redact.addDefaultRedactPathStrings(opts.redact);
const omitHeaderNamesSerializer = createOmitPropertiesSerializer('headers', {
omitPropertyNames: opts.omitHeaderNames ?? defaultOmitHeaderNames,
});
opts.serializers = {
...defaultSerializers,
...omitHeaderNamesSerializer,
...opts.serializers,
};
opts.serializers = createSerializers(opts);
const formatters = createFormatters(opts);
opts.base = {
...base,
Expand Down
19 changes: 18 additions & 1 deletion src/serializers/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import serializers from './index';
import serializers, { type Request, defaultOmitHeaderNames } from './index';

describe('req', () => {
const remoteAddress = '::ffff:123.45.67.89';
Expand Down Expand Up @@ -88,6 +88,23 @@ describe('req', () => {

expect(result).toStrictEqual({ ...expectedRequestBase });
});

it('omits defaultOmitHeaderNames by default', () => {
const objectWithDefaultOmitHeaderNameKeys = defaultOmitHeaderNames.reduce(
(headers, key) => ({ ...headers, [key]: 'header value' }),
{},
);
const request = {
...requestBase,
headers: {
...requestBase.headers,
...objectWithDefaultOmitHeaderNameKeys,
},
} as Partial<Request> as Request;
const result = serializers.req(request);

expect(result).toStrictEqual({ ...expectedRequestBase });
});
});

describe('res', () => {
Expand Down
53 changes: 40 additions & 13 deletions src/serializers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type pino from 'pino';
import { err, errWithCause } from 'pino-std-serializers';

import { omitProperties } from './omitProperties';
import { createOmitPropertiesSerializer } from './omitPropertiesSerializer';

export const defaultOmitHeaderNames = [
'x-envoy-attempt-count',
'x-envoy-decorator-operation',
Expand All @@ -18,7 +22,7 @@ interface Socket {
remoteAddress?: string;
remotePort?: string;
}
interface Request extends Record<string, unknown> {
export interface Request extends Record<string, unknown> {
method: string;
url: string;
headers: Record<string, string>;
Expand All @@ -40,16 +44,23 @@ const isObject = (value: unknown): boolean => {
return value != null && (type === 'object' || type === 'function');
};

const req = (request: Request) =>
isObject(request)
? {
method: request.method,
url: request.url,
headers: request.headers,
remoteAddress: request?.socket?.remoteAddress,
remotePort: request?.socket?.remotePort,
}
: request;
export const createReqSerializer = (opts: SerializerOptions) => {
const req = (request: Request) =>
isObject(request)
? {
method: request.method,
url: request.url,
headers: omitProperties(
request.headers,
opts.omitHeaderNames ?? defaultOmitHeaderNames,
),
remoteAddress: request?.socket?.remoteAddress,
remotePort: request?.socket?.remotePort,
}
: request;

return req;
};

const res = (response: Response) =>
isObject(response)
Expand All @@ -59,9 +70,25 @@ const res = (response: Response) =>
}
: response;

export default {
export const createSerializers = (
opts: SerializerOptions & Pick<pino.LoggerOptions, 'serializers'>,
) => {
const omitHeaderNamesSerializer = createOmitPropertiesSerializer('headers', {
omitPropertyNames: opts.omitHeaderNames ?? defaultOmitHeaderNames,
});
return {
...defaultSerializers,
req: createReqSerializer(opts),
...omitHeaderNamesSerializer,
...opts.serializers,
};
};

const defaultSerializers = {
err,
errWithCause,
res,
req,
req: createReqSerializer({}),
};

export default defaultSerializers;

0 comments on commit e4de83c

Please sign in to comment.