Skip to content

Commit 80c2f1a

Browse files
authored
fix(detector-container): suppress internal tracing (#2430)
1 parent cb89486 commit 80c2f1a

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

detectors/node/opentelemetry-resource-detector-container/src/detectors/ContainerDetector.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import { SEMRESATTRS_CONTAINER_ID } from '@opentelemetry/semantic-conventions';
2525

2626
import * as fs from 'fs';
2727
import * as util from 'util';
28-
import { diag } from '@opentelemetry/api';
28+
import { context, diag } from '@opentelemetry/api';
29+
import { suppressTracing } from '@opentelemetry/core';
2930
import { extractContainerIdFromLine } from './utils';
3031

3132
export class ContainerDetector implements DetectorSync {
@@ -43,7 +44,10 @@ export class ContainerDetector implements DetectorSync {
4344
private static readFileAsync = util.promisify(fs.readFile);
4445

4546
detect(_config?: ResourceDetectionConfig): IResource {
46-
return new Resource({}, this._getAttributes());
47+
const attributes = context.with(suppressTracing(context.active()), () =>
48+
this._getAttributes()
49+
);
50+
return new Resource({}, attributes);
4751
}
4852

4953
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as assert from 'assert';
18+
19+
import { FsInstrumentation } from '@opentelemetry/instrumentation-fs';
20+
import {
21+
InMemorySpanExporter,
22+
SimpleSpanProcessor,
23+
} from '@opentelemetry/sdk-trace-base';
24+
import { NodeSDK } from '@opentelemetry/sdk-node';
25+
import { IResource } from '@opentelemetry/resources';
26+
27+
describe('[Integration] ContainerDetector', () => {
28+
it('should not start spans for detector reads to filesystem', async () => {
29+
const memoryExporter = new InMemorySpanExporter();
30+
const sdk = new NodeSDK({
31+
instrumentations: [new FsInstrumentation()],
32+
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
33+
});
34+
35+
sdk.start();
36+
37+
// NOTE: detectors implementing the `DetectorSync` interface and starting
38+
// HTTP requests within the `detect` method will produce Noop Spans since
39+
// the SDK resolves the trace provider after resource detectors are triggered.
40+
// Ref: https://github.com/open-telemetry/opentelemetry-js/blob/38f6689480d28dcbdafcb7b5ba4b14025328ffda/experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L210-L240
41+
//
42+
// So having the detector in the config would result in no spans for Azure requests
43+
// being exported which is what we want. Although we may think we're safe of sending
44+
// internal tracing any change that delays these request will result in internal
45+
// tracing being exported. We do the detection outside the SDK constructor to have such
46+
// scenario.
47+
const {
48+
containerDetector,
49+
} = require('../build/src/detectors/ContainerDetector');
50+
51+
// NOTE: the require process makes use of the fs API so spans are being exported.
52+
// We need to check no new spans are exported when `detect` is called.
53+
await new Promise(r => setTimeout(r, 0));
54+
const numSpansAfterRequire = memoryExporter.getFinishedSpans().length;
55+
56+
const resource = containerDetector.detect() as IResource;
57+
await resource.waitForAsyncAttributes?.();
58+
59+
// Wait for the next loop to let the span close properly
60+
await new Promise(r => setTimeout(r, 0));
61+
const spans = memoryExporter.getFinishedSpans();
62+
63+
assert.equal(
64+
spans.length,
65+
numSpansAfterRequire,
66+
'no spans exported for ContainerDetector'
67+
);
68+
69+
await sdk.shutdown();
70+
});
71+
});

0 commit comments

Comments
 (0)