Skip to content

Commit 7c90935

Browse files
committed
fix: lookup of own package.json in published package
In the published package, the path to our own package.json file (from which we read the version for the resource attribute telemetry.distro.version) is different compared to development.
1 parent ff96aa8 commit 7c90935

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

src/init.ts

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,43 @@ import { NodeSDK, NodeSDKConfiguration } from '@opentelemetry/sdk-node';
1414
import { BatchSpanProcessor, SpanProcessor } from '@opentelemetry/sdk-trace-base';
1515
import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace-node';
1616

17-
import { version } from '../package.json';
18-
1917
import PodUidDetector from './detectors/node/opentelemetry-resource-detector-kubernetes-pod';
2018
import ServiceNameFallbackDetector from './detectors/node/opentelemetry-resource-detector-service-name-fallback';
2119
import { FileSpanExporter } from './util/FileSpanExporter';
2220
import { hasOptedIn, hasOptedOut, parseNumericEnvironmentVariableWithDefault } from './util/environment';
2321

22+
const logPrefix = 'Dash0 OpenTelemetry distribution for Node.js:';
2423
const debugOutput = hasOptedIn('DASH0_DEBUG');
2524

26-
printDebugOutput('Dash0 OpenTelemetry distribution for Node.js: Starting NodeSDK.');
25+
let packageJson;
26+
let version: string;
27+
try {
28+
// In the published package, the transpiled JS files are in dist/src/init.js, therefore the relative path to
29+
// package.json is two levels above the directory of init.js.
30+
packageJson = require('../../package.json');
31+
} catch (e1) {
32+
try {
33+
// In development, the directory is just src, therefore the relative path to package.json is only one level above.
34+
packageJson = require('../package.json');
35+
} catch (e2) {
36+
printDebugStderr(
37+
'Unable to find our own package.json file, will not transmit telemetry.distro.version. This warning can be safely ignored.',
38+
e1,
39+
e1,
40+
);
41+
}
42+
}
43+
// Since we read the package.json from two possible relative paths, we are extra-careful to make sure we actually are
44+
// reading from our own package.json file, hence the name check.
45+
if (packageJson && packageJson.name === '@dash0hq/opentelemetry') {
46+
version = packageJson.version;
47+
} else {
48+
printDebugStderr(
49+
`Unexpected package name in our own package.json: ${packageJson.name}, will not transmit telemetry.distro.version. This warning can be safely ignored.`,
50+
);
51+
}
52+
53+
printDebugStdout('Starting NodeSDK.');
2754

2855
let sdkShutdownHasBeenCalled = false;
2956

@@ -48,7 +75,7 @@ sdk.start();
4875
createBootstrapSpanIfRequested();
4976
installProcessExitHandlers();
5077

51-
printDebugOutput('Dash0 OpenTelemetry distribution for Node.js: NodeSDK started.');
78+
printDebugStdout('NodeSDK started.');
5279

5380
function spanProcessors(): SpanProcessor[] {
5481
const spanProcessors: SpanProcessor[] = [
@@ -105,10 +132,13 @@ function createInstrumentationConfig(): any {
105132
}
106133

107134
function resource() {
108-
return new Resource({
135+
const distroResourceAttributes: any = {
109136
'telemetry.distro.name': 'dash0-nodejs',
110-
'telemetry.distro.version': version,
111-
});
137+
};
138+
if (version) {
139+
distroResourceAttributes['telemetry.distro.version'] = version;
140+
}
141+
return new Resource(distroResourceAttributes);
112142
}
113143

114144
function resourceDetectors(): (Detector | DetectorSync)[] {
@@ -165,11 +195,9 @@ async function gracefulSdkShutdown(callProcessExit: boolean) {
165195
sdkShutdownHasBeenCalled = true;
166196
await sdk.shutdown();
167197

168-
printDebugOutput(
169-
'Dash0 OpenTelemetry distribution for Node.js: OpenTelemetry SDK has been shut down successfully.',
170-
);
198+
printDebugStdout('OpenTelemetry SDK has been shut down successfully.');
171199
} catch (err) {
172-
console.error('Dash0 OpenTelemetry distribution for Node.js: Error shutting down the OpenTelemetry SDK:', err);
200+
console.error(logPrefix, 'Error shutting down the OpenTelemetry SDK:', err);
173201
} finally {
174202
if (callProcessExit) {
175203
process.exit(0);
@@ -199,8 +227,14 @@ function executePromiseWithTimeout(promise: Promise<any>, timeoutMillis: number,
199227
});
200228
}
201229

202-
function printDebugOutput(message: string) {
230+
function printDebugStdout(message: string, ...additional: any[]) {
231+
if (debugOutput) {
232+
console.log(logPrefix, message, ...additional);
233+
}
234+
}
235+
236+
function printDebugStderr(message: string, ...additional: any[]) {
203237
if (debugOutput) {
204-
console.log(message);
238+
console.error(logPrefix, message, ...additional);
205239
}
206240
}

0 commit comments

Comments
 (0)