Skip to content

Clean up the API for consistency #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:license {:name "The MIT License (MIT)"
:url "http://opensource.org/licenses/MIT"
:comments "Copyright (c) 2018 Unbounce Marketing Solutions Inc."}
:profiles {:dev {:dependencies [[org.clojure/test.check "0.9.0"]]}}
:dependencies [[org.clojure/clojure "1.9.0"]
[com.datadoghq/java-dogstatsd-client "2.5"]]
:global-vars {*warn-on-reflection* true})
83 changes: 48 additions & 35 deletions src/com/unbounce/dogstatsd/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
(:import [com.timgroup.statsd StatsDClient NonBlockingStatsDClient ServiceCheck NoOpStatsDClient]))

;; In case setup! is not called, this prevents nullpointer exceptions i.e. Unit tests
(defonce ^:private ^StatsDClient client
(NoOpStatsDClient.))
(defonce ^:private ^StatsDClient client (NoOpStatsDClient.))

(defn str-array [tags]
(def ^:private ^"[Ljava.lang.String;" -empty-array (into-array String []))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to look up the ^"[Ljava.lang.String;" thing and didn't know what to google for 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a typehint for a string array


(defn ^"[Ljava.lang.String;" str-array [tags]
(into-array String tags))

(defn setup!
Expand All @@ -32,46 +33,58 @@
tags)))))

(defn increment
[metric & {:keys [tags sample-rate]}]
(let [tags (str-array tags)]
(if sample-rate
(.incrementCounter client metric sample-rate tags)
(.incrementCounter client metric tags))))
([metric]
(.incrementCounter client metric -empty-array))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW i split it up this way because I think this would be a faster path on the critical path because it does away w/ adding sample rate or tags (by calling .incrementCounter directly)

([metric {:keys [tags sample-rate]}]
(let [tags (str-array tags)]
(if sample-rate
(.incrementCounter client metric sample-rate tags)
(.incrementCounter client metric tags)))))

(defn decrement
[metric & {:keys [tags sample-rate]}]
(let [tags (str-array tags)]
(if sample-rate
(.decrementCounter client metric sample-rate tags)
(.decrementCounter client metric tags))))
([metric]
(.decrementCounter client metric -empty-array))
([metric {:keys [tags sample-rate]}]
(let [tags (str-array tags)]
(if sample-rate
(.decrementCounter client metric sample-rate tags)
(.decrementCounter client metric tags)))))

(defn gauge
[metric value {:keys [sample-rate tags]}]
(let [value (double value)
tags (str-array tags)
sample-rate (when sample-rate (double sample-rate))
f (fn [^String metric ^double value {:keys [^double sample-rate #^"[Ljava.lang.String;" tags]}]
(if sample-rate
(.recordGaugeValue client metric value sample-rate tags)
(.recordGaugeValue client metric value tags)))]
(f metric value {:sample-rate sample-rate
:tags tags})))
([^String metric ^Double value]
(.recordGaugeValue client metric value -empty-array))
([^String metric ^Double value {:keys [sample-rate tags]}]
(let [tags (str-array tags)
sample-rate ^Double sample-rate]
(if sample-rate
(.recordGaugeValue client metric value sample-rate tags)
(.recordGaugeValue client metric value tags)))))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gzmask I cleaned this API up a bit to remove the need to create an anonymous function

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason of the anonymous function is that I wanted to get rid of the propagate effect of type annotations - so that clojure code remains clojurish and Java types are localized to where the interop happens. Not sure if this is a good practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm uncertain what propogate effects you're thinking of.

From the outside you won't need to know about the typehints. The typehints are used to help the clojure compiler pick a function rather than use reflection.


(defn histogram
[metric value {:keys [sample-rate tags]}]
(let [value (double value)
tags (str-array tags)
sample-rate (when sample-rate (double sample-rate))
f (fn [^String metric ^double value {:keys [^double sample-rate #^"[Ljava.lang.String;" tags]}]
(if sample-rate
(.recordHistogramValue client metric value sample-rate tags)
(.recordHistogramValue client metric value tags)))]
(f metric value {:sample-rate sample-rate
:tags tags})))
([^String metric ^Double value]
(.recordHistogramValue client metric value -empty-array))
([^String metric ^Double value {:keys [sample-rate tags]}]
(let [tags (str-array tags)
sample-rate ^Double sample-rate]
(if sample-rate
(.recordHistogramValue client metric value sample-rate tags)
(.recordHistogramValue client metric value tags)))))

(defmacro time!
"Times the body and submits the execution time to metric"
[metric opts & body]
"Times the body and records the execution time (in msecs) as a histogram.

Takes opts is a map of :sample-rate and :tags to apply to the histogram

Examples:

(statsd/time! [\"my.metric\"]
(Thread/sleep 1000))

(statsd/time! [\"my.metric.with.tags\" {:tags #{\"foo\" :sample-rate 0.3}}]
(Thread/sleep 1000))

"
[[metric opts] & body]
`(let [t0# (System/currentTimeMillis)
res# (do ~@body)]
(histogram ~metric (- (System/currentTimeMillis) t0#) ~opts)
Expand Down
29 changes: 14 additions & 15 deletions test/com/unbounce/dogstatsd/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,34 @@

(s/def ::sample-rate number?)
(s/def ::tags (s/coll-of string?))
(s/def ::opts (s/keys :req-un [::tags ::sample-rate]))
(s/def ::opts (s/keys :opt-un [::tags ::sample-rate]))

(s/fdef sut/str-array
:args (s/cat :tags ::tags)
:ret #(instance? java.lang.Object %))

(s/fdef sut/increment
:args (s/cat :metric string?
:opts (s/* (s/alt :_ (s/cat :_ (partial = :tags)
:_ ::tags)
:_ (s/cat :_ (partial = :sample-rate)
:_ ::sample-rate))))
:opts (s/? ::opts))
:ret nil?)

(s/fdef sut/decrement
:args (s/cat :metric string?
:opts (s/* (s/alt :_ (s/cat :_ (partial = :tags)
:_ ::tags)
:_ (s/cat :_ (partial = :sample-rate)
:_ ::sample-rate))))
:opts (s/? ::opts))
:ret nil?)


(s/fdef sut/gauge
:args (s/cat :metric string?
:value number?
:opts ::opts)
:opts (s/? ::opts))
:ret nil?)


(s/fdef sut/histogram
:args (s/cat :metric string?
:value number?
:opts ::opts)
:opts (s/? ::opts))
:ret nil?)

(t/deftest datadog-statsd-metrics
Expand All @@ -50,16 +44,21 @@
(stest/instrument `sut/gauge)
(stest/instrument `sut/histogram)

(sut/setup!)

(stest/check `sut/str-array)
(stest/check `sut/increment)
(stest/check `sut/decrement)
(stest/check `sut/gauge)
(stest/check `sut/histogram)

(t/are [x y] (= x y)
nil (sut/increment "asdf" :tags ["asdf"] :sample-rate 1)
nil (sut/decrement "asdf" :tags ["asdf"] :sample-rate 1)
nil (sut/increment "asdf")
nil (sut/increment "asdf" {:tags ["asdf"] :sample-rate 1})

nil (sut/decrement "asdf")
nil (sut/decrement "asdf" {:tags ["asdf"] :sample-rate 1})

nil (sut/gauge "asdf" 20)
nil (sut/gauge "asdf" 20 {:tags ["asdf" "asdf"] :sample-rate 1})

nil (sut/histogram "asdf" 20)
nil (sut/histogram "asdf" 20 {:tags ["asdf" "asdf"] :sample-rate 1}))))