UNPKG

egg-tracer-sdwk

Version:
32 lines (25 loc) 680 B
'use strict'; const uuid = require('uuid'); const TRACE_ID = Symbol('traceId'); class Tracer { constructor(ctx) { this.ctx = ctx; } get traceId() { const {ctx} = this // ctx 未设置tracerId if (!this[TRACE_ID]) { let requestId = ctx.get('x-ca-request-id') if (!requestId ) { requestId = uuid.v1() ctx.set('x-ca-request-id', requestId) this[TRACE_ID] = `${ctx.app.name}/new/${process.pid}/${requestId}` return this[TRACE_ID] } this[TRACE_ID] = `${ctx.app.name}/inherit/${process.pid}/${requestId}` return this[TRACE_ID] } return this[TRACE_ID]; } } module.exports = Tracer;