Skip to content

Commit

Permalink
Merge pull request #86 from macalimlim/fix-n-count
Browse files Browse the repository at this point in the history
Fix n count
  • Loading branch information
theanirudhvyas committed Aug 27, 2019
2 parents e3b55ac + d7ae342 commit 741f21f
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. This change

## Unreleased Changes

## 3.0.0-alpha.6 - 2019-08-22
- Fix increment/decrement count to accept both number and map

## 3.0.0-alpha.5 - 2019-08-20
- Exposes Java methods for init, config, producer, sentry, metrics, fixtures namespaces
- Adds util methods for converting data types between java and Clojure
Expand All @@ -24,6 +27,9 @@ All notable changes to this project will be documented in this file. This change
## 3.0.0-alpha - 2019-06-21
- Upgrades kafka streams to version 2.1. Please refer [this](UpgradeGuide.md) to upgrade

## 2.12.4 - 2019-08-22
- Fix increment/decrement count to accept both number and map

## 2.12.3 - 2019-07-26
- Fix functions to either take vector or string as input

Expand Down
22 changes: 17 additions & 5 deletions src/ziggurat/metrics.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,25 @@
(intercalate-dot metric-namespaces)
metric-namespaces))

(defn- get-v
[f d v]
(if (f v) v d))

(def ^:private get-int (partial get-v number? 1))

(def ^:private get-map (partial get-v map? {}))

(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 (get-metric-namespaces metric-namespaces)
meter ^Meter (mk-meter metric-namespace metric additional-tags)]
(.mark meter (sign 1)))))
(inc-or-dec-count sign metric-namespace metric 1 {}))
([sign metric-namespace metric n-or-additional-tags]
(inc-or-dec-count sign metric-namespace metric (get-int n-or-additional-tags) (get-map n-or-additional-tags)))
([sign metric-namespace metric n additional-tags]
(inc-or-dec-count sign {:metric-namespace metric-namespace :metric metric :n n :additional-tags additional-tags}))
([sign {:keys [metric-namespace metric n additional-tags]}]
(let [metric-ns (get-metric-namespaces metric-namespace)
meter ^Meter (mk-meter metric-ns metric (get-map additional-tags))]
(.mark meter (sign (get-int n))))))

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

Expand Down
78 changes: 54 additions & 24 deletions test/ziggurat/metrics_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
(deftest increment-count-test
(let [metric "metric3"
expected-topic-entity-name "expected-topic-entity-name"
input-additional-tags {:topic_name expected-topic-entity-name}]
input-additional-tags {:topic_name expected-topic-entity-name}
expected-n 1]
(testing "increases count on the meter - vector as an argument"
(let [expected-metric-namespace ["metric" "ns"]
mk-meter-args (atom nil)
Expand All @@ -58,8 +59,36 @@
(reset! mk-meter-args {:metric-namespace metric-namespace
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespace metric input-additional-tags)
(is (= 1 (.getCount meter)))
(metrics/increment-count expected-metric-namespace metric expected-n input-additional-tags)
(is (= expected-n (.getCount meter)))
(is (= (metrics/intercalate-dot expected-metric-namespace) (:metric-namespace @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "increases count on the meter - 3rd argument is a number"
(let [expected-metric-namespace ["metric" "ns"]
mk-meter-args (atom nil)
meter (Meter.)
expected-additional-tags {}]
(with-redefs [metrics/mk-meter (fn [metric-namespace metric additional-tags]
(is (= additional-tags expected-additional-tags))
(reset! mk-meter-args {:metric-namespace metric-namespace
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespace metric expected-n)
(is (= expected-n (.getCount meter)))
(is (= (metrics/intercalate-dot expected-metric-namespace) (:metric-namespace @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "increases count on the meter - 3rd argument is a map"
(let [expected-metric-namespace ["metric" "ns"]
mk-meter-args (atom nil)
meter (Meter.)
expected-additional-tags {}]
(with-redefs [metrics/mk-meter (fn [metric-namespace metric additional-tags]
(is (= additional-tags expected-additional-tags))
(reset! mk-meter-args {:metric-namespace metric-namespace
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespace metric expected-additional-tags)
(is (= expected-n (.getCount meter)))
(is (= (metrics/intercalate-dot expected-metric-namespace) (:metric-namespace @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "increases count on the meter - string as an argument"
Expand All @@ -72,36 +101,36 @@
(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/increment-count expected-metric-namespaces metric expected-n input-additional-tags)
(is (= expected-n (.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-namespace "metric-ns"
mk-meter-args (atom nil)
meter (Meter.)
expected-additional-tags nil]
expected-additional-tags {}]
(with-redefs [metrics/mk-meter (fn [metric-namespace metric additional-tags]
(is (= additional-tags expected-additional-tags))
(reset! mk-meter-args {:metric-namespace metric-namespace
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespace metric)
(is (= 1 (.getCount meter)))
(is (= expected-n (.getCount meter)))
(is (= expected-metric-namespace (:metric-namespace @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "increases count on the meter when additional-tags is nil"
(let [expected-metric-namespace "metric-ns"
mk-meter-args (atom nil)
meter (Meter.)
expected-additional-tags nil]
expected-additional-tags {}]
(with-redefs [metrics/mk-meter (fn [metric-namespace metric additional-tags]
(is (= additional-tags expected-additional-tags))
(reset! mk-meter-args {:metric-namespace metric-namespace
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespace metric expected-additional-tags)
(is (= 1 (.getCount meter)))
(metrics/increment-count expected-metric-namespace metric expected-n expected-additional-tags)
(is (= expected-n (.getCount meter)))
(is (= expected-metric-namespace (:metric-namespace @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "-incrementCount calls increment-count with the correct arguments"
Expand Down Expand Up @@ -133,7 +162,8 @@
metric "metric3"
mk-meter-args (atom nil)
meter (Meter.)
input-additional-tags {:topic_name expected-topic-name}]
input-additional-tags {:topic_name expected-topic-name}
expected-n 1]
(testing "decreases count on the meter - vector as an argument"
(let [expected-additional-tags input-additional-tags
expected-metric-namespace ["metric" "ns"]]
Expand All @@ -142,9 +172,9 @@
(reset! mk-meter-args {:metric-namespace metric-namespace
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespace metric input-additional-tags)
(is (= 1 (.getCount meter)))
(metrics/decrement-count expected-metric-namespace metric input-additional-tags)
(metrics/increment-count expected-metric-namespace metric expected-n input-additional-tags)
(is (= expected-n (.getCount meter)))
(metrics/decrement-count expected-metric-namespace metric expected-n input-additional-tags)
(is (zero? (.getCount meter)))
(is (= (metrics/intercalate-dot expected-metric-namespace) (:metric-namespace @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
Expand All @@ -156,9 +186,9 @@
(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)
(metrics/increment-count expected-metric-namespaces metric expected-n input-additional-tags)
(is (= expected-n (.getCount meter)))
(metrics/decrement-count expected-metric-namespaces metric expected-n input-additional-tags)
(is (zero? (.getCount meter)))
(is (= expected-metric-namespaces (:metric-namespaces @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
Expand All @@ -170,23 +200,23 @@
(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)
(metrics/increment-count expected-metric-namespaces metric expected-n input-additional-tags)
(is (= expected-n (.getCount meter)))
(metrics/decrement-count expected-metric-namespaces metric expected-n input-additional-tags)
(is (zero? (.getCount meter)))
(is (= (metrics/intercalate-dot expected-metric-namespaces) (:metric-namespaces @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
(testing "decreases count on the meter when additional-tags is nil"
(let [expected-additional-tags nil
(let [expected-additional-tags {}
expected-metric-namespace "metric-ns"]
(with-redefs [metrics/mk-meter (fn [metric-namespace metric additional-tags]
(is (= additional-tags expected-additional-tags))
(reset! mk-meter-args {:metric-namespace metric-namespace
:metric metric})
meter)]
(metrics/increment-count expected-metric-namespace metric expected-additional-tags)
(is (= 1 (.getCount meter)))
(metrics/decrement-count expected-metric-namespace metric expected-additional-tags)
(metrics/increment-count expected-metric-namespace metric expected-n expected-additional-tags)
(is (= expected-n (.getCount meter)))
(metrics/decrement-count expected-metric-namespace metric expected-n expected-additional-tags)
(is (zero? (.getCount meter)))
(is (= expected-metric-namespace (:metric-namespace @mk-meter-args)))
(is (= metric (:metric @mk-meter-args))))))
Expand Down

0 comments on commit 741f21f

Please sign in to comment.