mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
move relabeling functionality to its own package
also remove the returned error as it was always nil
This commit is contained in:
parent
679d225c8d
commit
7714b9c781
|
@ -30,7 +30,7 @@ import (
|
||||||
"golang.org/x/net/context/ctxhttp"
|
"golang.org/x/net/context/ctxhttp"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/config"
|
"github.com/prometheus/prometheus/config"
|
||||||
"github.com/prometheus/prometheus/retrieval"
|
"github.com/prometheus/prometheus/relabel"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -239,7 +239,7 @@ func (n *Notifier) Send(alerts ...*model.Alert) {
|
||||||
func (n *Notifier) relabelAlerts(alerts []*model.Alert) []*model.Alert {
|
func (n *Notifier) relabelAlerts(alerts []*model.Alert) []*model.Alert {
|
||||||
var relabeledAlerts []*model.Alert
|
var relabeledAlerts []*model.Alert
|
||||||
for _, alert := range alerts {
|
for _, alert := range alerts {
|
||||||
labels, _ := retrieval.Relabel(alert.Labels, n.opts.RelabelConfigs...)
|
labels := relabel.Process(alert.Labels, n.opts.RelabelConfigs...)
|
||||||
if labels != nil {
|
if labels != nil {
|
||||||
alert.Labels = labels
|
alert.Labels = labels
|
||||||
relabeledAlerts = append(relabeledAlerts, alert)
|
relabeledAlerts = append(relabeledAlerts, alert)
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package retrieval
|
package relabel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
|
@ -23,27 +23,23 @@ import (
|
||||||
"github.com/prometheus/prometheus/config"
|
"github.com/prometheus/prometheus/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Relabel returns a relabeled copy of the given label set. The relabel configurations
|
// Process returns a relabeled copy of the given label set. The relabel configurations
|
||||||
// are applied in order of input.
|
// are applied in order of input.
|
||||||
// If a label set is dropped, nil is returned.
|
// If a label set is dropped, nil is returned.
|
||||||
func Relabel(labels model.LabelSet, cfgs ...*config.RelabelConfig) (model.LabelSet, error) {
|
func Process(labels model.LabelSet, cfgs ...*config.RelabelConfig) model.LabelSet {
|
||||||
out := model.LabelSet{}
|
out := model.LabelSet{}
|
||||||
for ln, lv := range labels {
|
for ln, lv := range labels {
|
||||||
out[ln] = lv
|
out[ln] = lv
|
||||||
}
|
}
|
||||||
var err error
|
|
||||||
for _, cfg := range cfgs {
|
for _, cfg := range cfgs {
|
||||||
if out, err = relabel(out, cfg); err != nil {
|
if out = relabel(out, cfg); out == nil {
|
||||||
return nil, err
|
return nil
|
||||||
}
|
|
||||||
if out == nil {
|
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out, nil
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func relabel(labels model.LabelSet, cfg *config.RelabelConfig) (model.LabelSet, error) {
|
func relabel(labels model.LabelSet, cfg *config.RelabelConfig) model.LabelSet {
|
||||||
values := make([]string, 0, len(cfg.SourceLabels))
|
values := make([]string, 0, len(cfg.SourceLabels))
|
||||||
for _, ln := range cfg.SourceLabels {
|
for _, ln := range cfg.SourceLabels {
|
||||||
values = append(values, string(labels[ln]))
|
values = append(values, string(labels[ln]))
|
||||||
|
@ -53,11 +49,11 @@ func relabel(labels model.LabelSet, cfg *config.RelabelConfig) (model.LabelSet,
|
||||||
switch cfg.Action {
|
switch cfg.Action {
|
||||||
case config.RelabelDrop:
|
case config.RelabelDrop:
|
||||||
if cfg.Regex.MatchString(val) {
|
if cfg.Regex.MatchString(val) {
|
||||||
return nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
case config.RelabelKeep:
|
case config.RelabelKeep:
|
||||||
if !cfg.Regex.MatchString(val) {
|
if !cfg.Regex.MatchString(val) {
|
||||||
return nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
case config.RelabelReplace:
|
case config.RelabelReplace:
|
||||||
indexes := cfg.Regex.FindStringSubmatchIndex(val)
|
indexes := cfg.Regex.FindStringSubmatchIndex(val)
|
||||||
|
@ -90,7 +86,7 @@ func relabel(labels model.LabelSet, cfg *config.RelabelConfig) (model.LabelSet,
|
||||||
default:
|
default:
|
||||||
panic(fmt.Errorf("retrieval.relabel: unknown relabel action type %q", cfg.Action))
|
panic(fmt.Errorf("retrieval.relabel: unknown relabel action type %q", cfg.Action))
|
||||||
}
|
}
|
||||||
return labels, nil
|
return labels
|
||||||
}
|
}
|
||||||
|
|
||||||
// sum64 sums the md5 hash to an uint64.
|
// sum64 sums the md5 hash to an uint64.
|
|
@ -11,7 +11,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package retrieval
|
package relabel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -280,10 +280,7 @@ func TestRelabel(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
res, err := Relabel(test.input, test.relabel...)
|
res := Process(test.input, test.relabel...)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Test %d: error relabeling: %s", i+1, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !reflect.DeepEqual(res, test.output) {
|
if !reflect.DeepEqual(res, test.output) {
|
||||||
t.Errorf("Test %d: relabel output mismatch: expected %#v, got %#v", i+1, test.output, res)
|
t.Errorf("Test %d: relabel output mismatch: expected %#v, got %#v", i+1, test.output, res)
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/config"
|
"github.com/prometheus/prometheus/config"
|
||||||
|
"github.com/prometheus/prometheus/relabel"
|
||||||
"github.com/prometheus/prometheus/storage"
|
"github.com/prometheus/prometheus/storage"
|
||||||
"github.com/prometheus/prometheus/util/httputil"
|
"github.com/prometheus/prometheus/util/httputil"
|
||||||
)
|
)
|
||||||
|
@ -275,10 +276,8 @@ type relabelAppender struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app relabelAppender) Append(s *model.Sample) error {
|
func (app relabelAppender) Append(s *model.Sample) error {
|
||||||
labels, err := Relabel(model.LabelSet(s.Metric), app.relabelings...)
|
labels := relabel.Process(model.LabelSet(s.Metric), app.relabelings...)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("metric relabeling error %s: %s", s.Metric, err)
|
|
||||||
}
|
|
||||||
// Check if the timeseries was dropped.
|
// Check if the timeseries was dropped.
|
||||||
if labels == nil {
|
if labels == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -25,6 +25,7 @@ import (
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/config"
|
"github.com/prometheus/prometheus/config"
|
||||||
|
"github.com/prometheus/prometheus/relabel"
|
||||||
"github.com/prometheus/prometheus/retrieval/discovery"
|
"github.com/prometheus/prometheus/retrieval/discovery"
|
||||||
"github.com/prometheus/prometheus/storage"
|
"github.com/prometheus/prometheus/storage"
|
||||||
)
|
)
|
||||||
|
@ -448,10 +449,8 @@ func targetsFromGroup(tg *config.TargetGroup, cfg *config.ScrapeConfig) ([]*Targ
|
||||||
|
|
||||||
preRelabelLabels := labels
|
preRelabelLabels := labels
|
||||||
|
|
||||||
labels, err := Relabel(labels, cfg.RelabelConfigs...)
|
labels := relabel.Process(labels, cfg.RelabelConfigs...)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error while relabeling instance %d in target group %s: %s", i, tg, err)
|
|
||||||
}
|
|
||||||
// Check if the target was dropped.
|
// Check if the target was dropped.
|
||||||
if labels == nil {
|
if labels == nil {
|
||||||
continue
|
continue
|
||||||
|
@ -469,7 +468,7 @@ func targetsFromGroup(tg *config.TargetGroup, cfg *config.ScrapeConfig) ([]*Targ
|
||||||
}
|
}
|
||||||
labels[model.AddressLabel] = model.LabelValue(addr)
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
||||||
}
|
}
|
||||||
if err = config.CheckTargetAddress(labels[model.AddressLabel]); err != nil {
|
if err := config.CheckTargetAddress(labels[model.AddressLabel]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue