Skip to content

Commit 9f65380

Browse files
committed
Add daily reset functionality for Prometheus registry to manage memory growth
1 parent 7afe643 commit 9f65380

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

src/middlewares/metrics/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@ const httpGeoRequestsTotal = new client.Counter({
4343
registers: [register],
4444
});
4545

46+
function msUntilNextDailyHour(targetHour = 4, targetMinute = 0) {
47+
const now = new Date();
48+
const next = new Date(now);
49+
next.setHours(targetHour, targetMinute, 0, 0);
50+
// If the target time has already passed today, schedule for tomorrow
51+
if (next <= now) {
52+
next.setDate(next.getDate() + 1);
53+
}
54+
return next.getTime() - now.getTime();
55+
}
56+
57+
// Schedule a daily reset (4AM) of the Prometheus registry to prevent
58+
// memory growth from high-cardinality labels.
59+
function scheduleDailyRegistryClear(hour = 4, minute = 0) {
60+
const scheduleNext = () => {
61+
const delay = msUntilNextDailyHour(hour, minute);
62+
setTimeout(() => register.resetMetrics(), delay);
63+
};
64+
65+
scheduleNext();
66+
}
67+
// scheduleDailyRegistryClear(4, 0);
4668

4769
// ---------------------------------------------------------------------------
4870
// PM2 Cluster Registry Aggregation

0 commit comments

Comments
 (0)