Skip to content

Commit 9a7690e

Browse files
authored
fix(detector-alibaba): suppres internal tracing (#2429)
1 parent 3ee6f09 commit 9a7690e

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

detectors/node/opentelemetry-resource-detector-alibaba-cloud/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@opentelemetry/api": "^1.0.0"
5757
},
5858
"dependencies": {
59-
"@opentelemetry/resources": "^1.0.0",
59+
"@opentelemetry/resources": "^1.10.0",
6060
"@opentelemetry/semantic-conventions": "^1.27.0"
6161
},
6262
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/detectors/node/opentelemetry-resource-detector-alibaba-cloud#readme"

detectors/node/opentelemetry-resource-detector-alibaba-cloud/src/detectors/AlibabaCloudEcsDetector.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { context } from '@opentelemetry/api';
18+
import { suppressTracing } from '@opentelemetry/core';
1719
import {
1820
DetectorSync,
1921
IResource,
@@ -61,7 +63,10 @@ class AlibabaCloudEcsDetector implements DetectorSync {
6163
* @param config (unused) The resource detection config
6264
*/
6365
detect(_config?: ResourceDetectionConfig): IResource {
64-
return new Resource({}, this._getAttributes());
66+
const attributes = context.with(suppressTracing(context.active()), () =>
67+
this._getAttributes()
68+
);
69+
return new Resource({}, attributes);
6570
}
6671

6772
/** Gets identity and host info and returns them as attribs. Empty object if fails */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
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] AlibabaCloudEcsDetector', () => {
28+
it('should not start spans for detector requests', async () => {
29+
const memoryExporter = new InMemorySpanExporter();
30+
const sdk = new NodeSDK({
31+
instrumentations: [new HttpInstrumentation()],
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 Alibaba 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+
alibabaCloudEcsDetector,
49+
} = require('../../build/src/detectors/AlibabaCloudEcsDetector');
50+
const resource = alibabaCloudEcsDetector.detect() as IResource;
51+
await resource.waitForAsyncAttributes?.();
52+
53+
// Wait for the next loop to let the span close properly
54+
await new Promise(r => setTimeout(r, 0));
55+
const spans = memoryExporter.getFinishedSpans();
56+
57+
assert.equal(spans.length, 0, 'no spans exported for GcpDetector');
58+
59+
await sdk.shutdown();
60+
});
61+
});

0 commit comments

Comments
 (0)