mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
refactored TestScrapeLoopAppend and added a test for empty labels
This commit is contained in:
parent
4addee2bee
commit
675ce533c9
|
@ -112,7 +112,7 @@ func (ls Labels) Get(name string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Has returns if the label with the given name is present.
|
// Has returns true if the label with the given name is present.
|
||||||
func (ls Labels) Has(name string) bool {
|
func (ls Labels) Has(name string) bool {
|
||||||
for _, l := range ls {
|
for _, l := range ls {
|
||||||
if l.Name == name {
|
if l.Name == name {
|
||||||
|
|
|
@ -660,42 +660,97 @@ func TestScrapeLoopRunCreatesStaleMarkersOnParseFailure(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestScrapeLoopAppend(t *testing.T) {
|
func TestScrapeLoopAppend(t *testing.T) {
|
||||||
app := &collectResultAppender{}
|
|
||||||
|
|
||||||
sl := newScrapeLoop(context.Background(),
|
tests := []struct {
|
||||||
nil, nil, nil,
|
title string
|
||||||
nopMutator,
|
honorLabels bool
|
||||||
nopMutator,
|
scrapeLabels string
|
||||||
func() storage.Appender { return app },
|
discoveryLabels []string
|
||||||
)
|
expLset labels.Labels
|
||||||
|
expValue float64
|
||||||
now := time.Now()
|
}{
|
||||||
_, _, err := sl.append([]byte("metric_a 1\nmetric_b NaN\n"), now)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Unexpected append error: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ingestedNaN := math.Float64bits(app.result[1].v)
|
|
||||||
if ingestedNaN != value.NormalNaN {
|
|
||||||
t.Fatalf("Appended NaN samples wasn't as expected. Wanted: %x Got: %x", value.NormalNaN, ingestedNaN)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeepEqual will report NaNs as being different, so replace with a different value.
|
|
||||||
app.result[1].v = 42
|
|
||||||
want := []sample{
|
|
||||||
{
|
{
|
||||||
metric: labels.FromStrings(model.MetricNameLabel, "metric_a"),
|
// When "honor_labels" is not set
|
||||||
t: timestamp.FromTime(now),
|
// label name collision is handler by adding a prefix.
|
||||||
v: 1,
|
title: "Label name collision",
|
||||||
},
|
honorLabels: false,
|
||||||
{
|
scrapeLabels: `metric{n="1"} 0`,
|
||||||
metric: labels.FromStrings(model.MetricNameLabel, "metric_b"),
|
discoveryLabels: []string{"n", "2"},
|
||||||
t: timestamp.FromTime(now),
|
expLset: labels.FromStrings("__name__", "metric", "exported_n", "1", "n", "2"),
|
||||||
v: 42,
|
expValue: 0,
|
||||||
|
}, {
|
||||||
|
// Labels with no value need to be removed as these should not be ingested.
|
||||||
|
title: "Delete Empty labels",
|
||||||
|
honorLabels: false,
|
||||||
|
scrapeLabels: `metric{n=""} 0`,
|
||||||
|
discoveryLabels: nil,
|
||||||
|
expLset: labels.FromStrings("__name__", "metric"),
|
||||||
|
expValue: 0,
|
||||||
|
}, {
|
||||||
|
// Honor Labels should ignore labels with the same name.
|
||||||
|
title: "Honor Labels",
|
||||||
|
honorLabels: true,
|
||||||
|
scrapeLabels: `metric{n1="1" n2="2"} 0`,
|
||||||
|
discoveryLabels: []string{"n1", "0"},
|
||||||
|
expLset: labels.FromStrings("__name__", "metric", "n1", "1", "n2", "2"),
|
||||||
|
expValue: 0,
|
||||||
|
}, {
|
||||||
|
title: "Stale - NaN",
|
||||||
|
honorLabels: false,
|
||||||
|
scrapeLabels: `metric NaN`,
|
||||||
|
discoveryLabels: nil,
|
||||||
|
expLset: labels.FromStrings("__name__", "metric"),
|
||||||
|
expValue: float64(value.NormalNaN),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(want, app.result) {
|
|
||||||
t.Fatalf("Appended samples not as expected. Wanted: %+v Got: %+v", want, app.result)
|
for _, test := range tests {
|
||||||
|
app := &collectResultAppender{}
|
||||||
|
sp := &scrapePool{
|
||||||
|
config: &config.ScrapeConfig{
|
||||||
|
HonorLabels: test.honorLabels,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
discoveryLabels := &Target{
|
||||||
|
labels: labels.FromStrings(test.discoveryLabels...),
|
||||||
|
}
|
||||||
|
|
||||||
|
sl := newScrapeLoop(context.Background(),
|
||||||
|
nil, nil, nil,
|
||||||
|
func(l labels.Labels) labels.Labels {
|
||||||
|
return sp.mutateSampleLabels(l, discoveryLabels)
|
||||||
|
},
|
||||||
|
func(l labels.Labels) labels.Labels {
|
||||||
|
return sp.mutateReportSampleLabels(l, discoveryLabels)
|
||||||
|
},
|
||||||
|
func() storage.Appender { return app },
|
||||||
|
)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
_, _, err := sl.append([]byte(test.scrapeLabels), now)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected append error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := []sample{
|
||||||
|
{
|
||||||
|
metric: test.expLset,
|
||||||
|
t: timestamp.FromTime(now),
|
||||||
|
v: test.expValue,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the expected value is NaN
|
||||||
|
// DeepEqual will report NaNs as being different,
|
||||||
|
// so replace it with the expected one.
|
||||||
|
if test.expValue == float64(value.NormalNaN) {
|
||||||
|
app.result[0].v = expected[0].v
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Test:%s", test.title)
|
||||||
|
testutil.Equals(t, expected, app.result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue