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

add option to disable/ignore request-id header #4193

Merged
merged 5 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/build-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const schema = {
onProtoPoisoning: { type: 'string', default: defaultInitOptions.onProtoPoisoning },
onConstructorPoisoning: { type: 'string', default: defaultInitOptions.onConstructorPoisoning },
pluginTimeout: { type: 'integer', default: defaultInitOptions.pluginTimeout },
requestIdHeader: { type: 'string', default: defaultInitOptions.requestIdHeader },
requestIdHeader: { type: ['string', 'boolean'], default: defaultInitOptions.requestIdHeader },
requestIdLogLabel: { type: 'string', default: defaultInitOptions.requestIdLogLabel },
http2SessionTimeout: { type: 'integer', default: defaultInitOptions.http2SessionTimeout },
exposeHeadRoutes: { type: 'boolean', default: defaultInitOptions.exposeHeadRoutes },
Expand Down
2 changes: 1 addition & 1 deletion fastify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export type FastifyServerOptions<
serializerOpts?: FJSOptions | Record<string, unknown>,
serverFactory?: FastifyServerFactory<RawServer>,
caseSensitive?: boolean,
requestIdHeader?: string,
requestIdHeader?: string | false,
requestIdLogLabel?: string;
jsonShorthand?: boolean;
genReqId?: <RequestGeneric extends RequestGenericInterface = RequestGenericInterface, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault>(req: FastifyRequest<RequestGeneric, RawServer, RawRequestDefaultExpression<RawServer>, FastifySchema, TypeProvider>) => string,
Expand Down
2 changes: 1 addition & 1 deletion fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function fastify (options) {

validateBodyLimitOption(options.bodyLimit)

const requestIdHeader = options.requestIdHeader || defaultInitOptions.requestIdHeader
const requestIdHeader = (options.requestIdHeader === false) ? false : (options.requestIdHeader || defaultInitOptions.requestIdHeader)
const genReqId = options.genReqId || reqIdGenFactory()
const requestIdLogLabel = options.requestIdLogLabel || 'reqId'
const bodyLimit = options.bodyLimit || defaultInitOptions.bodyLimit
Expand Down
2 changes: 1 addition & 1 deletion lib/configValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"use strict";
module.exports = validate10;
module.exports.default = validate10;
const schema11 = {"type":"object","additionalProperties":false,"properties":{"connectionTimeout":{"type":"integer","default":0},"keepAliveTimeout":{"type":"integer","default":72000},"forceCloseConnections":{"oneOf":[{"type":"string","pattern":"idle"},{"type":"boolean"}]},"maxRequestsPerSocket":{"type":"integer","default":0,"nullable":true},"requestTimeout":{"type":"integer","default":0},"bodyLimit":{"type":"integer","default":1048576},"caseSensitive":{"type":"boolean","default":true},"allowUnsafeRegex":{"type":"boolean","default":false},"http2":{"type":"boolean"},"https":{"if":{"not":{"oneOf":[{"type":"boolean"},{"type":"null"},{"type":"object","additionalProperties":false,"required":["allowHTTP1"],"properties":{"allowHTTP1":{"type":"boolean"}}}]}},"then":{"setDefaultValue":true}},"ignoreTrailingSlash":{"type":"boolean","default":false},"ignoreDuplicateSlashes":{"type":"boolean","default":false},"disableRequestLogging":{"type":"boolean","default":false},"jsonShorthand":{"type":"boolean","default":true},"maxParamLength":{"type":"integer","default":100},"onProtoPoisoning":{"type":"string","default":"error"},"onConstructorPoisoning":{"type":"string","default":"error"},"pluginTimeout":{"type":"integer","default":10000},"requestIdHeader":{"type":"string","default":"request-id"},"requestIdLogLabel":{"type":"string","default":"reqId"},"http2SessionTimeout":{"type":"integer","default":72000},"exposeHeadRoutes":{"type":"boolean","default":true},"versioning":{"type":"object","additionalProperties":true,"required":["storage","deriveVersion"],"properties":{"storage":{},"deriveVersion":{}}},"constraints":{"type":"object","additionalProperties":{"type":"object","required":["name","storage","validate","deriveConstraint"],"additionalProperties":true,"properties":{"name":{"type":"string"},"storage":{},"validate":{},"deriveConstraint":{}}}}}};
const schema11 = {"type":"object","additionalProperties":false,"properties":{"connectionTimeout":{"type":"integer","default":0},"keepAliveTimeout":{"type":"integer","default":72000},"forceCloseConnections":{"oneOf":[{"type":"string","pattern":"idle"},{"type":"boolean"}]},"maxRequestsPerSocket":{"type":"integer","default":0,"nullable":true},"requestTimeout":{"type":"integer","default":0},"bodyLimit":{"type":"integer","default":1048576},"caseSensitive":{"type":"boolean","default":true},"allowUnsafeRegex":{"type":"boolean","default":false},"http2":{"type":"boolean"},"https":{"if":{"not":{"oneOf":[{"type":"boolean"},{"type":"null"},{"type":"object","additionalProperties":false,"required":["allowHTTP1"],"properties":{"allowHTTP1":{"type":"boolean"}}}]}},"then":{"setDefaultValue":true}},"ignoreTrailingSlash":{"type":"boolean","default":false},"ignoreDuplicateSlashes":{"type":"boolean","default":false},"disableRequestLogging":{"type":"boolean","default":false},"jsonShorthand":{"type":"boolean","default":true},"maxParamLength":{"type":"integer","default":100},"onProtoPoisoning":{"type":"string","default":"error"},"onConstructorPoisoning":{"type":"string","default":"error"},"pluginTimeout":{"type":"integer","default":10000},"requestIdHeader":{"type":["string","boolean"],"default":"request-id"},"requestIdLogLabel":{"type":"string","default":"reqId"},"http2SessionTimeout":{"type":"integer","default":72000},"exposeHeadRoutes":{"type":"boolean","default":true},"versioning":{"type":"object","additionalProperties":true,"required":["storage","deriveVersion"],"properties":{"storage":{},"deriveVersion":{}}},"constraints":{"type":"object","additionalProperties":{"type":"object","required":["name","storage","validate","deriveConstraint"],"additionalProperties":true,"properties":{"name":{"type":"string"},"storage":{},"validate":{},"deriveConstraint":{}}}}}};
const func2 = Object.prototype.hasOwnProperty;
const pattern0 = new RegExp("idle", "u");

Expand Down
2 changes: 1 addition & 1 deletion lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ function buildRouting (options) {
req.headers[kRequestAcceptVersion] = undefined
}

const id = req.headers[requestIdHeader] || genReqId(req)
const id = requestIdHeader === false ? genReqId(req) : (req.headers[requestIdHeader] || genReqId(req))

const loggerBinding = {
[requestIdLogLabel]: id
Expand Down
108 changes: 108 additions & 0 deletions test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,51 @@ test('The request id header key can be customized', t => {
})
})

test('The request id header key can be ignored', t => {
t.plan(9)
const REQUEST_ID = 'ignore-me'

const stream = split(JSON.parse)
const fastify = Fastify({
logger: { stream, level: 'info' },
requestIdHeader: false
})
t.teardown(() => fastify.close())

fastify.get('/', (req, reply) => {
t.equal(req.id, 'req-1')
req.log.info('some log message')
reply.send({ id: req.id })
})

fastify.inject({
method: 'GET',
url: '/',
headers: {
'request-id': REQUEST_ID
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.equal(payload.id, 'req-1')

stream.once('data', line => {
t.equal(line.reqId, 'req-1')
t.equal(line.msg, 'incoming request', 'message is set')

stream.once('data', line => {
t.equal(line.reqId, 'req-1')
t.equal(line.msg, 'some log message', 'message is set')

stream.once('data', line => {
t.equal(line.reqId, 'req-1')
t.equal(line.msg, 'request completed', 'message is set')
})
})
})
})
})

test('The request id header key can be customized along with a custom id generator', t => {
t.plan(12)
const REQUEST_ID = '42'
Expand Down Expand Up @@ -393,6 +438,69 @@ test('The request id header key can be customized along with a custom id generat
})
})

test('The request id header key can be ignored along with a custom id generator', t => {
t.plan(12)
const REQUEST_ID = 'ignore-me'

const stream = split(JSON.parse)
const fastify = Fastify({
logger: { stream, level: 'info' },
requestIdHeader: false,
genReqId (req) {
return 'foo'
}
})
t.teardown(() => fastify.close())

fastify.get('/one', (req, reply) => {
t.equal(req.id, 'foo')
req.log.info('some log message')
reply.send({ id: req.id })
})

fastify.get('/two', (req, reply) => {
t.equal(req.id, 'foo')
req.log.info('some log message 2')
reply.send({ id: req.id })
})

const matches = [
{ reqId: 'foo', msg: /incoming request/ },
{ reqId: 'foo', msg: /some log message/ },
{ reqId: 'foo', msg: /request completed/ },
{ reqId: 'foo', msg: /incoming request/ },
{ reqId: 'foo', msg: /some log message 2/ },
{ reqId: 'foo', msg: /request completed/ }
]

let i = 0
stream.on('data', line => {
t.match(line, matches[i])
i += 1
})

fastify.inject({
method: 'GET',
url: '/one',
headers: {
'request-id': REQUEST_ID
}
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.equal(payload.id, 'foo')
})

fastify.inject({
method: 'GET',
url: '/two'
}, (err, res) => {
t.error(err)
const payload = JSON.parse(res.payload)
t.equal(payload.id, 'foo')
})
})

test('The request id log label can be changed', t => {
t.plan(6)
const REQUEST_ID = '42'
Expand Down
1 change: 1 addition & 0 deletions test/types/fastify.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ expectAssignable<FastifyInstance<http.Server, http.IncomingMessage, http.ServerR
expectAssignable<FastifyInstance>(fastify({ serverFactory: () => http.createServer() }))
expectAssignable<FastifyInstance>(fastify({ caseSensitive: true }))
expectAssignable<FastifyInstance>(fastify({ requestIdHeader: 'request-id' }))
expectAssignable<FastifyInstance>(fastify({ requestIdHeader: false }))
expectAssignable<FastifyInstance>(fastify({ genReqId: () => 'request-id' }))
expectAssignable<FastifyInstance>(fastify({ trustProxy: true }))
expectAssignable<FastifyInstance>(fastify({ querystringParser: () => ({ foo: 'bar' }) }))
Expand Down
2 changes: 1 addition & 1 deletion test/types/instance.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ type InitialConfig = Readonly<{
onProtoPoisoning?: 'error' | 'remove' | 'ignore',
onConstructorPoisoning?: 'error' | 'remove' | 'ignore',
pluginTimeout?: number,
requestIdHeader?: string,
requestIdHeader?: string | false,
requestIdLogLabel?: string,
http2SessionTimeout?: number
}>
Expand Down
2 changes: 1 addition & 1 deletion types/instance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ export interface FastifyInstance<
onProtoPoisoning?: ProtoAction,
onConstructorPoisoning?: ConstructorAction,
pluginTimeout?: number,
requestIdHeader?: string,
requestIdHeader?: string | false,
requestIdLogLabel?: string,
http2SessionTimeout?: number
}>
Expand Down