Subject: [PATCH] Use the #:namespace argument for metric registries

From: Christopher Baines <[email-masked]>
Date: Sun, 6 Sep 2020 13:07:29 +0100

---
 .../agent-messaging/http.scm                  | 22 +++++--------------
 guix-build-coordinator/coordinator.scm        | 18 ++++++++-------
 guix-build-coordinator/datastore/sqlite.scm   | 11 ++++------
 scripts/guix-build-coordinator.in             |  4 +++-
 4 files changed, 23 insertions(+), 32 deletions(-)

diff --git a/guix-build-coordinator/agent-messaging/http.scm b/guix-build-coordinator/agent-messaging/http.scm
index 1a9a9e9..91b686b 100644
--- a/guix-build-coordinator/agent-messaging/http.scm
+++ b/guix-build-coordinator/agent-messaging/http.scm
@@ -142,9 +142,6 @@ port. Also, the port used can be changed by passing the --port option.\n"
         ""))
 
 (define (base-datastore-metrics-updater build-coordinator)
-  (define namespace
-    "guixbuildcoordinator")
-
   (define datastore
     (build-coordinator-datastore build-coordinator))
 
@@ -152,34 +149,27 @@ port. Also, the port used can be changed by passing the --port option.\n"
     (build-coordinator-metrics-registry build-coordinator))
 
   (let ((builds-total
-         (make-gauge-metric registry
-                            (string-append namespace
-                                           "_builds_total")
+         (make-gauge-metric registry "builds_total"
                             #:labels '(system)))
         (allocated-builds-total
          (make-gauge-metric registry
-                            (string-append namespace
-                                           "_allocated_builds_total")
+                            "allocated_builds_total"
                             #:labels '(agent_id)))
         (build-results-total
          (make-gauge-metric registry
-                            (string-append namespace
-                                           "_build_results_total")
+                            "build_results_total"
                             #:labels '(agent_id result)))
         (setup-failures-total
          (make-gauge-metric registry
-                            (string-append namespace
-                                           "_setup_failures_total")
+                            "setup_failures_total"
                             #:labels '(agent_id reason)))
         (build-allocation-plan-total
          (make-gauge-metric registry
-                            (string-append namespace
-                                           "_build_allocation_plan_total")
+                            "build_allocation_plan_total"
                             #:labels '(agent_id)))
         (unprocessed-hook-events-total
          (make-gauge-metric registry
-                            (string-append namespace
-                                           "_unprocessed_hook_events_total")
+                            "unprocessed_hook_events_total"
                             #:labels '(event))))
     (define (zero-metric-for-agents metric)
       (for-each (lambda (agent-details)
diff --git a/guix-build-coordinator/coordinator.scm b/guix-build-coordinator/coordinator.scm
index 33b5afe..c8727c8 100644
--- a/guix-build-coordinator/coordinator.scm
+++ b/guix-build-coordinator/coordinator.scm
@@ -68,7 +68,9 @@
                         set-build-coordinator-allocator-thread!))
 
 (define* (make-build-coordinator #:key datastore hooks
-                                 (metrics-registry (make-metrics-registry))
+                                 (metrics-registry (make-metrics-registry
+                                                    #:namespace
+                                                    "guixbuildcoordinator_"))
                                  (allocation-strategy
                                   basic-build-allocation-strategy))
   (let ((build-coordinator
@@ -198,12 +200,12 @@
   (define success-counter-metric
     (make-counter-metric
      (build-coordinator-metrics-registry build-coordinator)
-     "guixbuildcoordinator_allocator_allocations_total"))
+     "allocator_allocations_total"))
 
   (define failure-counter-metric
     (make-counter-metric
      (build-coordinator-metrics-registry build-coordinator)
-     "guixbuildcoordinator_allocator_failures_total"))
+     "allocator_failures_total"))
 
   (define (allocate-builds-loop)
     (while #t
@@ -215,7 +217,7 @@
             (atomic-box-set! allocation-needed #f)))
         (call-with-duration-metric
          (build-coordinator-metrics-registry build-coordinator)
-         "guixbuildcoordinator_allocate_builds_duration_seconds"
+         "allocate_builds_duration_seconds"
          (lambda ()
            (with-exception-handler
                (lambda (exn)
@@ -270,13 +272,13 @@
   (define success-counter-metric
     (make-counter-metric
      (build-coordinator-metrics-registry build-coordinator)
-     "guixbuildcoordinator_hook_success_total"
+     "hook_success_total"
      #:labels '(event)))
 
   (define failure-counter-metric
     (make-counter-metric
      (build-coordinator-metrics-registry build-coordinator)
-     "guixbuildcoordinator_hook_failure_total"
+     "hook_failure_total"
      #:labels '(event)))
 
   (define (process-events event-name handler)
@@ -330,7 +332,7 @@
 (define (fetch-builds build-coordinator agent systems count)
   (call-with-duration-metric
    (build-coordinator-metrics-registry build-coordinator)
-   "guixbuildcoordinator_coordinator_fetch_builds_duration_seconds"
+   "coordinator_fetch_builds_duration_seconds"
    (lambda ()
      (let ((update-made (datastore-update-agent-requested-systems
                          (build-coordinator-datastore build-coordinator)
@@ -393,7 +395,7 @@
 
   (call-with-duration-metric
    (build-coordinator-metrics-registry build-coordinator)
-   "guixbuildcoordinator_coordinator_handle_build_result_duration_seconds"
+   "coordinator_handle_build_result_duration_seconds"
    (lambda ()
      (let* ((result (assoc-ref result-json "result"))
             (success? (string=? result "success")))
diff --git a/guix-build-coordinator/datastore/sqlite.scm b/guix-build-coordinator/datastore/sqlite.scm
index f3e3bbf..13d9ec7 100644
--- a/guix-build-coordinator/datastore/sqlite.scm
+++ b/guix-build-coordinator/datastore/sqlite.scm
@@ -126,13 +126,10 @@
   (define registry (slot-ref datastore 'metrics-registry))
 
   (if registry
-      (let* ((namespaced-metric-name
-              (string-append "guixbuildcoordinator_" metric-name))
-             (metric
-              (or (metrics-registry-fetch-metric registry namespaced-metric-name)
-                  (make-histogram-metric
-                   registry
-                   namespaced-metric-name)))
+      (let* ((metric
+              (or (metrics-registry-fetch-metric registry metric-name)
+                  (make-histogram-metric registry
+                                         metric-name)))
              (start-time (current-time)))
         (let ((result (thunk)))
           (metric-observe metric (- (current-time) start-time))
diff --git a/scripts/guix-build-coordinator.in b/scripts/guix-build-coordinator.in
index 87982c0..f4c5048 100644
--- a/scripts/guix-build-coordinator.in
+++ b/scripts/guix-build-coordinator.in
@@ -434,7 +434,9 @@ processed?:      ~A
                                (append %service-option-defaults
                                        %base-option-defaults)
                                arguments))
-          (metrics-registry (make-metrics-registry))
+          (metrics-registry (make-metrics-registry
+                             #:namespace
+                             "guixbuildcoordinator_"))
           (datastore (database-uri->datastore
                       (assq-ref opts 'database)
                       #:metrics-registry metrics-registry))
-- 
2.28.0