Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/mimir/client/rules.go
4096 views
1
package client
2
3
import (
4
"context"
5
"io"
6
"net/url"
7
8
"github.com/prometheus/prometheus/model/rulefmt"
9
"gopkg.in/yaml.v3"
10
)
11
12
// RemoteWriteConfig is used to specify a remote write endpoint
13
type RemoteWriteConfig struct {
14
URL string `json:"url,omitempty"`
15
}
16
17
// CreateRuleGroup creates a new rule group
18
func (r *MimirClient) CreateRuleGroup(ctx context.Context, namespace string, rg rulefmt.RuleGroup) error {
19
payload, err := yaml.Marshal(&rg)
20
if err != nil {
21
return err
22
}
23
24
escapedNamespace := url.PathEscape(namespace)
25
path := r.apiPath + "/" + escapedNamespace
26
op := r.apiPath + "/" + "<namespace>"
27
28
res, err := r.doRequest(op, path, "POST", payload)
29
if err != nil {
30
return err
31
}
32
33
res.Body.Close()
34
35
return nil
36
}
37
38
// DeleteRuleGroup deletes a rule group
39
func (r *MimirClient) DeleteRuleGroup(ctx context.Context, namespace, groupName string) error {
40
escapedNamespace := url.PathEscape(namespace)
41
escapedGroupName := url.PathEscape(groupName)
42
path := r.apiPath + "/" + escapedNamespace + "/" + escapedGroupName
43
op := r.apiPath + "/" + "<namespace>" + "/" + "<group_name>"
44
45
res, err := r.doRequest(op, path, "DELETE", nil)
46
if err != nil {
47
return err
48
}
49
50
res.Body.Close()
51
52
return nil
53
}
54
55
// ListRules retrieves a rule group
56
func (r *MimirClient) ListRules(ctx context.Context, namespace string) (map[string][]rulefmt.RuleGroup, error) {
57
path := r.apiPath
58
op := r.apiPath
59
if namespace != "" {
60
path = path + "/" + namespace
61
op = op + "/" + "<namespace>"
62
}
63
64
res, err := r.doRequest(op, path, "GET", nil)
65
if err != nil {
66
return nil, err
67
}
68
69
defer res.Body.Close()
70
body, err := io.ReadAll(res.Body)
71
72
if err != nil {
73
return nil, err
74
}
75
76
ruleSet := map[string][]rulefmt.RuleGroup{}
77
err = yaml.Unmarshal(body, &ruleSet)
78
if err != nil {
79
return nil, err
80
}
81
82
return ruleSet, nil
83
}
84
85