Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adds response reader. #432

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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