Skip to content

Commit

Permalink
Merge d299f33 into be27bb7
Browse files Browse the repository at this point in the history
  • Loading branch information
macalimlim committed Jul 19, 2019
2 parents be27bb7 + d299f33 commit 0bd6e94
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ziggurat/metrics.clj
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@
(let [topic-name (:topic_name additional-tags)]
(dissoc additional-tags (when (some #(= % topic-name) ns) :topic_name))))

(defn- get-metric-namespaces
[metric-namespaces]
(if (vector? metric-namespaces)
(intercalate-dot metric-namespaces)
metric-namespaces))

(defn- inc-or-dec-count
([sign metric-namespace metric]
(inc-or-dec-count sign metric-namespace metric nil))
([sign metric-namespaces metric additional-tags]
(let [metric-namespace (intercalate-dot metric-namespaces)
(let [metric-namespace (get-metric-namespaces metric-namespaces)
meter ^Meter (mk-meter metric-namespace metric (remove-topic-tag-for-old-namespace additional-tags metric-namespaces))]
(.mark meter (sign 1)))))

Expand All @@ -69,7 +75,7 @@
([metric-namespaces time-val]
(report-time metric-namespaces time-val nil))
([metric-namespaces time-val additional-tags]
(let [metric-namespace (intercalate-dot metric-namespaces)
(let [metric-namespace (get-metric-namespaces metric-namespaces)
histogram ^Histogram (mk-histogram metric-namespace "all" (remove-topic-tag-for-old-namespace additional-tags metric-namespaces))]
(.update histogram (int time-val)))))

Expand Down
43 changes: 43 additions & 0 deletions test/ziggurat/metrics_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@
(is (= 1 (.getCount meter)))
(is (= (apply str (interpose "." expected-metric-namespaces)) (:metric-namespaces @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "increases count on the meter - string as an argument"
(let [expected-metric-namespaces "metric-ns"
mk-meter-args (atom nil)
meter (Meter.)
expected-additional-tags input-additional-tags]
(with-redefs [metrics/mk-meter (fn [metric-namespaces metric additional-tags]
(is (= additional-tags expected-additional-tags))
(reset! mk-meter-args {:metric-namespaces metric-namespaces
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespaces metric input-additional-tags)
(is (= 1 (.getCount meter)))
(is (= expected-metric-namespaces (:metric-namespaces @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "increases count on the meter - w/o additional-tags argument"
(let [expected-metric-namespaces [expected-topic-entity-name "metric-ns"]
mk-meter-args (atom nil)
Expand Down Expand Up @@ -125,6 +139,20 @@
(is (zero? (.getCount meter)))
(is (= (apply str (interpose "." expected-metric-namespaces)) (:metric-namespaces @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "decreases count on the meter - string as an argument"
(let [expected-additional-tags input-additional-tags
expected-metric-namespaces "metric-ns"]
(with-redefs [metrics/mk-meter (fn [metric-namespaces metric additional-tags]
(is (= additional-tags expected-additional-tags))
(reset! mk-meter-args {:metric-namespaces metric-namespaces
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespaces metric input-additional-tags)
(is (= 1 (.getCount meter)))
(metrics/decrement-count expected-metric-namespaces metric input-additional-tags)
(is (zero? (.getCount meter)))
(is (= expected-metric-namespaces (:metric-namespaces @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "decreases count on the meter - without topic name on the namespace"
(let [expected-additional-tags input-additional-tags
expected-metric-namespaces ["metric-ns"]]
Expand Down Expand Up @@ -173,6 +201,21 @@
(is (= 1 (.getCount histogram)))
(is (= (apply str (interpose "." expected-metric-namespaces)) (:metric-namespaces @mk-histogram-args)))
(is (= "all" (:metric @mk-histogram-args))))))
(testing "updates time-val - string as an argument"
(let [expected-metric-namespaces "message-received-delay-histogram"
mk-histogram-args (atom nil)
reservoir (UniformReservoir.)
histogram (Histogram. reservoir)
expected-additional-tags input-additional-tags]
(with-redefs [metrics/mk-histogram (fn [metric-namespaces metric additional-tags]
(is (= additional-tags expected-additional-tags))
(reset! mk-histogram-args {:metric-namespaces metric-namespaces
:metric metric})
histogram)]
(metrics/report-time expected-metric-namespaces time-val input-additional-tags)
(is (= 1 (.getCount histogram)))
(is (= expected-metric-namespaces (:metric-namespaces @mk-histogram-args)))
(is (= "all" (:metric @mk-histogram-args))))))
(testing "updates time-val - w/o additional-tags argument"
(let [expected-metric-namespaces [expected-topic-entity-name "message-received-delay-histogram"]
mk-histogram-args (atom nil)
Expand Down

0 comments on commit 0bd6e94

Please sign in to comment.