Skip to content

Commit ba7dca6

Browse files
committed
report metrics to the /metrics endpoint
1 parent 05c0803 commit ba7dca6

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

apps/webapp/app/v3/dynamicFlushScheduler.server.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { nanoid } from "nanoid";
22
import pLimit from "p-limit";
3+
import { Gauge } from "prom-client";
4+
import { metricsRegister } from "~/metrics.server";
35
import { logger } from "~/services/logger.server";
46

57
export type DynamicFlushSchedulerConfig<T> = {
@@ -10,7 +12,6 @@ export type DynamicFlushSchedulerConfig<T> = {
1012
};
1113

1214
export class DynamicFlushScheduler<T> {
13-
// private batchQueue: T[][]; // Adjust the type according to your data structure
1415
private currentBatch: T[]; // Adjust the type according to your data structure
1516
private readonly BATCH_SIZE: number;
1617
private readonly FLUSH_INTERVAL: number;
@@ -30,10 +31,24 @@ export class DynamicFlushScheduler<T> {
3031
this.flushTimer = null;
3132
this.startFlushTimer();
3233
this.setupShutdownHandlers();
34+
35+
const scheduler = this;
36+
new Gauge({
37+
name: "dynamic_flush_scheduler_batch_size",
38+
help: "Number of items in the current dynamic flush scheduler batch",
39+
collect() {
40+
this.set(scheduler.currentBatch.length);
41+
},
42+
registers: [metricsRegister],
43+
});
3344
}
3445

3546
async addToBatch(items: T[]): Promise<void> {
3647
this.currentBatch.push(...items);
48+
logger.debug("Adding items to batch", {
49+
batchSize: this.BATCH_SIZE,
50+
newSize: this.currentBatch.length,
51+
});
3752

3853
if (this.currentBatch.length >= this.BATCH_SIZE) {
3954
await this.flushNextBatch();
@@ -52,8 +67,12 @@ export class DynamicFlushScheduler<T> {
5267
private async shutdown(): Promise<void> {
5368
if (this.isShuttingDown) return;
5469
this.isShuttingDown = true;
70+
logger.log("Shutting down dynamic flush scheduler...");
71+
5572
await this.checkAndFlush();
5673
this.clearTimer();
74+
75+
logger.log("All items have been flushed.");
5776
}
5877

5978
private clearTimer(): void {

0 commit comments

Comments
 (0)