Last active
November 17, 2022 10:28
-
-
Save AlexanderDzhoganov/9305e08f09996a75361baff018e5c91d to your computer and use it in GitHub Desktop.
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
import { AsyncLocalStorage } from 'async_hooks' | |
import nanoid from 'nanoid' | |
export const traceIdLocalStorage = new AsyncLocalStorage() | |
async function addTraceIdToRequest(ctx, next) { | |
const traceId = await nanoid() | |
ctx.set('X-Trace-ID', traceId) | |
traceIdLocalStorage.run({ traceId }, next) | |
} | |
function logMessage(message) { | |
const {traceId} = traceIdLocalStorage.getStore(); | |
console.log(`[${traceId}] ${message}`) | |
} | |
app.use(addTraceIdToRequest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment