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

fix(http-plugin): strip otel custom http header #983 #984

Merged
merged 1 commit into from
May 11, 2020
Merged
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
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