|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -// NOTE: in other test the modules have already been required and cached (`GcpDetector.test.ts`) |
18 |
| -// so SDK cannot instrument them. We make sure we load fresh copies so RITM hook does its work. |
19 |
| -// we remove all the require chain: detector -> gcp-metadata -> gaxios -> node-fetch |
20 |
| -// but kepp other cache entries to not slow down the test more than necessary |
21 |
| -const modules = [ |
22 |
| - 'opentelemetry-resource-detector-gcp', |
23 |
| - 'gcp-metadata', |
24 |
| - 'gaxios', |
25 |
| - 'node-fetch', |
26 |
| -]; |
27 |
| -Object.keys(require.cache) |
28 |
| - .filter(path => modules.some(m => path.includes(m))) |
29 |
| - .forEach(key => { |
30 |
| - delete require.cache[key]; |
31 |
| - }); |
32 |
| - |
33 |
| -import * as assert from 'assert'; |
34 |
| - |
35 |
| -import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; |
36 | 17 | import {
|
37 |
| - InMemorySpanExporter, |
38 |
| - SimpleSpanProcessor, |
39 |
| -} from '@opentelemetry/sdk-trace-base'; |
40 |
| -import { NodeSDK } from '@opentelemetry/sdk-node'; |
41 |
| -import { detectResources } from '@opentelemetry/resources'; |
| 18 | + runTestFixture, |
| 19 | + TestCollector, |
| 20 | +} from '@opentelemetry/contrib-test-utils'; |
| 21 | +import * as assert from 'assert'; |
42 | 22 |
|
43 |
| -describe('[Integration] GcpDetector', () => { |
| 23 | +describe('[Integration] GcpDetector', async () => { |
44 | 24 | it('should not start spans for detector requests', async () => {
|
45 |
| - const memoryExporter = new InMemorySpanExporter(); |
46 |
| - const sdk = new NodeSDK({ |
47 |
| - instrumentations: [new HttpInstrumentation()], |
48 |
| - spanProcessors: [new SimpleSpanProcessor(memoryExporter)], |
| 25 | + await runTestFixture({ |
| 26 | + cwd: __dirname, |
| 27 | + argv: ['fixtures/detect-with-http-instrumentation.mjs'], |
| 28 | + env: { |
| 29 | + NODE_OPTIONS: |
| 30 | + '--experimental-loader=@opentelemetry/instrumentation/hook.mjs', |
| 31 | + METADATA_SERVER_DETECTION: 'assume-present', |
| 32 | + }, |
| 33 | + checkResult: (err, stdout, stderr) => { |
| 34 | + assert.ifError(err); |
| 35 | + }, |
| 36 | + checkCollector: (collector: TestCollector) => { |
| 37 | + assert.equal( |
| 38 | + collector.spans.length, |
| 39 | + 0, |
| 40 | + 'no spans exported for GcpDetector' |
| 41 | + ); |
| 42 | + }, |
49 | 43 | });
|
50 |
| - |
51 |
| - sdk.start(); |
52 |
| - |
53 |
| - process.env.METADATA_SERVER_DETECTION = 'assume-present'; |
54 |
| - |
55 |
| - // NOTE: detectors implementing the `DetectorSync` interface and starting |
56 |
| - // HTTP requests within the `detect` method will produce Noop Spans since |
57 |
| - // the SDK resolves the trace provider after resource detectors are triggered. |
58 |
| - // Ref: https://github.com/open-telemetry/opentelemetry-js/blob/38f6689480d28dcbdafcb7b5ba4b14025328ffda/experimental/packages/opentelemetry-sdk-node/src/sdk.ts#L210-L240 |
59 |
| - // |
60 |
| - // So having the detector in the config would result in no spans for Azure requests |
61 |
| - // being exported which is what we want. Although we may think we're safe of sending |
62 |
| - // internal tracing any change that delays these request will result in internal |
63 |
| - // tracing being exported. We do the detection outside the SDK constructor to have such |
64 |
| - // scenario. |
65 |
| - const { gcpDetector } = require('../../build/src/detectors/GcpDetector'); |
66 |
| - const resource = detectResources({ detectors: [gcpDetector] }); |
67 |
| - await resource.waitForAsyncAttributes?.(); |
68 |
| - |
69 |
| - // Wait for the next loop to let the span close properly |
70 |
| - await new Promise(r => setTimeout(r, 0)); |
71 |
| - const spans = memoryExporter.getFinishedSpans(); |
72 |
| - |
73 |
| - assert.equal(spans.length, 0, 'no spans exported for GcpDetector'); |
74 |
| - |
75 |
| - await sdk.shutdown(); |
76 | 44 | }).timeout(15000);
|
77 | 45 | });
|
0 commit comments