Skip to content

Commit fcf7fe6

Browse files
committed
fix: do not assume a default value for DASH0_OTEL_COLLECTOR_BASE_URL
1 parent 77dfa60 commit fcf7fe6

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ function init() {
3737
}
3838
}
3939

40-
if (process.env.DASH0_DISABLE == null || process.env.DASH0_DISABLE.toLowerCase() !== 'true') {
41-
init();
42-
} else {
40+
if (process.env.DASH0_DISABLE != null && process.env.DASH0_DISABLE.toLowerCase() === 'true') {
4341
logProhibitiveError(`The distribution has been disabled by setting DASH0_DISABLE=${process.env.DASH0_DISABLE}.`);
42+
} else if (process.env.DASH0_OTEL_COLLECTOR_BASE_URL == null) {
43+
logProhibitiveError(`DASH0_OTEL_COLLECTOR_BASE_URL is not set.`);
44+
} else {
45+
init();
4446
}
4547

4648
function logProhibitiveError(message: string) {

src/init.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ printDebugStdout('Starting NodeSDK.');
5757

5858
let sdkShutdownHasBeenCalled = false;
5959

60-
let baseUrl = 'http://dash0-operator-opentelemetry-collector.dash0-operator-system.svc.cluster.local:4318';
61-
if (process.env.DASH0_OTEL_COLLECTOR_BASE_URL) {
62-
baseUrl = process.env.DASH0_OTEL_COLLECTOR_BASE_URL;
63-
}
60+
// Note: There is a check in index.ts that this env var is set.
61+
const baseUrl = process.env.DASH0_OTEL_COLLECTOR_BASE_URL;
6462

6563
const configuration: Partial<NodeSDKConfiguration> = {
6664
spanProcessors: spanProcessors(),

test/integration/ChildProcessWrapper.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const emulateKubernetesPath = path.join(repoRoot, 'test', 'integration', 'emulat
2626
export default class ChildProcessWrapper {
2727
private childProcess?: ChildProcess;
2828
private ready: boolean;
29+
private terminated: boolean;
2930
private options: ChildProcessWrapperOptions;
3031
private nextIpcRequestId: number;
3132
private responseEmitter: ResponseEmitter;
@@ -36,6 +37,7 @@ export default class ChildProcessWrapper {
3637
this.options.label = this.options.path;
3738
}
3839
this.ready = false;
40+
this.terminated = false;
3941
this.nextIpcRequestId = 0;
4042
this.responseEmitter = new ResponseEmitter();
4143
}
@@ -44,6 +46,9 @@ export default class ChildProcessWrapper {
4446
const { modulePath, forkOptions } = await this.createForkOptions();
4547
this.childProcess = fork(modulePath, this.options.args ?? [], forkOptions);
4648
this.listenToIpcMessages();
49+
this.childProcess.on('exit', () => {
50+
this.terminated = true;
51+
});
4752
this.echoOutputStreams();
4853
await this.waitUntilReady();
4954
}
@@ -119,6 +124,14 @@ export default class ChildProcessWrapper {
119124
}, this.options.waitForReadyRetryOptions);
120125
}
121126

127+
async waitUntilTerminated() {
128+
await waitUntil(() => {
129+
if (!this.terminated) {
130+
throw new Error('Child process has not terminated yet.');
131+
}
132+
}, this.options.waitForReadyRetryOptions);
133+
}
134+
122135
async stop(signal?: number | NodeJS.Signals): Promise<void> {
123136
if (!this.childProcess) {
124137
return;

test/integration/test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,36 @@ describe('attach', () => {
314314
});
315315
});
316316

317+
describe('timeout for flushing telemetry', () => {
318+
let appUnderTest: ChildProcessWrapper;
319+
320+
beforeEach(async () => {
321+
const appConfiguration = {
322+
path: 'test/apps/empty-event-loop',
323+
label: 'app',
324+
useTsNode: true,
325+
useDistro: true,
326+
env: {
327+
...process.env,
328+
// Weird finding: When setting DASH0_OTEL_COLLECTOR_BASE_URL to any url where the host name ends in .local
329+
// (like http://non-reachable-dns-name.local:4318), the timeout of 500 ms for flushing telemetry is ignored
330+
// because the DNS lookup blocks for 5 seconds. This apparently happens on Mac as well as on Linux/in Docker.
331+
DASH0_OTEL_COLLECTOR_BASE_URL: 'http://non-reachable-host.url:4318',
332+
DASH0_BOOTSTRAP_SPAN: 'Dash0 Test Bootstrap Span',
333+
},
334+
};
335+
appUnderTest = new ChildProcessWrapper(appConfiguration);
336+
});
337+
338+
it('should let process terminate after timeout', async () => {
339+
await appUnderTest.start();
340+
const startedAt = Date.now();
341+
await appUnderTest.waitUntilTerminated();
342+
const terminatedAt = Date.now();
343+
expect(terminatedAt - startedAt).to.be.lessThan(1000);
344+
});
345+
});
346+
317347
describe('print spans to file', () => {
318348
let appUnderTest: ChildProcessWrapper;
319349
const spanFilename = join(__dirname, 'spans.json');
@@ -406,6 +436,31 @@ describe('attach', () => {
406436
});
407437
});
408438

439+
describe('disable when export endpoint is not set', () => {
440+
let appUnderTest: ChildProcessWrapper;
441+
442+
before(async () => {
443+
const appConfiguration = defaultAppConfiguration(appPort);
444+
delete appConfiguration.env!.DASH0_OTEL_COLLECTOR_BASE_URL;
445+
appUnderTest = new ChildProcessWrapper(appConfiguration);
446+
await appUnderTest.start();
447+
});
448+
449+
after(async () => {
450+
await appUnderTest.stop();
451+
});
452+
453+
it('should do nothing if DASH0_OTEL_COLLECTOR_BASE_URL is not set', async () => {
454+
await delay(1000);
455+
await sendHttpRequestAndVerifyResponse();
456+
await delay(2000);
457+
458+
if (await collector().hasTelemetry()) {
459+
fail('The collector received telemetry data although it should not have received anything.');
460+
}
461+
});
462+
});
463+
409464
async function sendHttpRequestAndFetchTraceData() {
410465
await sendHttpRequestAndVerifyResponse();
411466
return collector().fetchTraces();

0 commit comments

Comments
 (0)