Skip to content

Commit

Permalink
fix: Prevent instrumentation crash and fix the system test (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
losalex committed Nov 1, 2022
1 parent 0acceba commit fdb6b26
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"precompile": "gts clean"
},
"dependencies": {
"@google-cloud/logging": "^10.1.11",
"@google-cloud/logging": "^10.2.2",
"google-auth-library": "^8.0.2",
"lodash.mapvalues": "^4.6.0",
"winston-transport": "^4.3.0"
Expand Down
19 changes: 14 additions & 5 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export const LOGGING_SPAN_KEY = 'logging.googleapis.com/spanId';
*/
export const LOGGING_SAMPLED_KEY = 'logging.googleapis.com/trace_sampled';

/**
* Default library version to be used if version retrieval fails
*/
export const NODEJS_WINSTON_DEFAULT_LIBRARY_VERSION = 'unknown';

// The variable to hold cached library version
let libraryVersion: string;

Expand Down Expand Up @@ -352,11 +357,15 @@ export function getNodejsLibraryVersion() {
if (libraryVersion) {
return libraryVersion;
}
libraryVersion = require(path.resolve(
__dirname,
'../../',
'package.json'
)).version;
try {
libraryVersion = require(path.resolve(
__dirname,
'../../',
'package.json'
)).version;
} catch (err) {
libraryVersion = NODEJS_WINSTON_DEFAULT_LIBRARY_VERSION;
}
return libraryVersion;
}

Expand Down
4 changes: 2 additions & 2 deletions system-test/logging-winston.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ describe('LoggingWinston', function () {
entry.data[instrumentation.DIAGNOSTIC_INFO_KEY][
instrumentation.INSTRUMENTATION_SOURCE_KEY
];
assert.equal(info[0].name, 'nodejs');
assert.equal(info[1].name, 'nodejs-winston');
assert.equal(info[0].name, 'nodejs-winston');
assert.equal(info[1].name, 'nodejs');
} else {
const data = entry.data as {message: string};
assert.strictEqual(data.message, ` ${MESSAGE}`);
Expand Down

0 comments on commit fdb6b26

Please sign in to comment.