Skip to content

Commit

Permalink
fix(detector-azure): suppress tracing for AzureVmDetector (#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-luna authored Aug 7, 2024
1 parent 1991aed commit 5952127
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@opentelemetry/api": "^1.0.0"
},
"dependencies": {
"@opentelemetry/core": "^1.25.1",
"@opentelemetry/resources": "^1.10.1",
"@opentelemetry/semantic-conventions": "^1.22.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/

import * as http from 'http';

import { context } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';
import {
DetectorSync,
IResource,
Expand Down Expand Up @@ -47,7 +50,10 @@ import {
*/
class AzureVmResourceDetector implements DetectorSync {
detect(): IResource {
return new Resource({}, this.getAzureVmMetadata());
const attributes = context.with(suppressTracing(context.active()), () =>
this.getAzureVmMetadata()
);
return new Resource({}, attributes);
}

async getAzureVmMetadata(): Promise<ResourceAttributes> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
* limitations under the License.
*/

export * from './AzureAppServiceDetector';
export * from './AzureFunctionsDetector';
export * from './AzureVmDetector';
export { azureAppServiceDetector } from './AzureAppServiceDetector';
export { azureFunctionsDetector } from './AzureFunctionsDetector';
export { azureVmDetector } from './AzureVmDetector';
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
* limitations under the License.
*/

export * from './detectors';
export {
azureAppServiceDetector,
azureFunctionsDetector,
azureVmDetector,
} from './detectors';
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ const AZURE_VM_METADATA_HOST = 'http://169.254.169.254';
const AZURE_VM_METADATA_PATH =
'/metadata/instance/compute?api-version=2021-12-13&format=json';

describe('AzureAppServiceDetector', () => {
describe('AzureVmServiceDetector', () => {
beforeEach(() => {
nock.disableNetConnect();
nock.cleanAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';

import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { NodeSDK } from '@opentelemetry/sdk-node';

describe('[Integration] AzureVmServiceDetector', () => {
it('should not start spans for detector requests', async () => {
const memoryExporter = new InMemorySpanExporter();
const sdk = new NodeSDK({
instrumentations: [new HttpInstrumentation()],
spanProcessors: [new SimpleSpanProcessor(memoryExporter)],
});

sdk.start();

// NOTE: detectors implementing the `DetectorSync` interface and starting
// HTTP requests within the `detect` method will produce Noop Spans since
// the SDK resolves the trace provider after resource detectors are triggered.
// Ref: https://github.com/open-telemetry/opentelemetry-js/blob/38f6689480d28dcbdafcb7b5ba4b14025328ffda/experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L210-L240
//
// So having the detector in the config would result in no spans for Azure requests
// being exported which is what we want. Although we may think we're safe of sending
// internal tracing any change that delays these request will result in internal
// tracing being exported. We do the detection outside the SDK constructor to have such
// scenario.
const {
azureVmDetector,
} = require('../../build/src/detectors/AzureVmDetector');
const resource = azureVmDetector.detect();
await resource.waitForAsyncAttributes?.();

// Wait for the next loop to let the span close properly
await new Promise(r => setTimeout(r, 0));
const spans = memoryExporter.getFinishedSpans();

assert.equal(spans.length, 0, 'no spans exported for AzureVmDetector');

await sdk.shutdown();
});
});
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5952127

Please sign in to comment.