]> git.ipfire.org Git - telemetry.git/commitdiff
source: Check if we have data for a source without going to disk
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Jun 2026 15:32:53 +0000 (15:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 5 Jun 2026 15:32:53 +0000 (15:32 +0000)
We will only check if the RRD is available if we really need to.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c
src/daemon/daemon.h
src/daemon/queue.c
src/daemon/queue.h
src/daemon/source.c
src/daemon/source.h

index 283608fcacf3cb07515590fb9205e2d0a2915118..b45ae9ef631f36f42a3b370fd0e2b4339ab23e55 100644 (file)
@@ -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);
 }
 
index 35857fbb4a8a88abbc1108be4ef801420c1776e9..f99b7e614d25d43c040778638d52558697f2aa43 100644 (file)
@@ -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);
index 32fd99fbee0ddb796162d87d1c0c0a42c2315f28..945e45fdbfe08269e297f2f95b7260b919d67111 100644 (file)
@@ -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;
index 0a51842a1f7f11eab49f9b7f02cfc44b218c977f..97506e5b4e9fd72ea22e0b8eb68c606d112735c3 100644 (file)
@@ -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);
 
index c089943301650d924b9bb5fea62529ac615b87f5..6382d50d82960d3c6364ab6874f261c4ee9c50c3 100644 (file)
@@ -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;
 }
index 446545e9d14f2b31b968c851a894450bb0c1ce32..67b2b2a30cd3177e8269c0f732a978b0c228bc00 100644 (file)
@@ -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 */