Skip to content

Commit 0414fb3

Browse files
fix: dont trace ourselves
closes open-telemetry#332 Signed-off-by: Olivier Albertini <[email protected]>
1 parent a0140ec commit 0414fb3

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

packages/opentelemetry-exporter-zipkin/src/zipkin.ts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class ZipkinExporter implements SpanExporter {
5656
method: 'POST',
5757
headers: {
5858
'Content-Type': 'application/json',
59+
'x-ot-request': 1,
5960
},
6061
},
6162
urlOpts

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

+2
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,11 @@ export class HttpPlugin extends BasePlugin<Http> {
346346
? (args.shift() as RequestOptions)
347347
: undefined
348348
);
349+
349350
options = optionsParsed;
350351

351352
if (
353+
Utils.isOpenTelemetryRequest(options) ||
352354
Utils.isIgnored(origin + pathname, plugin._config.ignoreOutgoingUrls)
353355
) {
354356
return original.apply(this, [options, ...args]);

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

+11
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,15 @@ export class Utils {
242242
const type = typeof options;
243243
return type === 'string' || (type === 'object' && !Array.isArray(options));
244244
}
245+
246+
/**
247+
* Check whether the given request should be ignored
248+
* Use case: Typically, exporter `SpanExporter` can use http module to send spans.
249+
* This will also generate spans (from the http-plugin) that will be sended through the exporter
250+
* and here we have loop.
251+
* @param {RequestOptions} options
252+
*/
253+
static isOpenTelemetryRequest(options: RequestOptions) {
254+
return options && options.headers && !!options.headers['x-ot-request'];
255+
}
245256
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ describe('HttpPlugin', () => {
125125
assertSpan(outgoingSpan, SpanKind.CLIENT, validations);
126126
});
127127

128+
it("should not trace requests with 'x-ot-request' header", async () => {
129+
const testPath = '/outgoing/do-not-trace';
130+
doNock(hostname, testPath, 200, 'Ok');
131+
132+
const options = {
133+
host: hostname,
134+
path: testPath,
135+
headers: { 'x-ot-request': 1 },
136+
};
137+
138+
const result = await httpRequest.get(options);
139+
const spans = memoryExporter.getFinishedSpans();
140+
assert.strictEqual(result.data, 'Ok');
141+
assert.strictEqual(spans.length, 0);
142+
});
143+
128144
const httpErrorCodes = [400, 401, 403, 404, 429, 501, 503, 504, 500];
129145

130146
for (let i = 0; i < httpErrorCodes.length; i++) {

0 commit comments

Comments
 (0)