-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from uswitch/fix/swallowing-errors-in-other-m…
…iddleware fix bug where zipkin-instrumentation-koa was swallowing exceptions in other middleware
- Loading branch information
Showing
3 changed files
with
10,941 additions
and
8,669 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2020 The OpenZipkin Authors; licensed to You under the Apache License, Version 2.0. | ||
// Copied in from https://github.com/openzipkin/zipkin-js/blob/ec89188cf6a07e184ab886c1dfb6c9dc276ddfa4/packages/zipkin-instrumentation-koa/src/koaMiddleware.js | ||
// to fix the bug where all exceptions in middleware are swallowed by the .catch | ||
|
||
import {option, Instrumentation} from 'zipkin' | ||
|
||
const {Some, None} = option | ||
|
||
/** | ||
* @typedef {Object} MiddlewareOptions | ||
* @property {Object} tracer | ||
* @property {string} serviceName | ||
* @property {number} port | ||
*/ | ||
|
||
/** | ||
* @param {MiddlewareOptions} | ||
* @return {ZipkinKoaMiddleware} | ||
*/ | ||
export default function koaMiddleware ({tracer, serviceName, port = 0}) { | ||
const instrumentation = new Instrumentation.HttpServer({tracer, serviceName, port}) | ||
|
||
/** | ||
* @method | ||
* @typedef {function} ZipkinKoaMiddleware | ||
* @param {Object} ctx | ||
* @param {function()} next | ||
*/ | ||
return function zipkinKoaMiddleware (ctx, next) { | ||
function readHeader (header) { | ||
const val = ctx.request.headers[header.toLowerCase()] | ||
if (val != null) { | ||
return new Some(val) | ||
} else { | ||
return None | ||
} | ||
} | ||
return tracer.scoped(() => { | ||
const method = ctx.request.method.toUpperCase() | ||
const id = instrumentation.recordRequest(method, ctx.request.href, readHeader) | ||
|
||
Object.defineProperty(ctx.request, '_trace_id', {configurable: false, get: () => id}) | ||
|
||
const recordResponse = () => { | ||
tracer.letId(id, () => { | ||
// support koa-route and koa-router | ||
const matchedPath = ctx.routePath || ctx._matchedRoute | ||
tracer.recordRpc(instrumentation.spanNameFromRoute(method, matchedPath, ctx.status)) | ||
instrumentation.recordResponse(id, ctx.status) | ||
}) | ||
} | ||
|
||
const recordError = (err) => { | ||
recordResponse() | ||
throw err | ||
} | ||
|
||
return next() | ||
.then(recordResponse) | ||
.catch(recordError) | ||
}) | ||
} | ||
}; |
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
Oops, something went wrong.