-
Notifications
You must be signed in to change notification settings - Fork 789
/
compat.go
64 lines (53 loc) · 1.76 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
package rules
import (
time "time"
"github.com/cortexproject/cortex/pkg/ingester/client"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/labels"
legacy_rulefmt "github.com/cortexproject/cortex/pkg/ruler/legacy_rulefmt"
)
// ToProto transforms a formatted prometheus rulegroup to a rule group protobuf
func ToProto(user string, namespace string, rl legacy_rulefmt.RuleGroup) *RuleGroupDesc {
rg := RuleGroupDesc{
Name: rl.Name,
Namespace: namespace,
Interval: time.Duration(rl.Interval),
Rules: formattedRuleToProto(rl.Rules),
User: user,
}
return &rg
}
func formattedRuleToProto(rls []legacy_rulefmt.Rule) []*RuleDesc {
rules := make([]*RuleDesc, len(rls))
for i := range rls {
rules[i] = &RuleDesc{
Expr: rls[i].Expr,
Record: rls[i].Record,
Alert: rls[i].Alert,
For: time.Duration(rls[i].For),
Labels: client.FromLabelsToLabelAdapters(labels.FromMap(rls[i].Labels)),
Annotations: client.FromLabelsToLabelAdapters(labels.FromMap(rls[i].Annotations)),
}
}
return rules
}
// FromProto generates a rulefmt RuleGroup
func FromProto(rg *RuleGroupDesc) legacy_rulefmt.RuleGroup {
formattedRuleGroup := legacy_rulefmt.RuleGroup{
Name: rg.GetName(),
Interval: model.Duration(rg.Interval),
Rules: make([]legacy_rulefmt.Rule, len(rg.GetRules())),
}
for i, rl := range rg.GetRules() {
newRule := legacy_rulefmt.Rule{
Record: rl.GetRecord(),
Alert: rl.GetAlert(),
Expr: rl.GetExpr(),
Labels: client.FromLabelAdaptersToLabels(rl.Labels).Map(),
Annotations: client.FromLabelAdaptersToLabels(rl.Annotations).Map(),
For: model.Duration(rl.GetFor()),
}
formattedRuleGroup.Rules[i] = newRule
}
return formattedRuleGroup
}