-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: monitor event loop lag (#11127)
This PR adds an event loop monitor that can be viewed in Grafana.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { promiseWithResolvers } from '@aztec/foundation/promise'; | ||
import { Timer } from '@aztec/foundation/timer'; | ||
|
||
import { EVENT_LOOP_LAG } from './metrics.js'; | ||
import { type Meter, type ObservableGauge, type ObservableResult, ValueType } from './telemetry.js'; | ||
|
||
/** | ||
* Detector for custom Aztec attributes | ||
*/ | ||
export class EventLoopMonitor { | ||
private eventLoopLag: ObservableGauge; | ||
private started = false; | ||
|
||
constructor(meter: Meter) { | ||
this.eventLoopLag = meter.createObservableGauge(EVENT_LOOP_LAG, { | ||
unit: 'us', | ||
valueType: ValueType.INT, | ||
description: 'How busy is the event loop', | ||
}); | ||
} | ||
|
||
start(): void { | ||
if (this.started) { | ||
return; | ||
} | ||
this.eventLoopLag.addCallback(this.measureLag); | ||
} | ||
|
||
stop(): void { | ||
if (!this.started) { | ||
return; | ||
} | ||
this.eventLoopLag.removeCallback(this.measureLag); | ||
} | ||
|
||
private measureLag = async (obs: ObservableResult): Promise<void> => { | ||
const timer = new Timer(); | ||
const { promise, resolve } = promiseWithResolvers<number>(); | ||
// how long does it take to schedule the next macro task? | ||
// if this number spikes then we're (1) either blocking the event loop with long running sync code | ||
// or (2) spamming the event loop with micro tasks | ||
setImmediate(() => { | ||
resolve(timer.us()); | ||
}); | ||
|
||
const lag = await promise; | ||
obs.observe(Math.floor(lag)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters