Skip to content

Commit 70eb373

Browse files
authored
fix: Prevent instrumentation crash and fix the system test (#756)
1 parent 8eaf12a commit 70eb373

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

handwritten/logging-winston/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"precompile": "gts clean"
5151
},
5252
"dependencies": {
53-
"@google-cloud/logging": "^10.1.11",
53+
"@google-cloud/logging": "^10.2.2",
5454
"google-auth-library": "^8.0.2",
5555
"lodash.mapvalues": "^4.6.0",
5656
"winston-transport": "^4.3.0"

handwritten/logging-winston/src/common.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ export const LOGGING_SPAN_KEY = 'logging.googleapis.com/spanId';
8989
*/
9090
export const LOGGING_SAMPLED_KEY = 'logging.googleapis.com/trace_sampled';
9191

92+
/**
93+
* Default library version to be used if version retrieval fails
94+
*/
95+
export const NODEJS_WINSTON_DEFAULT_LIBRARY_VERSION = 'unknown';
96+
9297
// The variable to hold cached library version
9398
let libraryVersion: string;
9499

@@ -352,11 +357,15 @@ export function getNodejsLibraryVersion() {
352357
if (libraryVersion) {
353358
return libraryVersion;
354359
}
355-
libraryVersion = require(path.resolve(
356-
__dirname,
357-
'../../',
358-
'package.json'
359-
)).version;
360+
try {
361+
libraryVersion = require(path.resolve(
362+
__dirname,
363+
'../../',
364+
'package.json'
365+
)).version;
366+
} catch (err) {
367+
libraryVersion = NODEJS_WINSTON_DEFAULT_LIBRARY_VERSION;
368+
}
360369
return libraryVersion;
361370
}
362371

handwritten/logging-winston/system-test/logging-winston.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ describe('LoggingWinston', function () {
185185
entry.data[instrumentation.DIAGNOSTIC_INFO_KEY][
186186
instrumentation.INSTRUMENTATION_SOURCE_KEY
187187
];
188-
assert.equal(info[0].name, 'nodejs');
189-
assert.equal(info[1].name, 'nodejs-winston');
188+
assert.equal(info[0].name, 'nodejs-winston');
189+
assert.equal(info[1].name, 'nodejs');
190190
} else {
191191
const data = entry.data as {message: string};
192192
assert.strictEqual(data.message, ` ${MESSAGE}`);

0 commit comments

Comments
 (0)