From: Michael Tremer Date: Fri, 5 Jun 2026 15:32:53 +0000 (+0000) Subject: source: Check if we have data for a source without going to disk X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d0128d4fcf80db86888c6f9d42a7c39e10503ef;p=telemetry.git source: Check if we have data for a source without going to disk We will only check if the RRD is available if we really need to. Signed-off-by: Michael Tremer --- diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 283608f..b45ae9e 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c @@ -511,8 +511,11 @@ int td_daemon_submit_metrics(td_daemon* self, td_metrics* metrics) { return td_queue_submit_metrics(self->queue, metrics); } -int td_daemon_flush_source( - td_daemon* self, td_source* source, const char* object) { +int td_daemon_has_source(td_daemon* self, td_source* source, const char* object) { + return td_queue_has_source(self->queue, source, object); +} + +int td_daemon_flush_source(td_daemon* self, td_source* source, const char* object) { return td_queue_flush_source(self->queue, source, object); } diff --git a/src/daemon/daemon.h b/src/daemon/daemon.h index 35857fb..f99b7e6 100644 --- a/src/daemon/daemon.h +++ b/src/daemon/daemon.h @@ -53,8 +53,8 @@ int td_daemon_exit(td_daemon* self, int rc); int td_daemon_submit_metrics(td_daemon* self, td_metrics* metrics); -int td_daemon_flush_source( - td_daemon* self, td_source* source, const char* object); +int td_daemon_has_source(td_daemon* self, td_source* source, const char* object); +int td_daemon_flush_source(td_daemon* self, td_source* source, const char* object); #ifdef HAVE_LIBNL3 td_netlink* td_daemon_get_netlink(td_daemon* self); diff --git a/src/daemon/queue.c b/src/daemon/queue.c index 32fd99f..945e45f 100644 --- a/src/daemon/queue.c +++ b/src/daemon/queue.c @@ -363,6 +363,26 @@ int td_queue_flush(td_queue* self) { return 0; } +int td_queue_has_source(td_queue* self,td_source* source, const char* object) { + struct td_queue_object* o = NULL; + + STAILQ_FOREACH(o, &self->queue, nodes) { + // Continue if the source does not match + if (o->source != source) + continue; + + // Continue if the object does not match + if (!td_queue_object_equals(self, o, object)) + continue; + + // Match + return 1; + } + + // No match + return 0; +} + int td_queue_flush_source(td_queue* self, td_source* source, const char* object) { struct td_queue_object* o = NULL; diff --git a/src/daemon/queue.h b/src/daemon/queue.h index 0a51842..97506e5 100644 --- a/src/daemon/queue.h +++ b/src/daemon/queue.h @@ -38,6 +38,8 @@ int td_queue_submit_metrics(td_queue* self, td_metrics* metrics); int td_queue_flush(td_queue* self); +int td_queue_has_source(td_queue* self,td_source* source, const char* object); + int td_queue_flush_source(td_queue* self, td_source* source, const char* object); diff --git a/src/daemon/source.c b/src/daemon/source.c index c089943..6382d50 100644 --- a/src/daemon/source.c +++ b/src/daemon/source.c @@ -1218,15 +1218,21 @@ int td_source_render(td_source* self, td_args* args, const char* object) { if (r < 0) return r; - // Commit any buffered data right now - r = td_daemon_flush_source(self->daemon, self, object); - if (r < 0) - return r; + // Check if we have this object + r = td_source_has_object(self, object); + switch (r) { + // Not found + case 0: + return -ENOENT; + + // Found + case 1: + break; - // Fail if the path does not exist - r = access(path, R_OK); - if (r < 0) - return -errno; + // Raise any other errors + default: + return r; + } // Add all data sources for (const td_rrd_ds* ds = self->impl->rrd_dss; ds->field; ds++) { @@ -1235,5 +1241,38 @@ int td_source_render(td_source* self, td_args* args, const char* object) { return r; } - return 0; + // Commit any buffered data right now + return td_daemon_flush_source(self->daemon, self, object); +} + +int td_source_has_object(td_source* self, const char* object) { + char path[PATH_MAX]; + int r; + + // Fast path by checking if there is anything in the queue + r = td_daemon_has_source(self->daemon, self, object); + if (r) + return r; + + // Determine the path to the RRD file + r = td_source_path(self, object, path); + if (r < 0) + return r; + + // Fail if the path does not exist + r = access(path, R_OK); + if (r < 0) { + switch (errno) { + // Object has not been found + case ENOENT: + return 0; + + // Error + default: + return -errno; + } + } + + // Object has been found + return 1; } diff --git a/src/daemon/source.h b/src/daemon/source.h index 446545e..67b2b2a 100644 --- a/src/daemon/source.h +++ b/src/daemon/source.h @@ -119,4 +119,6 @@ int td_source_commit_metrics(td_source* self, const char* object, int td_source_render(td_source* self, td_args* args, const char* object); +int td_source_has_object(td_source* self, const char* object); + #endif /* TELEMETRY_SOURCE_H */