Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/zipkin-instrumentation-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"zipkin": "^0.19.2"
},
"devDependencies": {
"express-http-proxy": "^0.11.0",
"express-http-proxy": "^1.6.0",
"node-fetch": "^2.6.0"
}
}
38 changes: 20 additions & 18 deletions packages/zipkin-instrumentation-express/src/wrapExpressHttpProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,33 @@ class ExpressHttpProxyInstrumentation {

function wrapProxy(proxy, {tracer, serviceName, remoteServiceName}) {
return function zipkinProxy(host, options = {}) {
function wrapDecorateRequest(instrumentation, decorateRequest) {
function wrapProxyReqOptDecorator(instrumentation, proxyReqOptDecorator) {
return (proxyReq, serverReq) => {
const serverTraceId = serverReq._trace_id;
let wrappedProxyReq = proxyReq;
if (typeof decorateRequest === 'function') {
if (typeof proxyReqOptDecorator === 'function') {
tracer.letId(serverTraceId, () => {
wrappedProxyReq = decorateRequest(proxyReq, serverReq);
wrappedProxyReq = proxyReqOptDecorator(proxyReq, serverReq);
});
}

return instrumentation.decorateAndRecordRequest(serverReq, wrappedProxyReq, serverTraceId);
};
}

function wrapIntercept(instrumentation, intercept) {
return (rsp, data, serverReq, res, callback) => {
const instrumentedCallback = (err, rspd, sent) => {
instrumentation.recordResponse(rsp, serverReq._trace_id_proxy);
return callback(err, rspd, sent);
};

function wrapUserResDecorator(instrumentation, userResDecorator) {
return (rsp, data, serverReq, res) => {
const serverTraceId = serverReq._trace_id;
if (typeof intercept === 'function') {
tracer.letId(serverTraceId,
() => intercept(rsp, data, serverReq, res, instrumentedCallback));
if (typeof userResDecorator === 'function') {
let decoratedResponse;
tracer.letId(serverTraceId, () => {
decoratedResponse = userResDecorator(rsp, data, serverReq, res);
instrumentation.recordResponse(rsp, serverReq._trace_id_proxy);
});
return decoratedResponse;
} else {
instrumentedCallback(null, data);
instrumentation.recordResponse(rsp, serverReq._trace_id_proxy);
return data;
}
};
}
Expand All @@ -87,11 +87,13 @@ function wrapProxy(proxy, {tracer, serviceName, remoteServiceName}) {

const wrappedOptions = options;

const {decorateRequest} = wrappedOptions;
wrappedOptions.decorateRequest = wrapDecorateRequest(instrumentation, decorateRequest);
const {proxyReqOptDecorator} = wrappedOptions;
wrappedOptions.proxyReqOptDecorator = wrapProxyReqOptDecorator(
instrumentation, proxyReqOptDecorator
);

const {intercept} = wrappedOptions;
wrappedOptions.intercept = wrapIntercept(instrumentation, intercept);
const {userResDecorator} = wrappedOptions;
wrappedOptions.userResDecorator = wrapUserResDecorator(instrumentation, userResDecorator);

return proxy(host, wrappedOptions);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ describe('express proxy instrumentation - integration test', () => {
const zipkinProxy = wrapProxy(proxy, {tracer: tracer.tracer(), remoteServiceName});
frontendApp.use(middleware({tracer: tracer.tracer()}));
frontendApp.use(zipkinProxy(`127.0.0.1:${backend.address().port}`, {
decorateRequest: (proxyReq) => {
tracer.tracer().recordBinary('decorateRequest', '');
proxyReqOptDecorator: (proxyReq) => {
tracer.tracer().recordBinary('proxyReqOptDecorator', '');
return proxyReq;
},
intercept: (rsp, data, serverReq, res, callback) => {
tracer.tracer().recordBinary('intercept', '');
callback(null, data);
userResDecorator: (rsp, data, serverReq, res) => {
tracer.tracer().recordBinary('userResDecorator', '');
return data;
}
}));

Expand All @@ -61,8 +61,8 @@ describe('express proxy instrumentation - integration test', () => {
tags: {
'http.path': path,
'http.status_code': '200',
intercept: '',
decorateRequest: ''
userResDecorator: '',
proxyReqOptDecorator: ''
}
});
}
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('express proxy instrumentation - integration test', () => {
'http.path': path,
'http.status_code': '500',
error: '500', // TODO: better error message
decorateRequest: ''
proxyReqOptDecorator: ''
}
}));
});
Expand Down
24 changes: 10 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3771,7 +3771,7 @@ debug@3.1.0, debug@=3.1.0, debug@~3.1.0:
dependencies:
ms "2.0.0"

debug@^3.0.0, debug@^3.1.0, debug@^3.1.1, debug@^3.2.6:
debug@^3.0.0, debug@^3.0.1, debug@^3.1.0, debug@^3.1.1, debug@^3.2.6:
version "3.2.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
Expand Down Expand Up @@ -4282,12 +4282,7 @@ es-to-primitive@^1.2.1:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

es6-promise@^3.2.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-3.3.1.tgz#a08cdde84ccdbf34d027a1451bc91d4bcd28a613"
integrity sha1-oIzd6EzNvzTQJ6FFG8kdS80ophM=

es6-promise@^4.0.3:
es6-promise@^4.0.3, es6-promise@^4.1.1:
version "4.2.8"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a"
integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==
Expand Down Expand Up @@ -4590,13 +4585,14 @@ expand-template@^2.0.3:
resolved "https://registry.yarnpkg.com/expand-template/-/expand-template-2.0.3.tgz#6e14b3fcee0f3a6340ecb57d2e8918692052a47c"
integrity sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==

express-http-proxy@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/express-http-proxy/-/express-http-proxy-0.11.0.tgz#f2e6ba2e9e8677b1ba335375c3cae670d5a1bb0a"
integrity sha1-8ua6Lp6Gd7G6M1N1w8rmcNWhuwo=
express-http-proxy@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/express-http-proxy/-/express-http-proxy-1.6.0.tgz#8672b1093cc96b8a93e8e3da948dd111a668ef22"
integrity sha512-7Re6Lepg96NA2wiv7DC5csChAScn4K76/UgYnC71XiITCT1cgGTJUGK6GS0pIixudg3Fbx3Q6mmEW3mZv5tHFQ==
dependencies:
es6-promise "^3.2.1"
raw-body "^2.1.7"
debug "^3.0.1"
es6-promise "^4.1.1"
raw-body "^2.3.0"

express@^4.17.0:
version "4.17.1"
Expand Down Expand Up @@ -8616,7 +8612,7 @@ raw-body@2.4.0:
iconv-lite "0.4.24"
unpipe "1.0.0"

raw-body@^2.1.7:
raw-body@^2.3.0:
version "2.4.1"
resolved "https://registry.yarnpkg.com/raw-body/-/raw-body-2.4.1.tgz#30ac82f98bb5ae8c152e67149dac8d55153b168c"
integrity sha512-9WmIKF6mkvA0SLmA2Knm9+qj89e+j1zqgyn8aXGd7+nAduPoqgI9lO57SAZNn/Byzo5P7JhXTyg9PzaJbH73bA==
Expand Down