Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Instrumentation performance #1354

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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