mirror of https://github.com/prometheus/prometheus
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.8 KiB
103 lines
2.8 KiB
9 years ago
|
// Copyright 2015 The Prometheus Authors
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
8 years ago
|
package relabel
|
||
10 years ago
|
|
||
|
import (
|
||
9 years ago
|
"crypto/md5"
|
||
10 years ago
|
"fmt"
|
||
10 years ago
|
"strings"
|
||
|
|
||
9 years ago
|
"github.com/prometheus/common/model"
|
||
10 years ago
|
|
||
|
"github.com/prometheus/prometheus/config"
|
||
|
)
|
||
|
|
||
8 years ago
|
// Process returns a relabeled copy of the given label set. The relabel configurations
|
||
10 years ago
|
// are applied in order of input.
|
||
|
// If a label set is dropped, nil is returned.
|
||
8 years ago
|
func Process(labels model.LabelSet, cfgs ...*config.RelabelConfig) model.LabelSet {
|
||
9 years ago
|
out := model.LabelSet{}
|
||
10 years ago
|
for ln, lv := range labels {
|
||
|
out[ln] = lv
|
||
|
}
|
||
|
for _, cfg := range cfgs {
|
||
8 years ago
|
if out = relabel(out, cfg); out == nil {
|
||
|
return nil
|
||
10 years ago
|
}
|
||
|
}
|
||
8 years ago
|
return out
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func relabel(labels model.LabelSet, cfg *config.RelabelConfig) model.LabelSet {
|
||
10 years ago
|
values := make([]string, 0, len(cfg.SourceLabels))
|
||
|
for _, ln := range cfg.SourceLabels {
|
||
|
values = append(values, string(labels[ln]))
|
||
10 years ago
|
}
|
||
10 years ago
|
val := strings.Join(values, cfg.Separator)
|
||
10 years ago
|
|
||
10 years ago
|
switch cfg.Action {
|
||
|
case config.RelabelDrop:
|
||
|
if cfg.Regex.MatchString(val) {
|
||
8 years ago
|
return nil
|
||
10 years ago
|
}
|
||
10 years ago
|
case config.RelabelKeep:
|
||
|
if !cfg.Regex.MatchString(val) {
|
||
8 years ago
|
return nil
|
||
10 years ago
|
}
|
||
10 years ago
|
case config.RelabelReplace:
|
||
9 years ago
|
indexes := cfg.Regex.FindStringSubmatchIndex(val)
|
||
10 years ago
|
// If there is no match no replacement must take place.
|
||
9 years ago
|
if indexes == nil {
|
||
10 years ago
|
break
|
||
|
}
|
||
9 years ago
|
res := cfg.Regex.ExpandString([]byte{}, cfg.Replacement, val, indexes)
|
||
|
if len(res) == 0 {
|
||
10 years ago
|
delete(labels, cfg.TargetLabel)
|
||
10 years ago
|
} else {
|
||
9 years ago
|
labels[cfg.TargetLabel] = model.LabelValue(res)
|
||
10 years ago
|
}
|
||
10 years ago
|
case config.RelabelHashMod:
|
||
9 years ago
|
mod := sum64(md5.Sum([]byte(val))) % cfg.Modulus
|
||
9 years ago
|
labels[cfg.TargetLabel] = model.LabelValue(fmt.Sprintf("%d", mod))
|
||
9 years ago
|
case config.RelabelLabelMap:
|
||
9 years ago
|
out := make(model.LabelSet, len(labels))
|
||
9 years ago
|
// Take a copy to avoid infinite loops.
|
||
|
for ln, lv := range labels {
|
||
|
out[ln] = lv
|
||
|
}
|
||
|
for ln, lv := range labels {
|
||
|
if cfg.Regex.MatchString(string(ln)) {
|
||
|
res := cfg.Regex.ReplaceAllString(string(ln), cfg.Replacement)
|
||
9 years ago
|
out[model.LabelName(res)] = lv
|
||
9 years ago
|
}
|
||
|
}
|
||
|
labels = out
|
||
10 years ago
|
default:
|
||
10 years ago
|
panic(fmt.Errorf("retrieval.relabel: unknown relabel action type %q", cfg.Action))
|
||
10 years ago
|
}
|
||
8 years ago
|
return labels
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
// sum64 sums the md5 hash to an uint64.
|
||
|
func sum64(hash [md5.Size]byte) uint64 {
|
||
|
var s uint64
|
||
|
|
||
|
for i, b := range hash {
|
||
|
shift := uint64((md5.Size - i - 1) * 8)
|
||
|
|
||
|
s |= uint64(b) << shift
|
||
|
}
|
||
|
return s
|
||
|
}
|