Skip to content

Commit

Permalink
0.8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicporteous committed Jul 5, 2021
1 parent 8777100 commit b5fc78f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serve",
"version": "0.8.3",
"version": "0.8.4",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
25 changes: 14 additions & 11 deletions src/tracing/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const tracingMiddleware = (opts) => {
tracerProvider.addSpanProcessor(opts.processor || defaultProcessor)

const instrumentations = []
.concat(opts.instrumentations, [ new KoaInstrumentation(), new HttpInstrumentation() ])
.concat([ new HttpInstrumentation(), new KoaInstrumentation() ], opts.instrumentations)
.filter((i) => !!i)

registerInstrumentations({ instrumentations, tracerProvider })
Expand All @@ -56,7 +56,11 @@ const tracingMiddleware = (opts) => {
exporter: exporter.name
})

return (ctx, next) => {
return async (ctx, next) => {

ctx.tracer = api.trace.getTracer(serviceName, serviceVersion)

await next()

const requestId = ctx.requestId

Expand All @@ -82,10 +86,6 @@ const tracingMiddleware = (opts) => {

}

ctx.tracer = api.trace.getTracer(serviceName, serviceVersion)

return next()

}

}
Expand All @@ -97,14 +97,17 @@ const traceRoute = () => async (ctx, next) => {
const path = ctx.path
const method = ctx.method

logger.debug('Tracing route params', { requestId, params, path })
const span = getCurrentSpan()
const traceId = span.spanContext().traceId

logger.debug('Tracing route params', { requestId, traceId, method, params, path })

const route = params
? Object.entries(params).reduce((acc, [ key, val ]) => acc.replace(`/${val}`, `/:${key}`), path)
: path

const span = ctx.tracer.startSpan(`${method} ${route}`, { kind: api.SpanKind.SERVER })
const { traceId } = span.spanContext()
span.setAttribute('http.route', route)
span.setAttribute('http.params', JSON.stringify(params))

logger.debug('Tracing route', { requestId, traceId, route })

Expand All @@ -114,11 +117,11 @@ const traceRoute = () => async (ctx, next) => {

await next()

span.setStatus({ code: api.StatusCode.OK })
span.setStatus({ code: api.SpanStatusCode.OK })

} catch (error) {

span.setStatus({ code: api.StatusCode.ERROR, message: error.message })
span.setStatus({ code: api.SpanStatusCode.ERROR, message: error.message })

}

Expand Down

0 comments on commit b5fc78f

Please sign in to comment.