-
Notifications
You must be signed in to change notification settings - Fork 31
/
operations.clj
121 lines (94 loc) · 2.83 KB
/
operations.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
110
111
112
113
114
115
116
117
118
119
120
121
(ns iapetos.operations
(:import [io.prometheus.client
Counter$Child
Histogram$Child
Histogram$Timer
Gauge$Child
Gauge$Timer
Summary$Child
Summary$Timer]))
;; ## Operation Protocols
(defprotocol ReadableCollector
(read-value [this]))
(defprotocol IncrementableCollector
(increment* [this amount]))
(defprotocol DecrementableCollector
(decrement* [this amount]))
(defprotocol SettableCollector
(set-value [this value])
(set-value-to-current-time [this]))
(defprotocol ObservableCollector
(observe [this amount]))
(defprotocol TimeableCollector
(start-timer [this]))
;; ## Derived Functions
(defn increment
([this] (increment this 1.0))
([this amount] (increment* this amount)))
(defn decrement
([this] (decrement this 1.0))
([this amount] (decrement* this amount)))
;; ## Counter
(extend-type Counter$Child
ReadableCollector
(read-value [this]
(.get ^Counter$Child this))
IncrementableCollector
(increment* [this amount]
(.inc ^Counter$Child this (double amount))))
;; ## Gauge
(extend-type Gauge$Child
ReadableCollector
(read-value [this]
(.get ^Gauge$Child this))
IncrementableCollector
(increment* [this amount]
(.inc ^Gauge$Child this (double amount)))
DecrementableCollector
(decrement* [this amount]
(.dec ^Gauge$Child this (double amount)))
ObservableCollector
(observe [this amount]
(.set ^Gauge$Child this (double amount)))
SettableCollector
(set-value [this value]
(.set ^Gauge$Child this (double value)))
(set-value-to-current-time [this]
(.setToCurrentTime ^Gauge$Child this))
TimeableCollector
(start-timer [this]
(let [^Gauge$Timer t (.startTimer ^Gauge$Child this)]
#(.setDuration t))))
;; ## Histogram
(extend-type Histogram$Child
ReadableCollector
(read-value [this]
(let [^io.prometheus.client.Histogram$Child$Value value
(.get ^Histogram$Child this)
buckets (vec (.-buckets value))]
{:sum (.-sum value)
:count (last buckets)
:buckets buckets}))
ObservableCollector
(observe [this amount]
(.observe ^Histogram$Child this (double amount)))
TimeableCollector
(start-timer [this]
(let [^Histogram$Timer t (.startTimer ^Histogram$Child this)]
#(.observeDuration t))))
;; ## Summary
(extend-type Summary$Child
ReadableCollector
(read-value [this]
(let [^io.prometheus.client.Summary$Child$Value value
(.get ^Summary$Child this)]
{:sum (.-sum value)
:count (.-count value)
:quantiles (into {} (.-quantiles value))}))
ObservableCollector
(observe [this amount]
(.observe ^Summary$Child this (double amount)))
TimeableCollector
(start-timer [this]
(let [^Summary$Timer t (.startTimer ^Summary$Child this)]
#(.observeDuration t))))