Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/loki/source/podlogs/relabel.go
5341 views
1
package podlogs
2
3
import (
4
"strings"
5
6
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
7
"github.com/prometheus/common/model"
8
"github.com/prometheus/prometheus/model/relabel"
9
)
10
11
func convertRelabelConfig(in []*promv1.RelabelConfig) ([]*relabel.Config, error) {
12
res := make([]*relabel.Config, 0, len(in))
13
14
for _, inRule := range in {
15
outRule := relabel.DefaultRelabelConfig
16
if len(inRule.SourceLabels) > 0 {
17
outRule.SourceLabels = convertLabelNames(inRule.SourceLabels)
18
}
19
if inRule.Separator != "" {
20
outRule.Separator = inRule.Separator
21
}
22
if inRule.Regex != "" {
23
regex, err := relabel.NewRegexp(inRule.Regex)
24
if err != nil {
25
return nil, err
26
}
27
outRule.Regex = regex
28
}
29
if inRule.Modulus != 0 {
30
outRule.Modulus = inRule.Modulus
31
}
32
if inRule.TargetLabel != "" {
33
outRule.TargetLabel = inRule.TargetLabel
34
}
35
if inRule.Replacement != "" {
36
outRule.Replacement = inRule.Replacement
37
}
38
if inRule.Action != "" {
39
outRule.Action = relabel.Action(strings.ToLower(inRule.Action))
40
}
41
42
res = append(res, &outRule)
43
}
44
45
return res, nil
46
}
47
48
func convertLabelNames(in []promv1.LabelName) model.LabelNames {
49
res := make([]model.LabelName, 0, len(in))
50
for _, inName := range in {
51
res = append(res, model.LabelName(inName))
52
}
53
return res
54
}
55
56