Skip to content

Commit

Permalink
fix(http): do not set outgoing http span as active in the context #1479
Browse files Browse the repository at this point in the history
… (#1546)
  • Loading branch information
vmarchaud authored Oct 13, 2020
1 parent bdd88f5 commit 4b5b023
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
30 changes: 17 additions & 13 deletions packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
SpanContext,
TraceFlags,
getExtractedSpanContext,
setActiveSpan,
} from '@opentelemetry/api';
import { BasePlugin, NoRecordingSpan } from '@opentelemetry/core';
import type {
Expand Down Expand Up @@ -410,21 +411,24 @@ export class HttpPlugin extends BasePlugin<Http> {
kind: SpanKind.CLIENT,
};
const span = plugin._startHttpSpan(operationName, spanOptions);
if (!optionsParsed.headers) {
optionsParsed.headers = {};
}
propagation.inject(
optionsParsed.headers,
undefined,
setActiveSpan(context.active(), span)
);

return plugin._tracer.withSpan(span, () => {
if (!optionsParsed.headers) optionsParsed.headers = {};
propagation.inject(optionsParsed.headers);

const request: ClientRequest = plugin._safeExecute(
span,
() => original.apply(this, [optionsParsed, ...args]),
true
);
const request: ClientRequest = plugin._safeExecute(
span,
() => original.apply(this, [optionsParsed, ...args]),
true
);

plugin._logger.debug('%s plugin outgoingRequest', plugin.moduleName);
plugin._tracer.bind(request);
return plugin._traceClientRequest(request, optionsParsed, span);
});
plugin._logger.debug('%s plugin outgoingRequest', plugin.moduleName);
plugin._tracer.bind(request);
return plugin._traceClientRequest(request, optionsParsed, span);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
propagation,
Span as ISpan,
SpanKind,
getActiveSpan,
} from '@opentelemetry/api';
import { NoopLogger } from '@opentelemetry/core';
import { NodeTracerProvider } from '@opentelemetry/node';
Expand Down Expand Up @@ -709,6 +710,14 @@ describe('HttpPlugin', () => {
SpanKind.CLIENT
);
});

it('should not set span as active in context for outgoing request', done => {
assert.deepStrictEqual(getActiveSpan(context.active()), undefined);
http.get(`${protocol}://${hostname}:${serverPort}/test`, res => {
assert.deepStrictEqual(getActiveSpan(context.active()), undefined);
done();
});
});
});

describe('with require parent span', () => {
Expand Down

0 comments on commit 4b5b023

Please sign in to comment.