-
Notifications
You must be signed in to change notification settings - Fork 786
/
compat.go
117 lines (103 loc) · 3.14 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
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
package ruler
import (
"context"
"time"
"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/notifier"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/rules"
"github.com/prometheus/prometheus/storage"
"github.com/weaveworks/common/user"
"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)
}
type pusherAppender struct {
pusher Pusher
labels []labels.Labels
samples []client.Sample
userID string
}
func (a *pusherAppender) Add(l labels.Labels, t int64, v float64) (uint64, error) {
a.labels = append(a.labels, l)
a.samples = append(a.samples, client.Sample{
TimestampMs: t,
Value: v,
})
return 0, nil
}
func (a *pusherAppender) AddFast(_ uint64, _ int64, _ float64) error {
return storage.ErrNotFound
}
func (a *pusherAppender) Commit() error {
// Since a.pusher is distributor, client.ReuseSlice will be called in a.pusher.Push.
// We shouldn't call client.ReuseSlice here.
_, err := a.pusher.Push(user.InjectOrgID(context.Background(), a.userID), client.ToWriteRequest(a.labels, a.samples, nil, client.RULE))
a.labels = nil
a.samples = nil
return err
}
func (a *pusherAppender) Rollback() error {
a.labels = nil
a.samples = nil
return nil
}
// PusherAppendable fulfills the storage.Appendable interface for prometheus manager
type PusherAppendable struct {
pusher Pusher
userID string
}
// Appender returns a storage.Appender
func (t *PusherAppendable) Appender() storage.Appender {
return &pusherAppender{
pusher: t.pusher,
userID: t.userID,
}
}
// engineQueryFunc returns a new query function using the rules.EngineQueryFunc function
// and passing an altered timestamp.
func engineQueryFunc(engine *promql.Engine, q storage.Queryable, delay time.Duration) rules.QueryFunc {
orig := rules.EngineQueryFunc(engine, q)
return func(ctx context.Context, qs string, t time.Time) (promql.Vector, error) {
return orig(ctx, qs, t.Add(-delay))
}
}
type ManagerFactory = func(
ctx context.Context,
userID string,
notifier *notifier.Manager,
logger log.Logger,
reg prometheus.Registerer,
) *rules.Manager
func DefaultTenantManagerFactory(
cfg Config,
p Pusher,
q storage.Queryable,
engine *promql.Engine,
) ManagerFactory {
return func(
ctx context.Context,
userID string,
notifier *notifier.Manager,
logger log.Logger,
reg prometheus.Registerer,
) *rules.Manager {
return rules.NewManager(&rules.ManagerOptions{
Appendable: &PusherAppendable{pusher: p, userID: userID},
Queryable: q,
QueryFunc: engineQueryFunc(engine, q, cfg.EvaluationDelay),
Context: user.InjectOrgID(ctx, userID),
ExternalURL: cfg.ExternalURL.URL,
NotifyFunc: SendAlerts(notifier, cfg.ExternalURL.URL.String()),
Logger: log.With(logger, "user", userID),
Registerer: reg,
OutageTolerance: cfg.OutageTolerance,
ForGracePeriod: cfg.ForGracePeriod,
ResendDelay: cfg.ResendDelay,
})
}
}