forked from benthosdev/benthos
/
local.go
187 lines (158 loc) · 4.87 KB
/
local.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
// Copyright (c) 2018 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, sub to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"sync"
"sync/atomic"
"github.com/Jeffail/benthos/lib/log"
)
//------------------------------------------------------------------------------
// LocalStat is a representation of a single metric stat. Interactions with this
// stat are thread safe.
type LocalStat struct {
value *int64
}
// Incr increments a metric by an amount.
func (l *LocalStat) Incr(count int64) error {
atomic.AddInt64(l.value, count)
return nil
}
// Decr decrements a metric by an amount.
func (l *LocalStat) Decr(count int64) error {
atomic.AddInt64(l.value, -count)
return nil
}
// Timing sets a timing metric.
func (l *LocalStat) Timing(delta int64) error {
atomic.StoreInt64(l.value, delta)
return nil
}
// Set sets a gauge metric.
func (l *LocalStat) Set(value int64) error {
atomic.StoreInt64(l.value, value)
return nil
}
//------------------------------------------------------------------------------
// Local is a metrics aggregator that stores metrics locally.
type Local struct {
flatCounters map[string]*int64
flatTimings map[string]*int64
sync.Mutex
}
// NewLocal creates and returns a new Local aggregator.
func NewLocal() *Local {
return &Local{
flatCounters: map[string]*int64{},
flatTimings: map[string]*int64{},
}
}
//------------------------------------------------------------------------------
// GetCounters returns a map of metric paths to counters.
func (l *Local) GetCounters() map[string]int64 {
l.Lock()
localFlatCounters := make(map[string]int64, len(l.flatCounters))
for k := range l.flatCounters {
localFlatCounters[k] = atomic.LoadInt64(l.flatCounters[k])
}
l.Unlock()
return localFlatCounters
}
// GetTimings returns a map of metric paths to timers.
func (l *Local) GetTimings() map[string]int64 {
l.Lock()
localFlatTimings := make(map[string]int64, len(l.flatTimings))
for k := range l.flatTimings {
localFlatTimings[k] = atomic.LoadInt64(l.flatTimings[k])
}
l.Unlock()
return localFlatTimings
}
//------------------------------------------------------------------------------
// GetCounter returns a stat counter object for a path.
func (l *Local) GetCounter(path string) StatCounter {
l.Lock()
ptr, exists := l.flatCounters[path]
if !exists {
var ctr int64
ptr = &ctr
l.flatCounters[path] = ptr
}
l.Unlock()
return &LocalStat{
value: ptr,
}
}
// GetTimer returns a stat timer object for a path.
func (l *Local) GetTimer(path string) StatTimer {
l.Lock()
ptr, exists := l.flatTimings[path]
if !exists {
var ctr int64
ptr = &ctr
l.flatTimings[path] = ptr
}
l.Unlock()
return &LocalStat{
value: ptr,
}
}
// GetGauge returns a stat gauge object for a path.
func (l *Local) GetGauge(path string) StatGauge {
l.Lock()
ptr, exists := l.flatCounters[path]
if !exists {
var ctr int64
ptr = &ctr
l.flatCounters[path] = ptr
}
l.Unlock()
return &LocalStat{
value: ptr,
}
}
// GetCounterVec returns a stat counter object for a path with the labels
// discarded.
func (l *Local) GetCounterVec(path string, n []string) StatCounterVec {
return fakeCounterVec(func() StatCounter {
return l.GetCounter(path)
})
}
// GetTimerVec returns a stat timer object for a path with the labels
// discarded.
func (l *Local) GetTimerVec(path string, n []string) StatTimerVec {
return fakeTimerVec(func() StatTimer {
return l.GetTimer(path)
})
}
// GetGaugeVec returns a stat timer object for a path with the labels
// discarded.
func (l *Local) GetGaugeVec(path string, n []string) StatGaugeVec {
return fakeGaugeVec(func() StatGauge {
return l.GetGauge(path)
})
}
// SetLogger does nothing.
func (l *Local) SetLogger(log log.Modular) {}
// Close stops the Local object from aggregating metrics and cleans up
// resources.
func (l *Local) Close() error {
return nil
}
//------------------------------------------------------------------------------