Skip to content

[FastifyInstance] Expose fastify#setGenReqId #5170

@metcoder95

Description

@metcoder95

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

Currently, we support genReqId initial config when creating a fastify app.
This affects the root instance, and all its child afterwards.

But currently, there's no way to customize it after creation. e.g. through a plugin, or just through an encapsulated context.

Having fastify#setGenReqId (similar to the work done for fastify#setChildLoggerFactory) can enable different use cases at fastify at different levels.

e.g.

Attach a function from a plugin
Maybe you have an observability plugin standardised for your company, and it handles custom ways of inferring or creating request ids.

function ObsPlugin(instance, config, done) {
  // do magic
  instance.setGenReqId((req) => {
    // infer it or create it in a custom way
  })
}

This will affect the parent context.

Create different request ids based on resources

// I might want to generate one per resource
fastify.register((instance, opts, done) => {
  instance.setGenReqId((req) => { /* one way for admin */})
}, { prefix: '/admin' })

fastify.register((instance, opts, done) => {
  instance.setGenReqId((req) => { /* one way for common users */})
}, { prefix: '/users' })

I believe the benefit of the encapsulated context can be a key differentiator.

Motivation

No response

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestNew feature to be addedsemver-minorIssue or PR that should land as semver minor

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions