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

Commit 01e1286

Browse files
authored
fix: Instrumentation version contains package.json content (#1310)
* fix: Instrumentation version contains package.json content * Fix parameter type * Another type fix
1 parent 91c1222 commit 01e1286

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@google-cloud/logging",
33
"version": "10.1.1",
4-
"description": "Stackdriver Logging Client Library for Node.js",
4+
"description": "Cloud Logging Client Library for Node.js",
55
"keywords": [
66
"google apis client",
77
"google api client",

samples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
"supertest": "^6.0.0",
2828
"uuid": "^8.0.0"
2929
}
30-
}
30+
}

src/utils/instrumentation.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,27 @@ function validateAndUpdateInstrumentation(
149149
/**
150150
* A helper function to truncate a value (library name or version for example). The value is truncated
151151
* when it is longer than {maxLen} chars and '*' is added instead of truncated suffix.
152-
* @param value {string} The value to be truncated.
152+
* @param value {object|string} The value to be truncated.
153153
* @param maxLen {number} The max length to be used for truncation.
154154
* @returns {string} The truncated value.
155155
*/
156-
function truncateValue(value: string, maxLen: number) {
156+
function truncateValue(value: object | string, maxLen: number) {
157+
// Currently there are cases when we get here JSON object instead of string
158+
// Adding here additional validation to see if version still can be retrieved
159+
if (typeof value !== 'string') {
160+
try {
161+
if (Object.prototype.hasOwnProperty.call(value, 'version')) {
162+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
163+
value = (value as any).version;
164+
}
165+
} catch (err) {
166+
// Ignore error since flow should continue
167+
}
168+
}
169+
// Return 'unknown' if version cannot be retrieved
170+
if (typeof value !== 'string') {
171+
return 'unknown';
172+
}
157173
if (value && value.length > maxLen) {
158174
return value.substring(0, maxLen).concat('*');
159175
}

test/instrumentation.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ describe('instrumentation_info', () => {
5151
);
5252
});
5353

54+
it('should set library version to unknown', () => {
55+
const data = {some: 'value'};
56+
const entry = instrumentation.createDiagnosticEntry(
57+
undefined,
58+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
59+
data as any
60+
) as Entry;
61+
assert.equal(
62+
entry.data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
63+
instrumentation.INSTRUMENTATION_SOURCE_KEY
64+
]?.[0]?.[NAME],
65+
instrumentation.NODEJS_LIBRARY_NAME_PREFIX
66+
);
67+
assert.equal(
68+
entry.data?.[instrumentation.DIAGNOSTIC_INFO_KEY]?.[
69+
instrumentation.INSTRUMENTATION_SOURCE_KEY
70+
]?.[0]?.[VERSION],
71+
'unknown'
72+
);
73+
});
74+
5475
it('should add instrumentation log entry to the list', () => {
5576
const dummyEntry = createEntry(undefined, undefined);
5677
const entries = instrumentation.populateInstrumentationInfo(dummyEntry);

0 commit comments

Comments
 (0)