Skip to content

Commit 32b22b4

Browse files
authored
fix: Switch instrumentation code to work with NODEJS_DEFAULT_LIBRARY_VERSION only (#1373)
* fix: Switch instrumentation code to work with NODEJS_DEFAULT_LIBRARY_VERSION only * Fix comment
1 parent 87e66eb commit 32b22b4

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

src/utils/instrumentation.ts

+7-19
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616

1717
import arrify = require('arrify');
18-
import path = require('path');
1918
import {Entry} from '../entry';
2019

2120
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -29,15 +28,17 @@ global.instrumentationAdded = false;
2928
// The global variable to avoid records inspection once instrumentation already written to prevent perf impact
3029
global.shouldSkipInstrumentationCheck = false;
3130

32-
// The variable to hold cached library version
33-
let libraryVersion: string;
34-
3531
// Max length for instrumentation library name and version values
3632
const maxDiagnosticValueLen = 14;
3733

3834
export const DIAGNOSTIC_INFO_KEY = 'logging.googleapis.com/diagnostic';
3935
export const INSTRUMENTATION_SOURCE_KEY = 'instrumentation_source';
4036
export const NODEJS_LIBRARY_NAME_PREFIX = 'nodejs';
37+
/**
38+
* Default library version to be used
39+
* Using release-please annotations to update DEFAULT_INSTRUMENTATION_VERSION with latest version.
40+
* See https://github.com/googleapis/release-please/blob/main/docs/customizing.md#updating-arbitrary-files
41+
*/
4142
export const NODEJS_DEFAULT_LIBRARY_VERSION = '10.3.0'; // {x-release-please-version}
4243
export const MAX_INSTRUMENTATION_COUNT = 3;
4344
export type InstrumentationInfo = {name: string; version: string};
@@ -184,24 +185,11 @@ function truncateValue(value: object | string, maxLen: number) {
184185
}
185186

186187
/**
187-
* The helper function to retrieve current library version from 'package.json' file. Note that
188-
* since we use {path.resolve}, the search for 'package.json' could be impacted by current working directory.
188+
* The helper function to retrieve current library version from annotated NODEJS_DEFAULT_LIBRARY_VERSION
189189
* @returns {string} A current library version.
190190
*/
191191
export function getNodejsLibraryVersion() {
192-
if (libraryVersion) {
193-
return libraryVersion;
194-
}
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-
}
204-
return libraryVersion;
192+
return NODEJS_DEFAULT_LIBRARY_VERSION;
205193
}
206194

207195
/**

0 commit comments

Comments
 (0)