|
| 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