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
17 changes: 17 additions & 0 deletions packages/zipkin-instrumentation-fetch/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {Tracer} from 'zipkin';

// ResponseReader allows the user to customize the response based on
// the response. In order to not to leak context, user should
// bind this to `instrumentation`. An example use case for such
// feature is read response header to know if the response comes
// was cached or not.
type ResponseReader = (traceId: string, res: any) => void

declare function wrapFetch(got: any, {
tracer: Tracer,
serviceName: string,
remoteServiceName: string,
responseReader: ResponseReader
}): any;

export default wrapFetch;
7 changes: 6 additions & 1 deletion packages/zipkin-instrumentation-fetch/src/wrapFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
Instrumentation
} = require('zipkin');

function wrapFetch(fetch, {tracer, serviceName, remoteServiceName}) {
function wrapFetch(fetch, {tracer, serviceName, remoteServiceName, responseReader}) {
const instrumentation = new Instrumentation.HttpClient({tracer, serviceName, remoteServiceName});
return function zipkinfetch(input, opts = {}) {
return new Promise((resolve, reject) => {
Expand All @@ -17,6 +17,11 @@ function wrapFetch(fetch, {tracer, serviceName, remoteServiceName}) {
instrumentation.recordResponse(traceId, res.status);
});
resolve(res);
if (responseReader) {
tracer.scoped(() => {
responseReader.bind(instrumentation)(traceId, res);
});
}
}).catch((err) => {
tracer.scoped(() => {
instrumentation.recordError(traceId, err);
Expand Down