Skip to content

Commit

Permalink
move relabeling functionality to its own package
Browse files Browse the repository at this point in the history
also remove the returned error as it was always nil
  • Loading branch information
brancz committed Aug 9, 2016
1 parent 679d225 commit 7714b9c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 30 deletions.
4 changes: 2 additions & 2 deletions notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"golang.org/x/net/context/ctxhttp"

"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/retrieval"
"github.com/prometheus/prometheus/relabel"
)

const (
Expand Down Expand Up @@ -239,7 +239,7 @@ func (n *Notifier) Send(alerts ...*model.Alert) {
func (n *Notifier) relabelAlerts(alerts []*model.Alert) []*model.Alert {
var relabeledAlerts []*model.Alert
for _, alert := range alerts {
labels, _ := retrieval.Relabel(alert.Labels, n.opts.RelabelConfigs...)
labels := relabel.Process(alert.Labels, n.opts.RelabelConfigs...)
if labels != nil {
alert.Labels = labels
relabeledAlerts = append(relabeledAlerts, alert)
Expand Down
24 changes: 10 additions & 14 deletions retrieval/relabel.go → relabel/relabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package retrieval
package relabel

import (
"crypto/md5"
Expand All @@ -23,27 +23,23 @@ import (
"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.
// 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{}
for ln, lv := range labels {
out[ln] = lv
}
var err error
for _, cfg := range cfgs {
if out, err = relabel(out, cfg); err != nil {
return nil, err
}
if out == nil {
return nil, nil
if out = relabel(out, cfg); out == nil {
return 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))
for _, ln := range cfg.SourceLabels {
values = append(values, string(labels[ln]))
Expand All @@ -53,11 +49,11 @@ func relabel(labels model.LabelSet, cfg *config.RelabelConfig) (model.LabelSet,
switch cfg.Action {
case config.RelabelDrop:
if cfg.Regex.MatchString(val) {
return nil, nil
return nil
}
case config.RelabelKeep:
if !cfg.Regex.MatchString(val) {
return nil, nil
return nil
}
case config.RelabelReplace:
indexes := cfg.Regex.FindStringSubmatchIndex(val)
Expand Down Expand Up @@ -90,7 +86,7 @@ func relabel(labels model.LabelSet, cfg *config.RelabelConfig) (model.LabelSet,
default:
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.
Expand Down
7 changes: 2 additions & 5 deletions retrieval/relabel_test.go → relabel/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package retrieval
package relabel

import (
"reflect"
Expand Down Expand Up @@ -280,10 +280,7 @@ func TestRelabel(t *testing.T) {
}

for i, test := range tests {
res, err := Relabel(test.input, test.relabel...)
if err != nil {
t.Errorf("Test %d: error relabeling: %s", i+1, err)
}
res := Process(test.input, test.relabel...)

if !reflect.DeepEqual(res, test.output) {
t.Errorf("Test %d: relabel output mismatch: expected %#v, got %#v", i+1, test.output, res)
Expand Down
7 changes: 3 additions & 4 deletions retrieval/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/prometheus/common/model"

"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/relabel"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/util/httputil"
)
Expand Down Expand Up @@ -275,10 +276,8 @@ type relabelAppender struct {
}

func (app relabelAppender) Append(s *model.Sample) error {
labels, err := Relabel(model.LabelSet(s.Metric), app.relabelings...)
if err != nil {
return fmt.Errorf("metric relabeling error %s: %s", s.Metric, err)
}
labels := relabel.Process(model.LabelSet(s.Metric), app.relabelings...)

// Check if the timeseries was dropped.
if labels == nil {
return nil
Expand Down
9 changes: 4 additions & 5 deletions retrieval/targetmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"golang.org/x/net/context"

"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/relabel"
"github.com/prometheus/prometheus/retrieval/discovery"
"github.com/prometheus/prometheus/storage"
)
Expand Down Expand Up @@ -448,10 +449,8 @@ func targetsFromGroup(tg *config.TargetGroup, cfg *config.ScrapeConfig) ([]*Targ

preRelabelLabels := labels

labels, err := Relabel(labels, cfg.RelabelConfigs...)
if err != nil {
return nil, fmt.Errorf("error while relabeling instance %d in target group %s: %s", i, tg, err)
}
labels := relabel.Process(labels, cfg.RelabelConfigs...)

// Check if the target was dropped.
if labels == nil {
continue
Expand All @@ -469,7 +468,7 @@ func targetsFromGroup(tg *config.TargetGroup, cfg *config.ScrapeConfig) ([]*Targ
}
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
}

Expand Down

0 comments on commit 7714b9c

Please sign in to comment.