Skip to content
This repository was archived by the owner on Mar 11, 2026. It is now read-only.

Commit ec40231

Browse files
authored
fix: Runtime package.json check causes breakage when bundled (#1364)
* fix: Runtime package.json check causes breakage when bundled * Correct anoner place to return NODEJS_DEFAULT_LIBRARY_VERSION
1 parent 46e57df commit ec40231

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/utils/instrumentation.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const maxDiagnosticValueLen = 14;
3838
export const DIAGNOSTIC_INFO_KEY = 'logging.googleapis.com/diagnostic';
3939
export const INSTRUMENTATION_SOURCE_KEY = 'instrumentation_source';
4040
export const NODEJS_LIBRARY_NAME_PREFIX = 'nodejs';
41+
export const NODEJS_DEFAULT_LIBRARY_VERSION = 'unknown';
4142
export const MAX_INSTRUMENTATION_COUNT = 3;
4243
export type InstrumentationInfo = {name: string; version: string};
4344

@@ -174,7 +175,7 @@ function truncateValue(value: object | string, maxLen: number) {
174175
}
175176
// Return 'unknown' if version cannot be retrieved
176177
if (typeof value !== 'string') {
177-
return 'unknown';
178+
return NODEJS_DEFAULT_LIBRARY_VERSION;
178179
}
179180
if (value && value.length > maxLen) {
180181
return value.substring(0, maxLen).concat('*');
@@ -191,11 +192,15 @@ export function getNodejsLibraryVersion() {
191192
if (libraryVersion) {
192193
return libraryVersion;
193194
}
194-
libraryVersion = require(path.resolve(
195-
__dirname,
196-
'../../../',
197-
'package.json'
198-
)).version;
195+
try {
196+
libraryVersion = require(path.resolve(
197+
__dirname,
198+
'../../../',
199+
'package.json'
200+
)).version;
201+
} catch (err) {
202+
libraryVersion = NODEJS_DEFAULT_LIBRARY_VERSION;
203+
}
199204
return libraryVersion;
200205
}
201206

0 commit comments

Comments
 (0)