Skip to content

Commit

Permalink
[EV-37][Indra] Select prometheus metrics when enabled over statsd
Browse files Browse the repository at this point in the history
  • Loading branch information
indrajithi committed Nov 22, 2023
1 parent ec34915 commit ceb1b19
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/ziggurat/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
(get (ziggurat-config) :rabbit-mq))

(defn prometheus-config []
(get config :prometheus))
(get (ziggurat-config) :prometheus))

(defn statsd-config []
(let [cfg (ziggurat-config)]
Expand Down
4 changes: 2 additions & 2 deletions src/ziggurat/init.clj
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@
(set-properties-for-structured-logging))

(defn start-common-states []
(start* #{#'metrics/statsd-reporter
(start* #{#'metrics/metrics-reporter
#'sentry-reporter
#'nrepl-server/server}))

(defn stop-common-states []
(mount/stop #'config/config
#'sentry-reporter
#'metrics/statsd-reporter
#'metrics/metrics-reporter
#'nrepl-server/server))

(defn start
Expand Down
30 changes: 19 additions & 11 deletions src/ziggurat/metrics.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns ziggurat.metrics
(:require [clojure.string :as str]
[clojure.tools.logging :as log]
[ziggurat.config :refer [statsd-config ziggurat-config]]
[ziggurat.config :refer [statsd-config ziggurat-config prometheus-config]]
[ziggurat.util.java-util :as util]
[mount.core :refer [defstate]]
[ziggurat.prometheus-exporter :as prometheus-exporter]
Expand Down Expand Up @@ -34,14 +34,20 @@
(let [metrics-impl-constructor (get-metrics-implementor-constructor)]
(reset! metric-impl (metrics-impl-constructor))))

(defstate statsd-reporter
(def prometheus-enabled?
(:enabled (prometheus-config)))

(defstate metrics-reporter
:start (do (log/info "Initializing Metrics")
(prometheus-exporter/start-http-server)
(initialise-metrics-library)
(metrics-interface/initialize @metric-impl (statsd-config)))
(if prometheus-enabled?
(prometheus-exporter/start-http-server)
(do
(initialise-metrics-library)
(metrics-interface/initialize @metric-impl (statsd-config)))))
:stop (do (log/info "Terminating Metrics")
(prometheus-exporter/stop-http-server)
(metrics-interface/terminate @metric-impl)))
(if prometheus-enabled?
(prometheus-exporter/stop-http-server)
(metrics-interface/terminate @metric-impl))))

(defn intercalate-dot
[names]
Expand Down Expand Up @@ -96,8 +102,9 @@
tags (remove-topic-tag-for-old-namespace (get-all-tags (get-map additional-tags) metric-namespaces) metric-namespace)
signed-int-value (sign (get-int n))]
(doseq [metric-ns metric-namespaces]
(prometheus-exporter/update-counter metric-ns metric tags signed-int-value)
(metrics-interface/update-counter @metric-impl metric-ns metric tags signed-int-value)))))
(if prometheus-enabled?
(prometheus-exporter/update-counter metric-ns metric tags signed-int-value)
(metrics-interface/update-counter @metric-impl metric-ns metric tags signed-int-value))))))

(def increment-count (partial inc-or-dec-count +))

Expand All @@ -116,8 +123,9 @@
integer-value (get-int val)
metric "all"]
(doseq [metric-ns intercalated-metric-namespaces]
(prometheus-exporter/report-histogram metric-ns metric tags integer-value)
(metrics-interface/update-timing @metric-impl metric-ns metric tags integer-value)))))
(if prometheus-enabled?
(prometheus-exporter/report-histogram metric-ns metric tags integer-value)
(metrics-interface/update-timing @metric-impl metric-ns metric tags integer-value))))))

(defn report-time
"This function is an alias for `report-histogram`.
Expand Down
4 changes: 2 additions & 2 deletions test/ziggurat/fixtures.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
(mount/stop)))

(defn mount-metrics [f]
(mount/start (mount/only [#'metrics/statsd-reporter]))
(mount/start (mount/only [#'metrics/metrics-reporter]))
(f)
(mount/stop #'metrics/statsd-reporter))
(mount/stop #'metrics/metrics-reporter))

(defn mount-config-with-tracer [f]
(mount-config)
Expand Down
12 changes: 6 additions & 6 deletions test/ziggurat/metrics_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

(deftest increment-count-test
(with-redefs [config (assoc-in config [:ziggurat :metrics :constructor] "ziggurat.util.mock-metrics-implementation/->MockImpl")]
(mount/start (mount/only [#'metrics/statsd-reporter]))
(mount/start (mount/only [#'metrics/metrics-reporter]))
(let [passed-metric-name "metric3"
expected-topic-entity-name "expected-topic-entity-name"
input-tags {:topic_name expected-topic-entity-name}
Expand Down Expand Up @@ -114,11 +114,11 @@
(reset! increment-count-called? true)))]
(metrics/-incrementCount metric-namespace passed-metric-name tags)
(is (true? @increment-count-called?))))))
(mount/stop #'metrics/statsd-reporter)))
(mount/stop #'metrics/metrics-reporter)))

(deftest decrement-count-test
(with-redefs [config (assoc-in config [:ziggurat :metrics :constructor] "ziggurat.util.mock-metrics-implementation/->MockImpl")]
(mount/start (mount/only [#'metrics/statsd-reporter]))
(mount/start (mount/only [#'metrics/metrics-reporter]))
(let [expected-topic-name "expected-topic-name"
passed-metric-name "metric3"
input-tags {:topic_name expected-topic-name}
Expand Down Expand Up @@ -195,11 +195,11 @@
(reset! decrement-count-called? true)))]
(metrics/-decrementCount metric-namespace passed-metric-name tags)
(is (true? @decrement-count-called?))))))
(mount/stop #'metrics/statsd-reporter)))
(mount/stop #'metrics/metrics-reporter)))

(deftest report-histogram-test
(with-redefs [config (assoc-in config [:ziggurat :metrics :constructor] "ziggurat.util.mock-metrics-implementation/->MockImpl")]
(mount/start (mount/only [#'metrics/statsd-reporter]))
(mount/start (mount/only [#'metrics/metrics-reporter]))
(let [topic-entity-name "expected-topic-entity-name"
input-tags {:topic_name topic-entity-name}
time-val 10
Expand Down Expand Up @@ -265,7 +265,7 @@
(reset! report-histogram-called? true)))]
(metrics/-reportTime metric-namespace time-val tags)
(is (true? @report-histogram-called?))))))
(mount/stop #'metrics/statsd-reporter)))
(mount/stop #'metrics/metrics-reporter)))

(deftest report-time-test
(let [metric-namespace "metric-namespace"
Expand Down

0 comments on commit ceb1b19

Please sign in to comment.