Skip to content

Commit

Permalink
Add Prometheus Receiver tests for honor_labels configuration (open-te…
Browse files Browse the repository at this point in the history
…lemetry#6369)

* refactor existing external labels test

* Add test for label_limit configuration

* shutdown mock prometheus http test server within testHelper

* fix spelling

* fix lint error

* rename testHelper to testMetricsReceiver

* fix: lint issue in metrics_receiver_helper_test.go

* Add Prometheus receiver test for honor_labels configuration

* update TestHonorLabelsFalseConfig to use testComponent
  • Loading branch information
mustafain117 authored and jamesmoessis committed Dec 6, 2021
1 parent 8d8b363 commit a67b3fa
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions receiver/prometheusreceiver/metrics_receiver_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,100 @@ func TestEmptyLabelValues(t *testing.T) {
}
testComponent(t, targets, false, "")
}

const honorLabelsTarget = `
# HELP test_gauge0 This is my gauge
# TYPE test_gauge0 gauge
test_gauge0{instance="hostname:8080",job="honor_labels_test",testLabel="value1"} 1
`

func verifyHonorLabelsFalse(t *testing.T, td *testData, rms []*pdata.ResourceMetrics) {
want := td.attributes
require.Greater(t, len(rms), 0, "At least one resource metric should be present")

metrics1 := rms[0].InstrumentationLibraryMetrics().At(0).Metrics()
ts1 := metrics1.At(0).Gauge().DataPoints().At(0).Timestamp()

doCompare(t, "honor_labels_false", want, rms[0], []testExpectation{
assertMetricPresent("test_gauge0",
compareMetricType(pdata.MetricDataTypeGauge),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(1),
//job and instance labels must be prefixed with "exported_"
compareAttributes(map[string]string{"exported_job": "honor_labels_test", "exported_instance": "hostname:8080", "testLabel": "value1"}),
},
},
}),
})
}

func TestHonorLabelsFalseConfig(t *testing.T) {
targets := []*testData{
{
name: "target1",
pages: []mockPrometheusResponse{
{code: 200, data: honorLabelsTarget},
},
validateFunc: verifyHonorLabelsFalse,
},
}

testComponent(t, targets, false, "")
}

func verifyHonorLabelsTrue(t *testing.T, td *testData, rms []*pdata.ResourceMetrics) {
//Test for honor_labels: true is skipped. Currently, the Prometheus receiver is unable to support this config
//See: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/5757
//TODO: Enable this test once issue 5757 is resolved
t.Skip("skipping test for honor_labels true configuration")

require.Greater(t, len(rms), 0, "At least one resource metric should be present")

//job and instance label values should be honored from honorLabelsTarget
expectedAttributes := td.attributes
expectedAttributes.Update("job", pdata.NewAttributeValueString("honor_labels_test"))
expectedAttributes.Update("instance", pdata.NewAttributeValueString("hostname:8080"))
expectedAttributes.Update("host.name", pdata.NewAttributeValueString("hostname"))
expectedAttributes.Update("port", pdata.NewAttributeValueString("8080"))

metrics1 := rms[0].InstrumentationLibraryMetrics().At(0).Metrics()
ts1 := metrics1.At(0).Gauge().DataPoints().At(0).Timestamp()

doCompare(t, "honor_labels_true", expectedAttributes, rms[0], []testExpectation{
assertMetricPresent("test_gauge0",
compareMetricType(pdata.MetricDataTypeGauge),
[]dataPointExpectation{
{
numberPointComparator: []numberPointComparator{
compareTimestamp(ts1),
compareDoubleValue(1),
compareAttributes(map[string]string{"testLabel": "value1"}),
},
},
}),
})
}

func TestHonorLabelsTrueConfig(t *testing.T) {
targets := []*testData{
{
name: "honor_labels_test",
pages: []mockPrometheusResponse{
{code: 200, data: honorLabelsTarget},
},
validateFunc: verifyHonorLabelsTrue,
},
}

mp, cfg, err := setupMockPrometheus(targets...)
require.Nilf(t, err, "Failed to create Prometheus config: %v", err)

for _, scrapeCfg := range cfg.ScrapeConfigs {
scrapeCfg.HonorLabels = true
}

testComponentCustomConfig(t, targets, mp, cfg)
}

0 comments on commit a67b3fa

Please sign in to comment.