Skip to content

Commit

Permalink
Add name to metrics (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdemare authored Jan 29, 2024
1 parent cf05e97 commit 3564594
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/nl/surf/eduhub_rio_mapper/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@
{:status http-status/bad-request :body "Malformed callback url"})))))

(defn wrap-metrics-getter
[app count-queues-fn fetch-jobs-by-status]
[app count-queues-fn fetch-jobs-by-status schac-home-to-name]
(fn with-metrics-getter [req]
(let [res (app req)]
(cond-> res
(:metrics res)
(assoc :status http-status/ok
:body (metrics/prometheus-render-metrics (count-queues-fn) (fetch-jobs-by-status)))))))
:body (metrics/prometheus-render-metrics (count-queues-fn) (fetch-jobs-by-status) schac-home-to-name))))))

(defn wrap-status-getter
[app config]
Expand Down Expand Up @@ -211,6 +211,7 @@

(defn make-app [{:keys [auth-config clients] :as config}]
(let [institution-schac-homes (clients-info/institution-schac-homes clients)
schac-home-to-name (reduce (fn [h c] (assoc h (:institution-schac-home c) (:institution-name c))) {} clients)
queue-counter-fn (fn [] (metrics/count-queues #(worker/queue-counts-by-key % config) institution-schac-homes))
jobs-by-status-counter-fn (fn [] (metrics/fetch-jobs-by-status-count config))
token-authenticator (-> (authentication/make-token-authenticator auth-config)
Expand All @@ -222,7 +223,8 @@
(wrap-job-enqueuer (partial worker/enqueue! config))
(wrap-status-getter config)
(wrap-metrics-getter queue-counter-fn
jobs-by-status-counter-fn)
jobs-by-status-counter-fn
schac-home-to-name)
(wrap-client-info clients)
(authentication/wrap-authentication token-authenticator)
(wrap-json-response)
Expand Down
14 changes: 7 additions & 7 deletions src/nl/surf/eduhub_rio_mapper/metrics.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@
(map process-pair)
(reduce process-triplet {}))))

(defn prometheus-current-jobs [queue-count]
(map (fn [[k v]] (format "rio_mapper_active_and_queued_job_count{schac_home=\"%s\"} %s" k v))
(defn prometheus-current-jobs [queue-count schac-home-to-name]
(map (fn [[k v]] (format "rio_mapper_active_and_queued_job_count{schac_home=\"%s\", institution_name=\"%s\"} %s" k (schac-home-to-name k) v))
queue-count))

(defn prometheus-jobs-by-status [jobs-count-by-status]
(defn prometheus-jobs-by-status [jobs-count-by-status schac-home-to-name]
{:pre [(every? string? (keys jobs-count-by-status))
(every? map? (vals jobs-count-by-status))]}
(mapcat (fn [status]
(map (fn [[k v]]
(format "rio_mapper_jobs_total{schac_home=\"%s\", job_status=\"%s\"} %s" k status v))
(format "rio_mapper_jobs_total{schac_home=\"%s\", institution_name=\"%s\", job_status=\"%s\"} %s" k (schac-home-to-name k) status v))
(get jobs-count-by-status status)))
["started" "done" "time_out" "error"]))

(defn prometheus-render-metrics [current-queue-count jobs-count-by-status]
(defn prometheus-render-metrics [current-queue-count jobs-count-by-status schac-home-to-name]
{:pre [(map? current-queue-count)
(every? string? (keys current-queue-count))
(every? integer? (vals current-queue-count))
(map? jobs-count-by-status)
(every? string? (keys jobs-count-by-status))
(every? map? (vals jobs-count-by-status))]}
(str/join "\n" (into (prometheus-current-jobs current-queue-count)
(prometheus-jobs-by-status jobs-count-by-status))))
(str/join "\n" (into (prometheus-current-jobs current-queue-count schac-home-to-name)
(prometheus-jobs-by-status jobs-count-by-status schac-home-to-name))))

(defn count-queues [grouped-queue-counter client-schac-homes]
{:post [(map? %)
Expand Down
11 changes: 6 additions & 5 deletions test/nl/surf/eduhub_rio_mapper/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@
(let [app (api/wrap-metrics-getter
api/routes
(constantly {"foo" 1, "bar" 2})
(constantly {"done" {"google" 123, "meta" 321}}))
(constantly {"done" {"google" 123, "meta" 321}})
{"delft" "TU", "leiden" "LU", "google" "alphabet", "meta" "facebook", "foo" "OOF", "bar" "RAB"})
{:keys [status body]} (app (request :get "/metrics"))]
(is (= http-status/ok status))
(is (= (str "rio_mapper_jobs_total{schac_home=\"meta\", job_status=\"done\"} 321\n"
"rio_mapper_jobs_total{schac_home=\"google\", job_status=\"done\"} 123\n"
"rio_mapper_active_and_queued_job_count{schac_home=\"foo\"} 1\n"
"rio_mapper_active_and_queued_job_count{schac_home=\"bar\"} 2")
(is (= (str "rio_mapper_jobs_total{schac_home=\"meta\", institution_name=\"facebook\", job_status=\"done\"} 321\n"
"rio_mapper_jobs_total{schac_home=\"google\", institution_name=\"alphabet\", job_status=\"done\"} 123\n"
"rio_mapper_active_and_queued_job_count{schac_home=\"foo\", institution_name=\"OOF\"} 1\n"
"rio_mapper_active_and_queued_job_count{schac_home=\"bar\", institution_name=\"RAB\"} 2")
body))))

(deftest wrap-job-queuer
Expand Down
13 changes: 7 additions & 6 deletions test/nl/surf/eduhub_rio_mapper/metrics_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
[nl.surf.eduhub-rio-mapper.metrics :as metrics]))

(deftest render-metrics
(is (= (metrics/prometheus-render-metrics {"delft" 12 "leiden" 32}
{"done" {"google" 123, "meta" 321}})
(str "rio_mapper_jobs_total{schac_home=\"meta\", job_status=\"done\"} 321\n"
"rio_mapper_jobs_total{schac_home=\"google\", job_status=\"done\"} 123\n"
"rio_mapper_active_and_queued_job_count{schac_home=\"delft\"} 12\n"
"rio_mapper_active_and_queued_job_count{schac_home=\"leiden\"} 32"))))
(is (= (str "rio_mapper_jobs_total{schac_home=\"meta\", institution_name=\"facebook\", job_status=\"done\"} 321\n"
"rio_mapper_jobs_total{schac_home=\"google\", institution_name=\"alphabet\", job_status=\"done\"} 123\n"
"rio_mapper_active_and_queued_job_count{schac_home=\"delft\", institution_name=\"TU\"} 12\n"
"rio_mapper_active_and_queued_job_count{schac_home=\"leiden\", institution_name=\"LU\"} 32")
(metrics/prometheus-render-metrics {"delft" 12 "leiden" 32}
{"done" {"google" 123, "meta" 321}}
{"delft" "TU", "leiden" "LU", "google" "alphabet", "meta" "facebook"}))))

0 comments on commit 3564594

Please sign in to comment.