]> git.ipfire.org Git - telemetry.git/commitdiff
source: Stagger sources to avoid load peaks
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 6 Oct 2025 17:03:52 +0000 (17:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 6 Oct 2025 17:03:52 +0000 (17:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/source.c

index cd075032c84248dbe4284e7a739e7581cd0f4b49..fd53b31de66ad74fdbd7ac7173e9ed435824bb58 100644 (file)
 #include "time.h"
 #include "util.h"
 
+// When the daemon starts up, we should not collect everything straight away to
+// avoid any usage peaks. Therefore we will stagger all sources randomly across
+// N seconds.
+#define STAGGER        60
+
 #define STEPSIZE 60 // seconds
 
 // Interval after which the heartbeat function is being called again
@@ -326,15 +331,19 @@ static int collecty_source_heartbeat(sd_event_source* source, uint64_t usec, voi
 }
 
 static int collecty_source_register_heartbeat(collecty_source* self) {
+       uint64_t heartbeat = 0;
        int r;
 
        // No need to do this if we don't have a collect method
        if (!self->impl->collect)
                return 0;
 
+       // To avoid any load peaks, stagger sources
+       heartbeat = SEC_TO_USEC(rand() % STAGGER);
+
        // Call the heartbeat function immediately
        r = sd_event_add_time_relative(self->loop, &self->events.heartbeat,
-                       CLOCK_MONOTONIC, 0, 0, collecty_source_heartbeat, self);
+                       CLOCK_MONOTONIC, heartbeat, 0, collecty_source_heartbeat, self);
        if (r < 0) {
                ERROR(self->ctx, "Failed to register the heartbeat timer: %s\n", strerror(-r));
                return r;