forked from benthosdev/benthos
/
combine.go
191 lines (159 loc) · 3.81 KB
/
combine.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
package metrics
import "github.com/dafanshu/benthos/v3/lib/log"
//------------------------------------------------------------------------------
// combinedWrapper wraps two existing Type implementations. Both implementations
// are fed all metrics.
type combinedWrapper struct {
t1 Type
t2 Type
}
// Combine returns a Type implementation that feeds metrics into two underlying
// Type implementations.
func Combine(t1, t2 Type) Type {
return &combinedWrapper{
t1: t1,
t2: t2,
}
}
func unwrapMetric(t Type) Type {
u, ok := t.(interface {
Unwrap() Type
})
if ok {
t = u.Unwrap()
}
return t
}
// Unwrap to the underlying metrics type.
func (c *combinedWrapper) Unwrap() Type {
t1 := unwrapMetric(c.t1)
t2 := unwrapMetric(c.t2)
if t1 == t2 {
return t1
}
return &combinedWrapper{
t1: t1,
t2: t2,
}
}
//------------------------------------------------------------------------------
type combinedCounter struct {
c1 StatCounter
c2 StatCounter
}
func (c *combinedCounter) Incr(count int64) error {
if err := c.c1.Incr(count); err != nil {
return err
}
return c.c2.Incr(count)
}
type combinedTimer struct {
c1 StatTimer
c2 StatTimer
}
func (c *combinedTimer) Timing(delta int64) error {
if err := c.c1.Timing(delta); err != nil {
return err
}
return c.c2.Timing(delta)
}
type combinedGauge struct {
c1 StatGauge
c2 StatGauge
}
func (c *combinedGauge) Set(value int64) error {
if err := c.c1.Set(value); err != nil {
return err
}
return c.c2.Set(value)
}
func (c *combinedGauge) Incr(count int64) error {
if err := c.c1.Incr(count); err != nil {
return err
}
return c.c2.Incr(count)
}
func (c *combinedGauge) Decr(count int64) error {
if err := c.c1.Decr(count); err != nil {
return err
}
return c.c2.Decr(count)
}
//------------------------------------------------------------------------------
type combinedCounterVec struct {
c1 StatCounterVec
c2 StatCounterVec
}
func (c *combinedCounterVec) With(labelValues ...string) StatCounter {
return &combinedCounter{
c1: c.c1.With(labelValues...),
c2: c.c2.With(labelValues...),
}
}
type combinedTimerVec struct {
c1 StatTimerVec
c2 StatTimerVec
}
func (c *combinedTimerVec) With(labelValues ...string) StatTimer {
return &combinedTimer{
c1: c.c1.With(labelValues...),
c2: c.c2.With(labelValues...),
}
}
type combinedGaugeVec struct {
c1 StatGaugeVec
c2 StatGaugeVec
}
func (c *combinedGaugeVec) With(labelValues ...string) StatGauge {
return &combinedGauge{
c1: c.c1.With(labelValues...),
c2: c.c2.With(labelValues...),
}
}
//------------------------------------------------------------------------------
func (c *combinedWrapper) GetCounter(path string) StatCounter {
return &combinedCounter{
c1: c.t1.GetCounter(path),
c2: c.t2.GetCounter(path),
}
}
func (c *combinedWrapper) GetCounterVec(path string, n []string) StatCounterVec {
return &combinedCounterVec{
c1: c.t1.GetCounterVec(path, n),
c2: c.t2.GetCounterVec(path, n),
}
}
func (c *combinedWrapper) GetTimer(path string) StatTimer {
return &combinedTimer{
c1: c.t1.GetTimer(path),
c2: c.t2.GetTimer(path),
}
}
func (c *combinedWrapper) GetTimerVec(path string, n []string) StatTimerVec {
return &combinedTimerVec{
c1: c.t1.GetTimerVec(path, n),
c2: c.t2.GetTimerVec(path, n),
}
}
func (c *combinedWrapper) GetGauge(path string) StatGauge {
return &combinedGauge{
c1: c.t1.GetGauge(path),
c2: c.t2.GetGauge(path),
}
}
func (c *combinedWrapper) GetGaugeVec(path string, n []string) StatGaugeVec {
return &combinedGaugeVec{
c1: c.t1.GetGaugeVec(path, n),
c2: c.t2.GetGaugeVec(path, n),
}
}
func (c *combinedWrapper) SetLogger(log log.Modular) {
c.t1.SetLogger(log)
c.t2.SetLogger(log)
}
func (c *combinedWrapper) Close() error {
c.t1.Close()
c.t2.Close()
return nil
}
//------------------------------------------------------------------------------