Created
August 18, 2022 03:14
-
-
Save damiancipolat/141a5e571a8ea601dec6381ac1219738 to your computer and use it in GitHub Desktop.
A hapi js distributed tracing plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cls = require('cls-hooked'); | |
const { v4: uuidV4 } = require('uuid'); | |
// generate a unique value for namespace | |
const name = 'settle-context'; | |
const nsId = `${name}:${uuidV4()}`; | |
const ns = cls.createNamespace(nsId); | |
const correlationIdKey = 'x-correlation-id'; | |
const getCorrelationId = () => ns.get(correlationIdKey); | |
const register = async (server) => { | |
server.ext('onRequest', (request, h) => { | |
const clsCtx = ns.createContext(); | |
ns.enter(clsCtx); | |
const requestId = request.headers[correlationIdKey.toLowerCase()] || uuidV4(); | |
console.log('xxxxx',requestId); | |
ns.set(correlationIdKey, requestId); | |
return h.continue; | |
}); | |
} | |
const plugin = { | |
name, | |
once: true, | |
register | |
}; | |
export default { | |
plugin, | |
getCorrelationId | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment