Skip to content

Commit

Permalink
fix(http-plugin): strip otel custom http header #983 (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed May 11, 2020
1 parent 45ea74a commit 35cda77
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/opentelemetry-plugin-http/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,12 @@ export class HttpPlugin extends BasePlugin<Http> {
extraOptions
);

if (utils.isOpenTelemetryRequest(optionsParsed)) {
delete optionsParsed.headers[utils.OT_REQUEST_HEADER];
return original.apply(this, [optionsParsed, ...args]);
}

if (
utils.isOpenTelemetryRequest(optionsParsed) ||
utils.isIgnored(
origin + pathname,
plugin._config.ignoreOutgoingUrls,
Expand Down
13 changes: 12 additions & 1 deletion packages/opentelemetry-plugin-http/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import { AttributeNames } from './enums/AttributeNames';
import * as url from 'url';
import { Socket } from 'net';

/**
* Specific header used by exporters to "mark" outgoing request to avoid creating
* spans for request that export them which would create a infinite loop.
*/
export const OT_REQUEST_HEADER = 'x-opentelemetry-outgoing-request';

export const HTTP_STATUS_SPECIAL_CASES: SpecialHttpStatusCodeMapping = {
Expand Down Expand Up @@ -298,9 +302,16 @@ export const isValidOptionsType = (options: unknown): boolean => {
* Use case: Typically, exporter `SpanExporter` can use http module to send spans.
* This will also generate spans (from the http-plugin) that will be sended through the exporter
* and here we have loop.
*
* TODO: Refactor this logic when a solution is found in
* https://github.com/open-telemetry/opentelemetry-specification/issues/530
*
*
* @param {RequestOptions} options
*/
export const isOpenTelemetryRequest = (options: RequestOptions) => {
export const isOpenTelemetryRequest = (
options: RequestOptions
): options is { headers: {} } & RequestOptions => {
return !!(options && options.headers && options.headers[OT_REQUEST_HEADER]);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ describe('HttpPlugin', () => {
};

const result = await httpRequest.get(options);
assert(
result.reqHeaders[OT_REQUEST_HEADER] === undefined,
'custom header should be stripped'
);
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(result.data, 'Ok');
assert.strictEqual(spans.length, 0);
Expand Down Expand Up @@ -293,6 +297,10 @@ describe('HttpPlugin', () => {
};

const result = await httpRequest.get(options);
assert(
result.reqHeaders[OT_REQUEST_HEADER] === undefined,
'custom header should be stripped'
);
const spans = memoryExporter.getFinishedSpans();
assert.strictEqual(result.data, 'Ok');
assert.strictEqual(spans.length, 0);
Expand Down

0 comments on commit 35cda77

Please sign in to comment.