Skip to content

Commit

Permalink
add clojure client to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Schauenberg committed Sep 23, 2013
1 parent 7c9ba1a commit d77b893
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions examples/statsd.clj
@@ -0,0 +1,49 @@
(ns statsd-client
"a simple StatsD client written in Clojure
Usage:
statsd-client/increment 'foo
statsd-client/decrement 'foo
statsd-client/increment 'foo 1
statsd-client/decrement 'foo 1
statsd-client/gauge 'foo 1
statsd-client/timing 'foo 1
"
(:import (java.net InetAddress DatagramPacket DatagramSocket)))

(def server-address "127.0.0.1")
(def server-port 8125)

; UDP helper functions
(defn make-socket
([] (new DatagramSocket))
([port] (new DatagramSocket port)))

(defn send-data [send-socket ip port data]
(let [ipaddress (InetAddress/getByName ip),
send-packet (new DatagramPacket (.getBytes data) (.length data) ipaddress port)]
(.send send-socket send-packet)))

(defn make-send [ip port]
(let [send-socket (make-socket)]
(fn [data] (send-data send-socket ip port data))))

(def send-msg (make-send server-address server-port))

; statsd client functions
(defn increment
([metric] (increment metric 1))
([metric value]
(send-msg (str metric ":" value "|c"))))

(defn decrement
([metric] (increment metric -1))
([metric value]
(send-msg (str metric ":" value "|c"))))

(defn timing [metric value]
(send-msg (str metric ":" value "|ms")))

(defn gauge [metric value]
(send-msg (str metric ":" value "|g")))

0 comments on commit d77b893

Please sign in to comment.