Skip to content

Commit

Permalink
Adds update-histogram to clj-statsd-wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
theanirudhvyas committed Apr 22, 2020
1 parent c7a335c commit 3bfc478
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
15 changes: 10 additions & 5 deletions src/ziggurat/clj_statsd_metrics_wrapper.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns ziggurat.clj-statsd-metrics-wrapper
(:require [clj-statsd :as statsd])
(:import (ziggurat.metrics_interface MetricsLib)))
(:require [clj-statsd :as statsd]
[ziggurat.metrics-interface :refer [MetricsLib]]))

(def rate 1.0)

Expand All @@ -27,12 +27,17 @@

(defn update-counter [namespace metric tags value]
(let [final-tags (build-tags tags)
signed-value value
final-metric (str namespace "." metric)]
(statsd/increment final-metric signed-value rate final-tags)))
(statsd/increment final-metric value rate final-tags)))

(defn update-histogram [namespace metric tags value]
(let [final-tags (build-tags tags)
final-metric (str namespace "." metric)]
(statsd/timing final-metric value rate final-tags)))

(deftype CljStatsd []
MetricsLib
(initialize [this statsd-config] (initialize statsd-config))
(terminate [this] (terminate))
(update-counter [this namespace metric tags signed-val] (update-counter namespace metric tags signed-val)))
(update-counter [this namespace metric tags signed-val] (update-counter namespace metric tags signed-val))
(update-histogram [this namespace metric tags value] (update-histogram namespace metric tags value)))
47 changes: 30 additions & 17 deletions test/ziggurat/clj_statsd_metrics_wrapper_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,33 @@
(is (nil? @statsd/cfg))
(is (nil? (await statsd/sockagt))))))

(deftest update-counter-test
(testing "it calls statsd/increment with the correctly formatted arguments"
(let [namespace "namespace"
metric "metric"
expected-metric "namespace.metric"
tags {:key :val :foo "bar" :foobar 2}
expected-tags ["key:val" "foo:bar" "foobar:2"]
value -1
increment-called (atom false)]
(with-redefs [statsd/increment (fn [k v rate tags]
(is (= expected-metric k))
(is (= value v))
(is (= clj-statsd-wrapper/rate rate))
(is (= expected-tags tags))
(reset! increment-called true))]
(update-counter namespace metric tags value)
(is (true? @increment-called))))))
(let [namespace "namespace"
metric "metric"
expected-metric "namespace.metric"
tags {:key :val :foo "bar" :foobar 2}
expected-tags ["key:val" "foo:bar" "foobar:2"]]
(deftest update-counter-test
(testing "it calls statsd/increment with the correctly formatted arguments"
(let [value -1
increment-called (atom false)]
(with-redefs [statsd/increment (fn [k v rate tags]
(is (= expected-metric k))
(is (= value v))
(is (= clj-statsd-wrapper/rate rate))
(is (= expected-tags tags))
(reset! increment-called true))]
(update-counter namespace metric tags value)
(is (true? @increment-called))))))

(deftest update-histogram-test
(testing "it calls statsd/timing with the correctly formatted arguments"
(let [value 100
timing-called (atom false)]
(with-redefs [statsd/timing (fn [k v rate tags]
(is (= expected-metric k))
(is (= value v))
(is (= clj-statsd-wrapper/rate rate))
(is (= expected-tags tags))
(reset! timing-called true))]
(update-histogram namespace metric tags value)
(is (true? @timing-called)))))))

0 comments on commit 3bfc478

Please sign in to comment.