-
Notifications
You must be signed in to change notification settings - Fork 20
/
types.go
214 lines (183 loc) · 5.75 KB
/
types.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package monitoring
import (
"context"
"sync"
"time"
"github.com/go-masonry/mortar/interfaces/monitor"
)
// *******************************************************************
// * Counter *
// *******************************************************************
type counter struct {
*tagsMetric
bricksCounter monitor.BricksCounter
extractors []monitor.ContextExtractor
}
func (c *counter) Inc() {
if counter, err := c.bricksCounter.WithTags(c.tags); c.shouldLogMetric(err) {
counter.Inc()
}
}
func (c *counter) Add(v float64) {
if counter, err := c.bricksCounter.WithTags(c.tags); c.shouldLogMetric(err) {
counter.Add(v)
}
}
func (c *counter) WithTags(tags monitor.Tags) monitor.TagsAwareCounter {
c.withTags(tags)
return c
}
func (c *counter) WithContext(ctx context.Context) monitor.TagsAwareCounter {
c.withContext(ctx, c.extractors)
return c
}
// *******************************************************************
// * Gauge *
// *******************************************************************
type gauge struct {
*tagsMetric
bricksGauge monitor.BricksGauge
extractors []monitor.ContextExtractor
}
// Set sets Gauge value
func (g *gauge) Set(v float64) {
if gauge, err := g.bricksGauge.WithTags(g.tags); g.shouldLogMetric(err) {
gauge.Set(v)
}
}
// Add adds (or subtracts if negative) from previously set value
func (g *gauge) Add(v float64) {
if gauge, err := g.bricksGauge.WithTags(g.tags); g.shouldLogMetric(err) {
gauge.Add(v)
}
}
// Inc adds 1
func (g *gauge) Inc() {
if gauge, err := g.bricksGauge.WithTags(g.tags); g.shouldLogMetric(err) {
gauge.Inc()
}
}
// Dec adds -1
func (g *gauge) Dec() {
if gauge, err := g.bricksGauge.WithTags(g.tags); g.shouldLogMetric(err) {
gauge.Dec()
}
}
func (g *gauge) WithTags(tags monitor.Tags) monitor.TagsAwareGauge {
g.withTags(tags)
return g
}
func (g *gauge) WithContext(ctx context.Context) monitor.TagsAwareGauge {
g.withContext(ctx, g.extractors)
return g
}
// *******************************************************************
// * histogram *
// *******************************************************************
type histogram struct {
*tagsMetric
bricksHistogram monitor.BricksHistogram
extractors []monitor.ContextExtractor
}
// Record value
func (h *histogram) Record(v float64) {
if histogram, err := h.bricksHistogram.WithTags(h.tags); h.shouldLogMetric(err) {
histogram.Record(v)
}
}
func (h *histogram) WithTags(tags monitor.Tags) monitor.TagsAwareHistogram {
h.withTags(tags)
return h
}
func (h *histogram) WithContext(ctx context.Context) monitor.TagsAwareHistogram {
h.withContext(ctx, h.extractors)
return h
}
// *******************************************************************
// * timer *
// *******************************************************************
type timer struct {
*tagsMetric
bricksTimer monitor.BricksTimer
extractors []monitor.ContextExtractor
}
// Record uses Histogram to record timed duration
// Since Histogram accepts float64 we will take the d.Seconds() which returns float64
func (t *timer) Record(d time.Duration) {
if timer, err := t.bricksTimer.WithTags(t.tags); t.shouldLogMetric(err) {
timer.Record(d)
}
}
func (t *timer) WithTags(tags monitor.Tags) monitor.TagsAwareTimer {
t.withTags(tags)
return t
}
func (t *timer) WithContext(ctx context.Context) monitor.TagsAwareTimer {
t.withContext(ctx, t.extractors)
return t
}
// *******************************************************************
// * tags helper *
// *******************************************************************
type tagsMetric struct {
sync.Mutex
tags monitor.Tags
onError func(error)
copied bool
}
func (tm *tagsMetric) withTags(tags monitor.Tags) {
tm.Lock()
defer tm.Unlock()
if !tm.copied {
var tagsCopy = monitor.Tags{}
for k, v := range tm.tags {
tagsCopy[k] = v
}
tm.tags = tagsCopy
tm.copied = true
}
for k, v := range tags {
tm.tags[k] = v
}
}
func (tm *tagsMetric) withContext(ctx context.Context, extractors []monitor.ContextExtractor) {
for _, extractor := range extractors {
extractedTags := extractor(ctx)
tm.withTags(extractedTags)
}
}
func (tm *tagsMetric) shouldLogMetric(err error) bool {
if err != nil {
tm.onError(err)
}
return err == nil
}
// Metric Constructors
func newCounterWithTags(bricksCounter monitor.BricksCounter, predefinedTags monitor.Tags, extractors []monitor.ContextExtractor, onError func(error)) monitor.TagsAwareCounter {
return &counter{
tagsMetric: &tagsMetric{tags: predefinedTags, onError: onError},
bricksCounter: bricksCounter,
extractors: extractors,
}
}
func newGaugeWithTags(bricksGauge monitor.BricksGauge, predefinedTags monitor.Tags, extractors []monitor.ContextExtractor, onError func(error)) monitor.TagsAwareGauge {
return &gauge{
tagsMetric: &tagsMetric{tags: predefinedTags, onError: onError},
bricksGauge: bricksGauge,
extractors: extractors,
}
}
func newHistogramWithTags(bricksHistogram monitor.BricksHistogram, predefinedTags monitor.Tags, extractors []monitor.ContextExtractor, onError func(error)) monitor.TagsAwareHistogram {
return &histogram{
tagsMetric: &tagsMetric{tags: predefinedTags, onError: onError},
bricksHistogram: bricksHistogram,
extractors: extractors,
}
}
func newTimerWithTags(bricksTimer monitor.BricksTimer, predefinedTags monitor.Tags, extractors []monitor.ContextExtractor, onError func(error)) monitor.TagsAwareTimer {
return &timer{
tagsMetric: &tagsMetric{tags: predefinedTags, onError: onError},
bricksTimer: bricksTimer,
extractors: extractors,
}
}