Skip to content

Instantly share code, notes, and snippets.

@AlexanderDzhoganov
Last active November 17, 2022 10:28
Show Gist options
  • Save AlexanderDzhoganov/9305e08f09996a75361baff018e5c91d to your computer and use it in GitHub Desktop.
Save AlexanderDzhoganov/9305e08f09996a75361baff018e5c91d to your computer and use it in GitHub Desktop.
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