Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a configurable prefix.

In an organization where lots of teams and apps
are using statsd, it is important to prefix
stats to keep them separate, but a pain to
repeat this prefix in every stat in your app.
This lets you define it (optionally) in one place.

The format I use in my work is "team.app.hostname."
  • Loading branch information...
commit b642a7ee03acb623460968ed8dd63f8d18022966 1 parent 966ed3e
Mark Roghelia mjroghelia authored
Showing with 18 additions and 8 deletions.
  1. +11 −8 src/clj_statsd.clj
  2. +7 −0 test/clj_statsd/test.clj
19 src/clj_statsd.clj
View
@@ -15,11 +15,12 @@
(defn setup
"Initialize configuration"
- [host port]
+ [host port & opts]
(send sockagt #(or % (DatagramSocket.)))
- (swap! cfg #(or % {:random (Random.)
- :host (InetAddress/getByName host)
- :port port})))
+ (swap! cfg #(or % (merge {:random (Random.)
+ :host (InetAddress/getByName host)
+ :port port}
+ (apply hash-map opts)))))
(defn send-stat
"Send a raw metric over the network."
@@ -38,10 +39,12 @@
"Send a metric over the network, based on the provided sampling rate.
This should be a fully formatted statsd metric line."
[^String content rate]
- (cond
- (nil? @cfg) nil
- (>= rate 1.0) (send-stat content)
- (<= (.nextDouble ^Random (:random @cfg)) rate) (send-stat (format "%s|@%f" content rate))))
+ (let [prefix (:prefix @cfg)
+ content (if prefix (str prefix content) content)]
+ (cond
+ (nil? @cfg) nil
+ (>= rate 1.0) (send-stat content)
+ (<= (.nextDouble ^Random (:random @cfg)) rate) (send-stat (format "%s|@%f" content rate)))))
(defn increment
"Increment a counter at specified rate, defaults to a one increment
7 test/clj_statsd/test.clj
View
@@ -69,3 +69,10 @@
(with-sampled-timing "test.time" 1.0
(Thread/sleep 200))
(is (= @cnt 2)))))
+
+(deftest should-prefix
+ (with-redefs [cfg (atom nil)]
+ (setup "localhost" 8125 :prefix "test.stats.")
+ (should-send-expected-stat "test.stats.gorets:1|c" 2 2
+ (increment "gorets")
+ (increment :gorets))))
Please sign in to comment.
Something went wrong with that request. Please try again.