Skip to content

Commit

Permalink
fix: Instrumentation performance (#1354)
Browse files Browse the repository at this point in the history
* fix: Instrumentation performance

* Rename instrumentationWritten
  • Loading branch information
losalex committed Oct 11, 2022
1 parent 5637ca3 commit 9f14ea1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/utils/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ declare const global: {[index: string]: any};
// libraries related info.
global.instrumentationAdded = false;

// The global variable to avoid records inspection once instrumentation already written to prevent perf impact
global.shouldSkipInstrumentationCheck = false;

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

Expand All @@ -46,6 +49,11 @@ export type InstrumentationInfo = {name: string; version: string};
export function populateInstrumentationInfo(
entry: Entry | Entry[]
): [Entry[], boolean] {
// Check if instrumentation data was already written once. This prevents also inspection of
// the entries for instrumentation data to prevent perf degradation
if (global.shouldSkipInstrumentationCheck) {
return [arrify(entry), false];
}
// Update the flag indicating that instrumentation entry was already added once,
// so any subsequent calls to this method will not add a separate instrumentation log entry
let isWritten = setInstrumentationStatus(true);
Expand All @@ -64,7 +72,10 @@ export function populateInstrumentationInfo(
validateAndUpdateInstrumentation(info);
// Indicate that instrumentation info log entry already exists
// and that current library info was added to existing log entry
isInfoAdded = isWritten = true;
global.shouldSkipInstrumentationCheck =
isInfoAdded =
isWritten =
true;
}
entries.push(entryItem);
}
Expand All @@ -74,7 +85,7 @@ export function populateInstrumentationInfo(
// instrumentation data for this library
if (!isWritten) {
entries.push(createDiagnosticEntry(undefined, undefined));
isInfoAdded = true;
global.shouldSkipInstrumentationCheck = isInfoAdded = true;
}
return [entries, isInfoAdded];
}
Expand Down
2 changes: 2 additions & 0 deletions test/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const LONG_VERSION_TEST = VERSION_TEST + '.0.0.0.0.0.0.0.0.11.1.1-ALPHA';
describe('instrumentation_info', () => {
beforeEach(() => {
instrumentation.setInstrumentationStatus(false);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).shouldSkipInstrumentationCheck = false;
});

it('should generate library info properly by default', () => {
Expand Down
4 changes: 4 additions & 0 deletions test/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ describe('Log', () => {

it('should set the partialSuccess properly for instrumentation record', async () => {
instrumentation.setInstrumentationStatus(false);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).shouldSkipInstrumentationCheck = false;
await log.write(ENTRIES, OPTIONS);
assert(
log.logging.loggingService.writeLogEntries.calledOnceWith(
Expand All @@ -569,6 +571,8 @@ describe('Log', () => {

it('should set the partialSuccess properly for existing instrumentation record', async () => {
ENTRIES.push(instrumentation.createDiagnosticEntry(undefined, undefined));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any).shouldSkipInstrumentationCheck = false;
await log.write(ENTRIES, OPTIONS);
assert(
log.logging.loggingService.writeLogEntries.calledOnceWith(
Expand Down

0 comments on commit 9f14ea1

Please sign in to comment.