-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathrelabel.go
More file actions
158 lines (140 loc) · 4.59 KB
/
relabel.go
File metadata and controls
158 lines (140 loc) · 4.59 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) The Thanos Community Authors.
// Licensed under the Apache License 2.0.
package function
import (
"context"
"regexp"
"strings"
"sync"
"github.com/thanos-io/promql-engine/execution/model"
"github.com/thanos-io/promql-engine/execution/telemetry"
"github.com/thanos-io/promql-engine/logicalplan"
"github.com/thanos-io/promql-engine/query"
"github.com/efficientgo/core/errors"
prommodel "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
)
type relabelOperator struct {
next model.VectorOperator
funcExpr *logicalplan.FunctionCall
once sync.Once
series []labels.Labels
}
func newRelabelOperator(
next model.VectorOperator,
funcExpr *logicalplan.FunctionCall,
opts *query.Options,
) model.VectorOperator {
oper := &relabelOperator{
next: next,
funcExpr: funcExpr,
}
return telemetry.NewOperator(telemetry.NewTelemetry(oper, opts.EnableAnalysis, opts.EnablePerStepStats, opts.Start.UnixMilli(), opts.End.UnixMilli(), opts.Step, opts.SampleLimiter), oper)
}
func (o *relabelOperator) String() string {
return "[relabel]"
}
func (o *relabelOperator) Explain() (next []model.VectorOperator) {
return []model.VectorOperator{o.next}
}
func (o *relabelOperator) Series(ctx context.Context) ([]labels.Labels, error) {
var err error
o.once.Do(func() { err = o.loadSeries(ctx) })
return o.series, err
}
func (o *relabelOperator) Next(ctx context.Context, buf []model.StepVector) (int, error) {
return o.next.Next(ctx, buf)
}
func (o *relabelOperator) loadSeries(ctx context.Context) (err error) {
series, err := o.next.Series(ctx)
if err != nil {
return err
}
o.series = make([]labels.Labels, len(series))
switch o.funcExpr.Func.Name {
case "label_join":
err = o.loadSeriesForLabelJoin(series)
case "label_replace":
err = o.loadSeriesForLabelReplace(series)
default:
err = errors.Newf("invalid function name for relabel operator: %s", o.funcExpr.Func.Name)
}
return err
}
func (o *relabelOperator) loadSeriesForLabelJoin(series []labels.Labels) error {
labelJoinDst, err := logicalplan.UnwrapString(o.funcExpr.Args[1])
if err != nil {
return errors.Wrap(err, "unable to unwrap string argument")
}
if !prommodel.UTF8Validation.IsValidLabelName(labelJoinDst) {
return errors.Newf("invalid destination label name in label_join: %s", labelJoinDst)
}
var labelJoinSrcLabels []string
labelJoinSep, err := logicalplan.UnwrapString(o.funcExpr.Args[2])
if err != nil {
return errors.Wrap(err, "unable to unwrap string argument")
}
for j := 3; j < len(o.funcExpr.Args); j++ {
srcLabel, err := logicalplan.UnwrapString(o.funcExpr.Args[j])
if err != nil {
return errors.Wrap(err, "unable to unwrap string argument")
}
labelJoinSrcLabels = append(labelJoinSrcLabels, srcLabel)
}
for i, s := range series {
lbls := s
srcVals := make([]string, len(labelJoinSrcLabels))
for j, src := range labelJoinSrcLabels {
srcVals[j] = lbls.Get(src)
}
lb := labels.NewBuilder(lbls)
if strval := strings.Join(srcVals, labelJoinSep); strval == "" {
lb.Del(labelJoinDst)
} else {
lb.Set(labelJoinDst, strval)
}
o.series[i] = lb.Labels()
}
return nil
}
func (o *relabelOperator) loadSeriesForLabelReplace(series []labels.Labels) error {
labelReplaceDst, err := logicalplan.UnwrapString(o.funcExpr.Args[1])
if err != nil {
return errors.Wrap(err, "unable to unwrap string argument")
}
if !prommodel.UTF8Validation.IsValidLabelName(labelReplaceDst) {
return errors.Newf("invalid destination label name in label_replace: %s", labelReplaceDst)
}
labelReplaceRepl, err := logicalplan.UnwrapString(o.funcExpr.Args[2])
if err != nil {
return errors.Wrap(err, "unable to unwrap string argument")
}
labelReplaceSrc, err := logicalplan.UnwrapString(o.funcExpr.Args[3])
if err != nil {
return errors.Wrap(err, "unable to unwrap string argument")
}
labelReplaceRegexVal, err := logicalplan.UnwrapString(o.funcExpr.Args[4])
if err != nil {
return errors.Wrap(err, "unable to unwrap string argument")
}
labelReplaceRegex, err := regexp.Compile("^(?:" + labelReplaceRegexVal + ")$")
if err != nil {
return errors.Newf("invalid regular expression in label_replace(): %s", labelReplaceRegexVal)
}
for i, s := range series {
lbls := s
srcVal := lbls.Get(labelReplaceSrc)
matches := labelReplaceRegex.FindStringSubmatchIndex(srcVal)
if len(matches) == 0 {
o.series[i] = lbls
continue
}
res := labelReplaceRegex.ExpandString([]byte{}, labelReplaceRepl, srcVal, matches)
lb := labels.NewBuilder(lbls).Del(labelReplaceDst)
if len(res) > 0 {
lb.Set(labelReplaceDst, string(res))
}
o.series[i] = lb.Labels()
}
return nil
}