File tree Expand file tree Collapse file tree 3 files changed +25
-2
lines changed
packages/opentelemetry-plugin-http Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -392,8 +392,12 @@ export class HttpPlugin extends BasePlugin<Http> {
392
392
extraOptions
393
393
) ;
394
394
395
+ if ( utils . isOpenTelemetryRequest ( optionsParsed ) ) {
396
+ delete optionsParsed . headers [ utils . OT_REQUEST_HEADER ] ;
397
+ return original . apply ( this , [ optionsParsed , ...args ] ) ;
398
+ }
399
+
395
400
if (
396
- utils . isOpenTelemetryRequest ( optionsParsed ) ||
397
401
utils . isIgnored (
398
402
origin + pathname ,
399
403
plugin . _config . ignoreOutgoingUrls ,
Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ import { AttributeNames } from './enums/AttributeNames';
33
33
import * as url from 'url' ;
34
34
import { Socket } from 'net' ;
35
35
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
+ */
36
40
export const OT_REQUEST_HEADER = 'x-opentelemetry-outgoing-request' ;
37
41
38
42
export const HTTP_STATUS_SPECIAL_CASES : SpecialHttpStatusCodeMapping = {
@@ -298,9 +302,16 @@ export const isValidOptionsType = (options: unknown): boolean => {
298
302
* Use case: Typically, exporter `SpanExporter` can use http module to send spans.
299
303
* This will also generate spans (from the http-plugin) that will be sended through the exporter
300
304
* 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
+ *
301
310
* @param {RequestOptions } options
302
311
*/
303
- export const isOpenTelemetryRequest = ( options : RequestOptions ) => {
312
+ export const isOpenTelemetryRequest = (
313
+ options : RequestOptions
314
+ ) : options is { headers : { } } & RequestOptions => {
304
315
return ! ! ( options && options . headers && options . headers [ OT_REQUEST_HEADER ] ) ;
305
316
} ;
306
317
Original file line number Diff line number Diff line change @@ -180,6 +180,10 @@ describe('HttpPlugin', () => {
180
180
} ;
181
181
182
182
const result = await httpRequest . get ( options ) ;
183
+ assert (
184
+ result . reqHeaders [ OT_REQUEST_HEADER ] === undefined ,
185
+ 'custom header should be stripped'
186
+ ) ;
183
187
const spans = memoryExporter . getFinishedSpans ( ) ;
184
188
assert . strictEqual ( result . data , 'Ok' ) ;
185
189
assert . strictEqual ( spans . length , 0 ) ;
@@ -293,6 +297,10 @@ describe('HttpPlugin', () => {
293
297
} ;
294
298
295
299
const result = await httpRequest . get ( options ) ;
300
+ assert (
301
+ result . reqHeaders [ OT_REQUEST_HEADER ] === undefined ,
302
+ 'custom header should be stripped'
303
+ ) ;
296
304
const spans = memoryExporter . getFinishedSpans ( ) ;
297
305
assert . strictEqual ( result . data , 'Ok' ) ;
298
306
assert . strictEqual ( spans . length , 0 ) ;
You can’t perform that action at this time.
0 commit comments