prometheus/pkg/labels/labels_test.go
Julius Volz ae93bae88f Fix: exclude metric name in labels.MatchLabels() (#6523)
This function is only used in one place to format an error message when
encountering multiple matches on the "one" side of a
one-to-many/many-to-one vector match, which is probably why nobodoy has
noticed this before. The hashing is already done correctly and excludes
the metric name label when using the "ignoring" matching modifier.

Signed-off-by: Julius Volz <julius.volz@gmail.com>
2019-12-27 09:32:19 +00:00

152 lines
2.8 KiB
Go

// Copyright 2019 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.
package labels
import (
"testing"
"github.com/prometheus/prometheus/util/testutil"
)
func TestLabels_MatchLabels(t *testing.T) {
labels := Labels{
{
Name: "__name__",
Value: "ALERTS",
},
{
Name: "alertname",
Value: "HTTPRequestRateLow",
},
{
Name: "alertstate",
Value: "pending",
},
{
Name: "instance",
Value: "0",
},
{
Name: "job",
Value: "app-server",
},
{
Name: "severity",
Value: "critical",
},
}
tests := []struct {
providedNames []string
on bool
expected Labels
}{
// on = true, explicitly including metric name in matching.
{
providedNames: []string{
"__name__",
"alertname",
"alertstate",
"instance",
},
on: true,
expected: Labels{
{
Name: "__name__",
Value: "ALERTS",
},
{
Name: "alertname",
Value: "HTTPRequestRateLow",
},
{
Name: "alertstate",
Value: "pending",
},
{
Name: "instance",
Value: "0",
},
},
},
// on = false, explicitly excluding metric name from matching.
{
providedNames: []string{
"__name__",
"alertname",
"alertstate",
"instance",
},
on: false,
expected: Labels{
{
Name: "job",
Value: "app-server",
},
{
Name: "severity",
Value: "critical",
},
},
},
// on = true, explicitly excluding metric name from matching.
{
providedNames: []string{
"alertname",
"alertstate",
"instance",
},
on: true,
expected: Labels{
{
Name: "alertname",
Value: "HTTPRequestRateLow",
},
{
Name: "alertstate",
Value: "pending",
},
{
Name: "instance",
Value: "0",
},
},
},
// on = false, implicitly excluding metric name from matching.
{
providedNames: []string{
"alertname",
"alertstate",
"instance",
},
on: false,
expected: Labels{
{
Name: "job",
Value: "app-server",
},
{
Name: "severity",
Value: "critical",
},
},
},
}
for i, test := range tests {
got := labels.MatchLabels(test.on, test.providedNames...)
testutil.Equals(t, test.expected, got, "unexpected labelset for test case %d", i)
}
}