This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
/
memory.go
186 lines (147 loc) · 4.85 KB
/
memory.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
package client
import (
"net/http"
"sync"
"time"
"github.com/hellofresh/stats-go/bucket"
"github.com/hellofresh/stats-go/incrementer"
"github.com/hellofresh/stats-go/state"
"github.com/hellofresh/stats-go/timer"
)
// Metric is a type for storing single duration metric
type Metric struct {
Bucket string
Elapsed time.Duration
}
// Memory is Client implementation for tests
type Memory struct {
sync.Mutex
httpMetricCallback bucket.HTTPMetricNameAlterCallback
httpRequestSection string
unicode bool
TimerMetrics []Metric
CountMetrics map[string]int
StateMetrics map[string]int
}
// NewMemory builds and returns new Memory instance
func NewMemory(unicode bool) *Memory {
client := &Memory{unicode: unicode}
client.ResetHTTPRequestSection()
client.resetMetrics()
return client
}
func (c *Memory) resetMetrics() {
c.TimerMetrics = []Metric{}
c.CountMetrics = map[string]int{}
c.StateMetrics = map[string]int{}
}
// BuildTimer builds timer to track metric timings
func (c *Memory) BuildTimer() timer.Timer {
return &timer.Memory{}
}
// Close resets all collected stats
func (c *Memory) Close() error {
c.resetMetrics()
return nil
}
// TrackRequest tracks HTTP Request stats
func (c *Memory) TrackRequest(r *http.Request, t timer.Timer, success bool) Client {
b := bucket.NewHTTPRequest(c.httpRequestSection, r, success, c.httpMetricCallback, c.unicode)
i := incrementer.NewMemory()
if nil != t {
c.TimerMetrics = append(c.TimerMetrics, Metric{Bucket: b.Metric(), Elapsed: t.Finish()})
}
i.IncrementAll(b)
for metric, value := range i.Metrics() {
c.CountMetrics[metric] += value
}
return c
}
// TrackOperation tracks custom operation
func (c *Memory) TrackOperation(section string, operation *bucket.MetricOperation, t timer.Timer, success bool) Client {
b := bucket.NewPlain(section, operation, success, true)
i := incrementer.NewMemory()
if nil != t {
c.TimerMetrics = append(c.TimerMetrics, Metric{Bucket: b.MetricWithSuffix(), Elapsed: t.Finish()})
}
i.IncrementAll(b)
for metric, value := range i.Metrics() {
c.CountMetrics[metric] += value
}
return c
}
// TrackOperationN tracks custom operation with n diff
func (c *Memory) TrackOperationN(section string, operation *bucket.MetricOperation, t timer.Timer, n int, success bool) Client {
b := bucket.NewPlain(section, operation, success, true)
i := incrementer.NewMemory()
if nil != t {
c.TimerMetrics = append(c.TimerMetrics, Metric{Bucket: b.MetricWithSuffix(), Elapsed: t.Finish()})
}
i.IncrementAllN(b, n)
for metric, value := range i.Metrics() {
c.CountMetrics[metric] += value
}
return c
}
// TrackMetric tracks custom metric, w/out ok/fail additional sections
func (c *Memory) TrackMetric(section string, operation *bucket.MetricOperation) Client {
b := bucket.NewPlain(section, operation, true, true)
i := incrementer.NewMemory()
i.Increment(b.Metric())
i.Increment(b.MetricTotal())
for metric, value := range i.Metrics() {
c.CountMetrics[metric] += value
}
return c
}
// TrackMetricN tracks custom metric with n diff, w/out ok/fail additional sections
func (c *Memory) TrackMetricN(section string, operation *bucket.MetricOperation, n int) Client {
b := bucket.NewPlain(section, operation, true, true)
i := incrementer.NewMemory()
i.IncrementN(b.Metric(), n)
i.IncrementN(b.MetricTotal(), n)
for metric, value := range i.Metrics() {
c.CountMetrics[metric] += value
}
return c
}
// TrackState tracks metric absolute value
func (c *Memory) TrackState(section string, operation *bucket.MetricOperation, value int) Client {
b := bucket.NewPlain(section, operation, true, true)
s := state.NewMemory()
s.Set(b.Metric(), value)
for metric, value := range s.Metrics() {
c.StateMetrics[metric] = value
}
return c
}
// SetHTTPMetricCallback sets callback handler that allows metric operation alteration for HTTP Request
func (c *Memory) SetHTTPMetricCallback(callback bucket.HTTPMetricNameAlterCallback) Client {
c.Lock()
defer c.Unlock()
c.httpMetricCallback = callback
return c
}
// GetHTTPMetricCallback gets callback handler that allows metric operation alteration for HTTP Request
func (c *Memory) GetHTTPMetricCallback() bucket.HTTPMetricNameAlterCallback {
c.Lock()
defer c.Unlock()
return c.httpMetricCallback
}
// SetHTTPRequestSection sets metric section for HTTP Request metrics
func (c *Memory) SetHTTPRequestSection(section string) Client {
c.Lock()
defer c.Unlock()
c.httpRequestSection = section
return c
}
// ResetHTTPRequestSection resets metric section for HTTP Request metrics to default value that is "request"
func (c *Memory) ResetHTTPRequestSection() Client {
return c.SetHTTPRequestSection(bucket.SectionRequest)
}
// Handler returns metrics endpoint for prometheus backend
func (c *Memory) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
})
}