Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35cda77

Browse files
authored
fix(http-plugin): strip otel custom http header open-telemetry#983 (open-telemetry#984)
1 parent 45ea74a commit 35cda77

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

packages/opentelemetry-plugin-http/src/http.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,12 @@ export class HttpPlugin extends BasePlugin<Http> {
392392
extraOptions
393393
);
394394

395+
if (utils.isOpenTelemetryRequest(optionsParsed)) {
396+
delete optionsParsed.headers[utils.OT_REQUEST_HEADER];
397+
return original.apply(this, [optionsParsed, ...args]);
398+
}
399+
395400
if (
396-
utils.isOpenTelemetryRequest(optionsParsed) ||
397401
utils.isIgnored(
398402
origin + pathname,
399403
plugin._config.ignoreOutgoingUrls,

packages/opentelemetry-plugin-http/src/utils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import { AttributeNames } from './enums/AttributeNames';
3333
import * as url from 'url';
3434
import { Socket } from 'net';
3535

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

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

packages/opentelemetry-plugin-http/test/functionals/http-enable.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ describe('HttpPlugin', () => {
180180
};
181181

182182
const result = await httpRequest.get(options);
183+
assert(
184+
result.reqHeaders[OT_REQUEST_HEADER] === undefined,
185+
'custom header should be stripped'
186+
);
183187
const spans = memoryExporter.getFinishedSpans();
184188
assert.strictEqual(result.data, 'Ok');
185189
assert.strictEqual(spans.length, 0);
@@ -293,6 +297,10 @@ describe('HttpPlugin', () => {
293297
};
294298

295299
const result = await httpRequest.get(options);
300+
assert(
301+
result.reqHeaders[OT_REQUEST_HEADER] === undefined,
302+
'custom header should be stripped'
303+
);
296304
const spans = memoryExporter.getFinishedSpans();
297305
assert.strictEqual(result.data, 'Ok');
298306
assert.strictEqual(spans.length, 0);

0 commit comments

Comments
 (0)