function wrapRequest(request, {tracer, serviceName, remoteServiceName}) { const instrumentation = new Instrumentation.HttpClient({tracer, serviceName, remoteServiceName}); return request.defaults((options, callback) => tracer.scoped(() => { const method = options.method || 'GET'; const url = options.uri || options.url; const wrappedOptions = instrumentation.recordRequest(options, url, method); const traceId = tracer.id; const recordResponse = (response) => { tracer.scoped(() => { instrumentation.recordResponse(traceId, response.statusCode); }); }; const recordError = (error) => { tracer.scoped(() => { instrumentation.recordError(traceId, error); }); }; return request(wrappedOptions, callback) .on('response', recordResponse) .on('error', recordError); })); }
function wrapFetch(fetch, {tracer, serviceName, remoteServiceName}) { const instrumentation = new Instrumentation.HttpClient({tracer, serviceName, remoteServiceName}); return function zipkinfetch(input, opts = {}) { return new Promise((resolve, reject) => { tracer.scoped(() => { const url = (typeof input === 'string') ? input : input.url; const method = opts.method || 'GET'; const zipkinOpts = instrumentation.recordRequest(opts, url, method); const traceId = tracer.id; fetch(url, zipkinOpts).then((res) => { tracer.scoped(() => { instrumentation.recordResponse(traceId, res.status); }); resolve(res); }).catch((err) => { tracer.scoped(() => { instrumentation.recordError(traceId, err); }); reject(err); }); }); }); }; }
instrumentation.recordResponse(traceId, error.response.status); } else { instrumentation.recordError(traceId, error);
const ctx = getZipkinContext(err.gotOptions); tracer.scoped(() => { instrumentation.recordError(ctx.traceId, err); }); return err;
constructor(tracer, remoteServiceName) { this.tracer = tracer; this.instrumentation = new Instrumentation.HttpClient({tracer, remoteServiceName}); /** * This section is very important, it guarantees correct trace duration */ this.httpRequest = request.defaults((options, callback) => tracer.scoped(() => { const method = options.method || 'GET'; const url = options.uri || options.url; const wrappedOptions = this.instrumentation.recordRequest(options, url, method); const traceId = tracer.id; const recordResponse = response => tracer.scoped( // TODO: scoped bc recordResponse leaks () => this.instrumentation.recordResponse(traceId, response.statusCode) ); const recordError = error => tracer.scoped( // TODO: scoped bc recordError leaks () => this.instrumentation.recordError(traceId, error) ); return request(wrappedOptions, callback) .on('response', recordResponse) .on('error', recordError); })); }
if (done) return; done = true; instrumentation.recordError(traceId, error); });
if (done) return; done = true; instrumentation.recordError(traceId, error); }); };
constructor(tracer, remoteServiceName) { this.tracer = tracer; this.instrumentation = new Instrumentation.HttpClient({tracer, remoteServiceName}); /** * This section is very important, it guarantees correct trace duration */ this.httpRequest = request.defaults((options, callback) => tracer.scoped(() => { const method = options.method || 'GET'; const url = options.uri || options.url; const wrappedOptions = this.instrumentation.recordRequest(options, url, method); const traceId = tracer.id; const recordResponse = response => tracer.scoped( // TODO: scoped bc recordResponse leaks () => this.instrumentation.recordResponse(traceId, response.statusCode) ); const recordError = error => tracer.scoped( // TODO: scoped bc recordError leaks () => this.instrumentation.recordError(traceId, error) ); return request(wrappedOptions, callback) .on('response', recordResponse) .on('error', recordError); })); }
function wrapRequest(request, {tracer, serviceName, remoteServiceName}) { const instrumentation = new Instrumentation.HttpClient({tracer, serviceName, remoteServiceName}); return request.defaults((options, callback) => tracer.scoped(() => { const method = options.method || 'GET'; const url = options.uri || options.url; const wrappedOptions = instrumentation.recordRequest(options, url, method); const traceId = tracer.id; const recordResponse = (response) => { tracer.scoped(() => { instrumentation.recordResponse(traceId, response.statusCode); }); }; const recordError = (error) => { tracer.scoped(() => { instrumentation.recordError(traceId, error); }); }; return request(wrappedOptions, callback) .on('response', recordResponse) .on('error', recordError); })); }
function wrapFetch(fetch, {tracer, serviceName, remoteServiceName}) { const instrumentation = new Instrumentation.HttpClient({tracer, serviceName, remoteServiceName}); return function zipkinfetch(input, opts = {}) { return new Promise((resolve, reject) => { tracer.scoped(() => { const url = (typeof input === 'string') ? input : input.url; const method = opts.method || 'GET'; const zipkinOpts = instrumentation.recordRequest(opts, url, method); const traceId = tracer.id; fetch(url, zipkinOpts).then((res) => { tracer.scoped(() => { instrumentation.recordResponse(traceId, res.status); }); resolve(res); }).catch((err) => { tracer.scoped(() => { instrumentation.recordError(traceId, err); }); reject(err); }); }); }); }; }
instrumentation.recordResponse(traceId, error.response.status); } else { instrumentation.recordError(traceId, error);
if (done) return; done = true; instrumentation.recordError(traceId, error); });
const ctx = getZipkinContext(err.gotOptions); tracer.scoped(() => { instrumentation.recordError(ctx.traceId, err); }); return err;