-
Notifications
You must be signed in to change notification settings - Fork 479
/
fanout.go
228 lines (199 loc) · 6.25 KB
/
fanout.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package prometheus
import (
"context"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/hashicorp/go-multierror"
"github.com/prometheus/prometheus/model/exemplar"
"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/metadata"
"github.com/prometheus/prometheus/scrape"
"github.com/prometheus/prometheus/storage"
)
var _ storage.Appendable = (*Fanout)(nil)
// Fanout supports the default Flow style of appendables since it can go to multiple outputs. It also allows the intercepting of appends.
type Fanout struct {
mut sync.RWMutex
// children is where to fan out.
children []storage.Appendable
// ComponentID is what component this belongs to.
componentID string
writeLatency prometheus.Histogram
samplesCounter prometheus.Counter
}
// NewFanout creates a fanout appendable.
func NewFanout(children []storage.Appendable, componentID string, register prometheus.Registerer) *Fanout {
wl := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "agent_prometheus_fanout_latency",
Help: "Write latency for sending to direct and indirect components",
})
_ = register.Register(wl)
s := prometheus.NewCounter(prometheus.CounterOpts{
Name: "agent_prometheus_forwarded_samples_total",
Help: "Total number of samples sent to downstream components.",
})
_ = register.Register(s)
return &Fanout{
children: children,
componentID: componentID,
writeLatency: wl,
samplesCounter: s,
}
}
// UpdateChildren allows changing of the children of the fanout.
func (f *Fanout) UpdateChildren(children []storage.Appendable) {
f.mut.Lock()
defer f.mut.Unlock()
f.children = children
}
// Appender satisfies the Appendable interface.
func (f *Fanout) Appender(ctx context.Context) storage.Appender {
f.mut.RLock()
defer f.mut.RUnlock()
// TODO(@tpaschalis): The `otelcol.receiver.prometheus` component reuses
// code from the prometheusreceiver which expects the Appender context to
// be contain both a scrape target and a metadata store, and fails the
// conversion if they are missing. We should find a way around this as both
// Targets and Metadata will be handled in a different way in Flow.
ctx = scrape.ContextWithTarget(ctx, &scrape.Target{})
ctx = scrape.ContextWithMetricMetadataStore(ctx, NoopMetadataStore{})
app := &appender{
children: make([]storage.Appender, 0),
componentID: f.componentID,
writeLatency: f.writeLatency,
samplesCounter: f.samplesCounter,
}
for _, x := range f.children {
if x == nil {
continue
}
app.children = append(app.children, x.Appender(ctx))
}
return app
}
type appender struct {
children []storage.Appender
componentID string
writeLatency prometheus.Histogram
samplesCounter prometheus.Counter
start time.Time
}
var _ storage.Appender = (*appender)(nil)
// Append satisfies the Appender interface.
func (a *appender) Append(ref storage.SeriesRef, l labels.Labels, t int64, v float64) (storage.SeriesRef, error) {
if a.start.IsZero() {
a.start = time.Now()
}
if ref == 0 {
ref = storage.SeriesRef(GlobalRefMapping.GetOrAddGlobalRefID(l))
}
var multiErr error
updated := false
for _, x := range a.children {
_, err := x.Append(ref, l, t, v)
if err != nil {
multiErr = multierror.Append(multiErr, err)
} else {
updated = true
}
}
if updated {
a.samplesCounter.Inc()
}
return ref, multiErr
}
// Commit satisfies the Appender interface.
func (a *appender) Commit() error {
defer a.recordLatency()
var multiErr error
for _, x := range a.children {
err := x.Commit()
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
return multiErr
}
// Rollback satisfies the Appender interface.
func (a *appender) Rollback() error {
defer a.recordLatency()
var multiErr error
for _, x := range a.children {
err := x.Rollback()
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
return multiErr
}
func (a *appender) recordLatency() {
if a.start.IsZero() {
return
}
duration := time.Since(a.start)
a.writeLatency.Observe(duration.Seconds())
}
// AppendExemplar satisfies the Appender interface.
func (a *appender) AppendExemplar(ref storage.SeriesRef, l labels.Labels, e exemplar.Exemplar) (storage.SeriesRef, error) {
if a.start.IsZero() {
a.start = time.Now()
}
if ref == 0 {
ref = storage.SeriesRef(GlobalRefMapping.GetOrAddGlobalRefID(l))
}
var multiErr error
for _, x := range a.children {
_, err := x.AppendExemplar(ref, l, e)
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
return ref, multiErr
}
// UpdateMetadata satisfies the Appender interface.
func (a *appender) UpdateMetadata(ref storage.SeriesRef, l labels.Labels, m metadata.Metadata) (storage.SeriesRef, error) {
if a.start.IsZero() {
a.start = time.Now()
}
if ref == 0 {
ref = storage.SeriesRef(GlobalRefMapping.GetOrAddGlobalRefID(l))
}
var multiErr error
for _, x := range a.children {
_, err := x.UpdateMetadata(ref, l, m)
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
return ref, multiErr
}
func (a *appender) AppendHistogram(ref storage.SeriesRef, l labels.Labels, t int64, h *histogram.Histogram, fh *histogram.FloatHistogram) (storage.SeriesRef, error) {
if a.start.IsZero() {
a.start = time.Now()
}
if ref == 0 {
ref = storage.SeriesRef(GlobalRefMapping.GetOrAddGlobalRefID(l))
}
var multiErr error
for _, x := range a.children {
_, err := x.AppendHistogram(ref, l, t, h, fh)
if err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
return ref, multiErr
}
// NoopMetadataStore implements the MetricMetadataStore interface.
type NoopMetadataStore map[string]scrape.MetricMetadata
// GetMetadata implements the MetricMetadataStore interface.
func (ms NoopMetadataStore) GetMetadata(familyName string) (scrape.MetricMetadata, bool) {
return scrape.MetricMetadata{}, false
}
// ListMetadata implements the MetricMetadataStore interface.
func (ms NoopMetadataStore) ListMetadata() []scrape.MetricMetadata { return nil }
// SizeMetadata implements the MetricMetadataStore interface.
func (ms NoopMetadataStore) SizeMetadata() int { return 0 }
// LengthMetadata implements the MetricMetadataStore interface.
func (ms NoopMetadataStore) LengthMetadata() int { return 0 }