-
Notifications
You must be signed in to change notification settings - Fork 1
/
store_accumulator.go
112 lines (93 loc) 路 3.86 KB
/
store_accumulator.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
// Copyright 2015-2023 Bleemeo
//
// bleemeo.com an infrastructure monitoring solution in the Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"time"
"github.com/influxdata/telegraf"
)
type Measurement struct {
Name string
Fields map[string]interface{}
Tags map[string]string
T []time.Time
}
// StoreAccumulator store in memory all value pushed by AddFields, AddGauge...
// All type (fields, gauge, counter) are processed the same, and can't be distinguished.
type StoreAccumulator struct {
Measurement []Measurement
Errors []error
}
// Send forward all captured measurement using acc.AddFields. It also send errors using AddError.
func (a *StoreAccumulator) Send(acc telegraf.Accumulator) {
for _, m := range a.Measurement {
acc.AddFields(m.Name, m.Fields, m.Tags, m.T...)
}
for _, err := range a.Errors {
acc.AddError(err)
}
}
func (a *StoreAccumulator) processMetrics(measurement string, fields map[string]interface{}, tags map[string]string, t ...time.Time) {
a.Measurement = append(a.Measurement, Measurement{
Name: measurement,
Fields: fields,
Tags: tags,
T: t,
})
}
// AddFields adds a metric to the accumulator with the given measurement
// name, fields, and tags (and timestamp). If a timestamp is not provided,
// then the accumulator sets it to "now".
func (a *StoreAccumulator) AddFields(measurement string, fields map[string]interface{}, tags map[string]string, t ...time.Time) {
a.processMetrics(measurement, fields, tags, t...)
}
// AddGauge is the same as AddFields, but will add the metric as a "Gauge" type.
func (a *StoreAccumulator) AddGauge(measurement string, fields map[string]interface{}, tags map[string]string, t ...time.Time) {
a.processMetrics(measurement, fields, tags, t...)
}
// AddCounter is the same as AddFields, but will add the metric as a "Counter" type.
func (a *StoreAccumulator) AddCounter(measurement string, fields map[string]interface{}, tags map[string]string, t ...time.Time) {
a.processMetrics(measurement, fields, tags, t...)
}
// AddSummary is the same as AddFields, but will add the metric as a "Summary" type.
func (a *StoreAccumulator) AddSummary(measurement string, fields map[string]interface{}, tags map[string]string, t ...time.Time) {
a.processMetrics(measurement, fields, tags, t...)
}
// AddHistogram is the same as AddFields, but will add the metric as a "Histogram" type.
func (a *StoreAccumulator) AddHistogram(measurement string, fields map[string]interface{}, tags map[string]string, t ...time.Time) {
a.processMetrics(measurement, fields, tags, t...)
}
// AddMetric adds an metric to the accumulator.
func (a *StoreAccumulator) AddMetric(telegraf.Metric) {
a.AddError(errNotImplemented)
}
// AddError reports an error.
func (a *StoreAccumulator) AddError(err error) {
a.Errors = append(a.Errors, err)
}
// SetPrecision takes two time.Duration objects. If the first is non-zero,
// it sets that as the precision. Otherwise, it takes the second argument
// as the order of time that the metrics should be rounded to, with the
// maximum being 1s.
func (a *StoreAccumulator) SetPrecision(time.Duration) {
a.AddError(errNotImplemented)
}
// WithTracking upgrades to a TrackingAccumulator with space for maxTracked
// metrics/batches.
func (a *StoreAccumulator) WithTracking(maxTracked int) telegraf.TrackingAccumulator {
_ = maxTracked
a.AddError(errNotImplemented)
return nil
}