Skip to content

Commit 5952127

Browse files
authored
fix(detector-azure): suppress tracing for AzureVmDetector (#2371)
1 parent 1991aed commit 5952127

File tree

7 files changed

+79
-6
lines changed

7 files changed

+79
-6
lines changed

detectors/node/opentelemetry-resource-detector-azure/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@opentelemetry/api": "^1.0.0"
4848
},
4949
"dependencies": {
50+
"@opentelemetry/core": "^1.25.1",
5051
"@opentelemetry/resources": "^1.10.1",
5152
"@opentelemetry/semantic-conventions": "^1.22.0"
5253
},

detectors/node/opentelemetry-resource-detector-azure/src/detectors/AzureVmDetector.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616

1717
import * as http from 'http';
18+
19+
import { context } from '@opentelemetry/api';
20+
import { suppressTracing } from '@opentelemetry/core';
1821
import {
1922
DetectorSync,
2023
IResource,
@@ -47,7 +50,10 @@ import {
4750
*/
4851
class AzureVmResourceDetector implements DetectorSync {
4952
detect(): IResource {
50-
return new Resource({}, this.getAzureVmMetadata());
53+
const attributes = context.with(suppressTracing(context.active()), () =>
54+
this.getAzureVmMetadata()
55+
);
56+
return new Resource({}, attributes);
5157
}
5258

5359
async getAzureVmMetadata(): Promise<ResourceAttributes> {

detectors/node/opentelemetry-resource-detector-azure/src/detectors/index.ts

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

17-
export * from './AzureAppServiceDetector';
18-
export * from './AzureFunctionsDetector';
19-
export * from './AzureVmDetector';
17+
export { azureAppServiceDetector } from './AzureAppServiceDetector';
18+
export { azureFunctionsDetector } from './AzureFunctionsDetector';
19+
export { azureVmDetector } from './AzureVmDetector';

detectors/node/opentelemetry-resource-detector-azure/src/index.ts

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

17-
export * from './detectors';
17+
export {
18+
azureAppServiceDetector,
19+
azureFunctionsDetector,
20+
azureVmDetector,
21+
} from './detectors';

detectors/node/opentelemetry-resource-detector-azure/test/detectors/AzureVmDetector.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ const AZURE_VM_METADATA_HOST = 'http://169.254.169.254';
372372
const AZURE_VM_METADATA_PATH =
373373
'/metadata/instance/compute?api-version=2021-12-13&format=json';
374374

375-
describe('AzureAppServiceDetector', () => {
375+
describe('AzureVmServiceDetector', () => {
376376
beforeEach(() => {
377377
nock.disableNetConnect();
378378
nock.cleanAll();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
26+
describe('[Integration] AzureVmServiceDetector', () => {
27+
it('should not start spans for detector requests', async () => {
28+
const memoryExporter = new InMemorySpanExporter();
29+
const sdk = new NodeSDK({
30+
instrumentations: [new HttpInstrumentation()],
31+
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
32+
});
33+
34+
sdk.start();
35+
36+
// NOTE: detectors implementing the `DetectorSync` interface and starting
37+
// HTTP requests within the `detect` method will produce Noop Spans since
38+
// the SDK resolves the trace provider after resource detectors are triggered.
39+
// Ref: https://github.com/open-telemetry/opentelemetry-js/blob/38f6689480d28dcbdafcb7b5ba4b14025328ffda/experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L210-L240
40+
//
41+
// So having the detector in the config would result in no spans for Azure requests
42+
// being exported which is what we want. Although we may think we're safe of sending
43+
// internal tracing any change that delays these request will result in internal
44+
// tracing being exported. We do the detection outside the SDK constructor to have such
45+
// scenario.
46+
const {
47+
azureVmDetector,
48+
} = require('../../build/src/detectors/AzureVmDetector');
49+
const resource = azureVmDetector.detect();
50+
await resource.waitForAsyncAttributes?.();
51+
52+
// Wait for the next loop to let the span close properly
53+
await new Promise(r => setTimeout(r, 0));
54+
const spans = memoryExporter.getFinishedSpans();
55+
56+
assert.equal(spans.length, 0, 'no spans exported for AzureVmDetector');
57+
58+
await sdk.shutdown();
59+
});
60+
});

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)