Skip to content

Commit 0b6a515

Browse files
authored
test(gcp-detector): properly isolate GcpDetectorIntegration.test.ts (#3005)
1 parent 35c7754 commit 0b6a515

File tree

2 files changed

+57
-55
lines changed

2 files changed

+57
-55
lines changed

packages/resource-detector-gcp/test/detectors/GcpDetectorIntegration.test.ts

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

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';
3617
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';
4222

43-
describe('[Integration] GcpDetector', () => {
23+
describe('[Integration] GcpDetector', async () => {
4424
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+
},
4943
});
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();
7644
}).timeout(15000);
7745
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
17+
import { NodeSDK } from '@opentelemetry/sdk-node';
18+
import { detectResources } from '@opentelemetry/resources';
19+
20+
const sdk = new NodeSDK({
21+
instrumentations: [new HttpInstrumentation()],
22+
});
23+
sdk.start();
24+
25+
// Defer import until after instrumentation is added
26+
const { gcpDetector } = await import(
27+
'../../../build/src/detectors/GcpDetector.js'
28+
);
29+
const resource = detectResources({ detectors: [gcpDetector] });
30+
await resource.waitForAsyncAttributes?.();
31+
32+
console.log(resource);
33+
34+
await sdk.shutdown();

0 commit comments

Comments
 (0)