-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
48 lines (39 loc) · 1.35 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(ns ddstatsd-clj.core
(:require [clojure.string :as str]
[ddstatsd-clj.network :refer [send-packet]]
[ddstatsd-clj.specs :as spec])
(:gen-class))
(defn- get-type-token
[type-name]
(case type-name
"increment" "c"
"decrement" "c"
"timing" "ms"
"gauge" "g"
"default"))
(defn- format-tags
[tags]
(str/join "," (map (fn [[key value]] (str/join ":" [(name key) value])) tags)))
(defn- format-message
[metric-name type value & [tags]]
(if-not tags
(format "%s:%d|%s" metric-name value (get-type-token type))
(format "%s:%d|%s|#%s" metric-name value (get-type-token type) (format-tags tags))))
(defn send-metric
"
Sends the metric value to dogstatsD.
Example:
```clojure
(def connection-map {:host \"localhost\" :port 8125})
;; Metric without any tag
(send-metric connection-map \"my_super_metric.count\" \"increment\" 1)
;; Metric with some tag
(send-metric connection-map \"my_another_super_metric.count\" \"increment\" 10 {:env \"production\"})
```
"
[connection-map metric-name type value & [tags]]
{:pre [(spec/check-input ::spec/metric-type type)
(spec/check-input ::spec/connection-map connection-map)
(when tags
(spec/check-input ::spec/tags tags))]}
(send-packet connection-map (format-message metric-name type value tags)))