-
Notifications
You must be signed in to change notification settings - Fork 781
/
compat.go
62 lines (52 loc) · 1.55 KB
/
compat.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
package ruler
import (
"context"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/storage"
"github.com/cortexproject/cortex/pkg/ingester/client"
)
// Pusher is an ingester server that accepts pushes.
type Pusher interface {
Push(context.Context, *client.WriteRequest) (*client.WriteResponse, error)
}
// appendableAppender adapts a distributor.Distributor to both a ruler.Appendable
// and a storage.Appender.
//
// Distributors need a context and storage.Appender doesn't allow for
// one. See
// https://github.com/prometheus/prometheus/pull/2000#discussion_r79108319 for
// reasons why.
type appendableAppender struct {
pusher Pusher
ctx context.Context
samples []model.Sample
}
func (a *appendableAppender) Appender() (storage.Appender, error) {
return a, nil
}
func (a *appendableAppender) Add(l labels.Labels, t int64, v float64) (uint64, error) {
m := make(model.Metric, len(l))
for _, lbl := range l {
m[model.LabelName(lbl.Name)] = model.LabelValue(lbl.Value)
}
a.samples = append(a.samples, model.Sample{
Metric: m,
Timestamp: model.Time(t),
Value: model.SampleValue(v),
})
return 0, nil
}
func (a *appendableAppender) AddFast(l labels.Labels, ref uint64, t int64, v float64) error {
_, err := a.Add(l, t, v)
return err
}
func (a *appendableAppender) Commit() error {
_, err := a.pusher.Push(a.ctx, client.ToWriteRequest(a.samples, client.RULE))
a.samples = nil
return err
}
func (a *appendableAppender) Rollback() error {
a.samples = nil
return nil
}