Skip to content

Commit

Permalink
fix: do not set outgoing http span as active in the context #1479
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Sep 19, 2020
1 parent 3309ea4 commit a88ceff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 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 @@ -28,6 +28,7 @@ import {
BasePlugin,
NoRecordingSpan,
getExtractedSpanContext,
setActiveSpan,
} from '@opentelemetry/core';
import {
ClientRequest,
Expand Down Expand Up @@ -422,21 +423,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 @@ -20,7 +20,10 @@ import {
Span as ISpan,
SpanKind,
} from '@opentelemetry/api';
import { NoopLogger } from '@opentelemetry/core';
import {
NoopLogger,
getActiveSpan,
} from '@opentelemetry/core';
import { NodeTracerProvider } from '@opentelemetry/node';
import {
InMemorySpanExporter,
Expand Down Expand Up @@ -751,6 +754,14 @@ describe('HttpPlugin', () => {
SpanKind.CLIENT
);
});

it('should not set span as active in context for outgoing request', async () => {
assert.deepStrictEqual(getActiveSpan(context.active()), undefined);
await httpRequest.get(`${protocol}://${hostname}:${serverPort}/test`);
assert.deepStrictEqual(getActiveSpan(context.active()), undefined);
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(spans.length, 2);
});
});

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

0 comments on commit a88ceff

Please sign in to comment.