-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric.clj
109 lines (85 loc) · 3.18 KB
/
metric.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(ns active.clojure.logger.metric
"Facilities for logging metrics."
(:require [active.clojure.logger.config.riemann :as riemann-config]
[active.clojure.logger.internal :as internal]
[active.clojure.monad :as monad]
[active.clojure.record :refer [define-record-type]]))
;;;; Configuration
(def metrics-config-default :events)
(defonce metrics-config (atom metrics-config-default))
(defn configure-metrics-logging
"Returns an object that can be fed to
[[set-global-log-metrics-config!]]."
[riemann-config desc]
(case desc
:events :events
:riemann (riemann-config/make-riemann-config riemann-config)))
(defn set-global-log-metrics-config!
[scc]
(reset! metrics-config scc))
(defn reset-global-log-metrics-config!
"Reset to back to default, if the config equals `compare`."
[compare]
(swap! metrics-config #(if (= % compare) metrics-config-default %)))
;;;; Data definition and DSL
(define-record-type LogMetric
(make-log-metric namespace label value map) log-metric?
[namespace log-metric-namespace
^{:doc "String"} label log-metric-label
^{:doc "Scalar value"} value log-metric-value
^{:doc "Map with more data or `nil`, see [[log-context-keys]]."}
map log-metric-map])
(define-record-type ^{:doc "Get the system time in milliseconds"} GetMilliTime
(make-get-milli-time) get-milli-time? [])
;;; Actions
(def get-milli-time (make-get-milli-time))
(defn log-metric-to-events!
[namespace label value mp]
(internal/log-event!-internal "metric"
namespace
:info
(merge mp {:label label :metric value})
(delay
[(str "Metric " label " = " value)])))
(defn log-metric-to-riemann!
[config label value mp]
(riemann-config/send-event-to-riemann! config "metric" mp {:label label :metric value}))
(defn log-metric!-internal
[namespace label value mp]
(let [mp (internal/sanitize-context mp)
scconf @metrics-config]
(case scconf
:events (log-metric-to-events! namespace label value mp)
(log-metric-to-riemann! scconf label value mp))))
(defn get-milli-time!
[]
(/ (double (System/nanoTime)) 1000000.0))
(defmacro log-metric!
([?label ?value]
`(internal/log-metric!-internal ~(str *ns*) ~?label ~?value nil))
([?label ?value ?mp]
`(internal/log-metric!-internal ~(str *ns*) ~?label ~?value ~?mp)))
(defmacro log-metric
([?label ?value]
`(make-log-metric ~(str *ns*) ~?label ~?value nil))
([?label ?value ?mp]
`(make-log-metric ~(str *ns*) ~?label ~?value ~?mp))
([?label ?value ?mp ?ns]
`(make-log-metric ~?ns ~?label ~?value ~?mp)))
;;;; Interpreter
(defn run-log-metric
[run-any env mstate m]
(cond
(log-metric? m)
(do
(log-metric!-internal (log-metric-namespace m)
(log-metric-label m)
(log-metric-value m)
(log-metric-map m))
[nil mstate])
(get-milli-time? m)
[(get-milli-time!) mstate]
:else
monad/unknown-command))
(def log-metrics-command-config
(monad/make-monad-command-config run-log-metric {} {}))