Skip to content

Commit

Permalink
fix: Prevent instrumentation crash and fix the system test (#666)
Browse files Browse the repository at this point in the history
* fix: Prevent instrumentation crash and fix the system test

* Update base package to 10.2.2.
  • Loading branch information
losalex authored Nov 1, 2022
1 parent 303320e commit 4e12496
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 @@ -51,7 +51,7 @@
"precompile": "gts clean"
},
"dependencies": {
"@google-cloud/logging": "^10.1.11",
"@google-cloud/logging": "^10.2.2",
"google-auth-library": "^8.0.2"
},
"devDependencies": {
Expand Down
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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_BUNYAN_DEFAULT_LIBRARY_VERSION = 'unknown';

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

Expand Down Expand Up @@ -479,11 +484,15 @@ export class LoggingBunyan extends Writable {
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_BUNYAN_DEFAULT_LIBRARY_VERSION;
}
return libraryVersion;
}
}
Expand Down
4 changes: 2 additions & 2 deletions system-test/logging-bunyan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ describe('LoggingBunyan', function () {
const info = (entry.data as any)[instrumentation.DIAGNOSTIC_INFO_KEY][
instrumentation.INSTRUMENTATION_SOURCE_KEY
];
assert.equal(info[0].name, 'nodejs');
assert.equal(info[0].name, 'nodejs-bunyan');
assert.ok(info[0].version.includes('.'));
assert.equal(info[1].name, 'nodejs-bunyan');
assert.equal(info[1].name, 'nodejs');
assert.ok(info[1].version.includes('.'));
isDiagnosticPresent = true;
} else {
Expand Down

0 comments on commit 4e12496

Please sign in to comment.