From 03a3b39b39115c749fbca8f990d242de112bb6eb Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 09:19:13 +0200 Subject: [PATCH 01/22] Try to make internal prometheus scrapper metrics working --- .../internal/metricsbuilder.go | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/receiver/prometheusreceiver/internal/metricsbuilder.go b/receiver/prometheusreceiver/internal/metricsbuilder.go index 9e02554818d1..05c5f22da026 100644 --- a/receiver/prometheusreceiver/internal/metricsbuilder.go +++ b/receiver/prometheusreceiver/internal/metricsbuilder.go @@ -114,7 +114,7 @@ func (b *metricBuilder) AddDataPoint(ls labels.Labels, t int64, v float64) error case isInternalMetric(metricName): b.hasInternalMetric = true lm := ls.Map() - delete(lm, model.MetricNameLabel) + b.defineInternalMetric(metricName) // See https://www.prometheus.io/docs/concepts/jobs_instances/#automatically-generated-labels-and-time-series // up: 1 if the instance is healthy, i.e. reachable, or 0 if the scrape failed. if metricName == scrapeUpMetricName && v != 1.0 { @@ -129,7 +129,6 @@ func (b *metricBuilder) AddDataPoint(ls labels.Labels, t int64, v float64) error zap.String("target_labels", fmt.Sprintf("%v", lm))) } } - return nil case b.useStartTimeMetric && b.matchStartTimeMetric(metricName): b.startTime = v } @@ -151,6 +150,42 @@ func (b *metricBuilder) AddDataPoint(ls labels.Labels, t int64, v float64) error return b.currentMf.Add(metricName, ls, t, v) } +func (b *metricBuilder) defineInternalMetric(metricName string) { + metadata, ok := b.mc.Metadata(metricName) + if ok { + b.logger.Debug("Internal metric seems correct") + return + } + if metadata.Metric != "" && metadata.Type != "" && metadata.Unit != "" && metadata.Help != "" { + b.logger.Debug("Internal metric seems already fully defined") + } + metadata.Metric = metricName + + switch metricName { + case scrapeUpMetricName: + metadata.Unit = "bool" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The scraping was sucessful" + case "scrape_duration_seconds": + metadata.Unit = "seconds" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "Duration of the scrape" + case "scrape_samples_scraped": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The number of samples the target exposed" + case "scrape_series_added": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The approximate number of new series in this scrape" + case "scrape_samples_post_metric_relabeling": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The number of samples remaining after metric relabeling was applied" + } + b.logger.Info("Internal metric defined", zap.String("metadata", fmt.Sprintf("%+v", metadata))) +} + // Build an opencensus data.MetricsData based on all added data complexValue. // The only error returned by this function is errNoDataToBuild. func (b *metricBuilder) Build() ([]*metricspb.Metric, int, int, error) { From 2215a65deb1f225105337dbc3e754b494cef6555 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 11:51:58 +0200 Subject: [PATCH 02/22] Alternative method, working directly on metricFamily --- .../internal/metricfamily.go | 45 ++++++++++++++++++- .../internal/metricsbuilder.go | 37 --------------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/receiver/prometheusreceiver/internal/metricfamily.go b/receiver/prometheusreceiver/internal/metricfamily.go index eca80c3fdeca..59b42b4b4a95 100644 --- a/receiver/prometheusreceiver/internal/metricfamily.go +++ b/receiver/prometheusreceiver/internal/metricfamily.go @@ -62,11 +62,18 @@ func newMetricFamily(metricName string, mc MetadataCache) MetricFamily { metadata.Metric = familyName metadata.Type = textparse.MetricTypeUnknown } - } + } else if !ok && isInternalMetric(metricName) { + metadata = defineInternalMetric(metricName, metadata) + } + //TODO convert it to OtelMetrics ? + ocaMetricType := convToOCAMetricType(metadata.Type) + if ocaMetricType == metricspb.MetricDescriptor_UNSPECIFIED { + //b.logger.Debug(fmt.Sprintf("Invalid metric : %s %+v", metricName, metadata)) + } return &metricFamily{ name: familyName, - mtype: convToOCAMetricType(metadata.Type), + mtype: ocaMetricType, mc: mc, droppedTimeseries: 0, labelKeys: make(map[string]bool), @@ -77,6 +84,40 @@ func newMetricFamily(metricName string, mc MetadataCache) MetricFamily { } } +// Define manualy the metadata of prometheus scrapper internal metrics +func defineInternalMetric(metricName string, metadata scrape.MetricMetadata) (scrape.MetricMetadata) { + if metadata.Metric != "" && metadata.Type != "" && metadata.Unit != "" && metadata.Help != "" { + //b.logger.Debug("Internal metric seems already fully defined") + return metadata + } + metadata.Metric = metricName + + switch metricName { + case scrapeUpMetricName: + metadata.Unit = "bool" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The scraping was sucessful" + case "scrape_duration_seconds": + metadata.Unit = "seconds" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "Duration of the scrape" + case "scrape_samples_scraped": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The number of samples the target exposed" + case "scrape_series_added": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The approximate number of new series in this scrape" + case "scrape_samples_post_metric_relabeling": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The number of samples remaining after metric relabeling was applied" + } + //b.logger.Info("Internal metric defined", zap.String("metadata", fmt.Sprintf("%+v", metadata))) + return metadata +} + func (mf *metricFamily) IsSameFamily(metricName string) bool { // trim known suffix if necessary familyName := normalizeMetricName(metricName) diff --git a/receiver/prometheusreceiver/internal/metricsbuilder.go b/receiver/prometheusreceiver/internal/metricsbuilder.go index 05c5f22da026..3268a5a4ed4a 100644 --- a/receiver/prometheusreceiver/internal/metricsbuilder.go +++ b/receiver/prometheusreceiver/internal/metricsbuilder.go @@ -114,7 +114,6 @@ func (b *metricBuilder) AddDataPoint(ls labels.Labels, t int64, v float64) error case isInternalMetric(metricName): b.hasInternalMetric = true lm := ls.Map() - b.defineInternalMetric(metricName) // See https://www.prometheus.io/docs/concepts/jobs_instances/#automatically-generated-labels-and-time-series // up: 1 if the instance is healthy, i.e. reachable, or 0 if the scrape failed. if metricName == scrapeUpMetricName && v != 1.0 { @@ -150,42 +149,6 @@ func (b *metricBuilder) AddDataPoint(ls labels.Labels, t int64, v float64) error return b.currentMf.Add(metricName, ls, t, v) } -func (b *metricBuilder) defineInternalMetric(metricName string) { - metadata, ok := b.mc.Metadata(metricName) - if ok { - b.logger.Debug("Internal metric seems correct") - return - } - if metadata.Metric != "" && metadata.Type != "" && metadata.Unit != "" && metadata.Help != "" { - b.logger.Debug("Internal metric seems already fully defined") - } - metadata.Metric = metricName - - switch metricName { - case scrapeUpMetricName: - metadata.Unit = "bool" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The scraping was sucessful" - case "scrape_duration_seconds": - metadata.Unit = "seconds" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "Duration of the scrape" - case "scrape_samples_scraped": - metadata.Unit = "count" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The number of samples the target exposed" - case "scrape_series_added": - metadata.Unit = "count" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The approximate number of new series in this scrape" - case "scrape_samples_post_metric_relabeling": - metadata.Unit = "count" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The number of samples remaining after metric relabeling was applied" - } - b.logger.Info("Internal metric defined", zap.String("metadata", fmt.Sprintf("%+v", metadata))) -} - // Build an opencensus data.MetricsData based on all added data complexValue. // The only error returned by this function is errNoDataToBuild. func (b *metricBuilder) Build() ([]*metricspb.Metric, int, int, error) { From 6a71cc8a5007ffd923dc34482797fc88db08341a Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 14:32:20 +0200 Subject: [PATCH 03/22] Try to fix unittests --- .../internal/metricfamily.go | 76 +++++++++---------- .../metrics_receiver_test.go | 15 ++-- 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/receiver/prometheusreceiver/internal/metricfamily.go b/receiver/prometheusreceiver/internal/metricfamily.go index 59b42b4b4a95..ebdc6bc500ad 100644 --- a/receiver/prometheusreceiver/internal/metricfamily.go +++ b/receiver/prometheusreceiver/internal/metricfamily.go @@ -63,13 +63,13 @@ func newMetricFamily(metricName string, mc MetadataCache) MetricFamily { metadata.Type = textparse.MetricTypeUnknown } } else if !ok && isInternalMetric(metricName) { - metadata = defineInternalMetric(metricName, metadata) - } - //TODO convert it to OtelMetrics ? - ocaMetricType := convToOCAMetricType(metadata.Type) - if ocaMetricType == metricspb.MetricDescriptor_UNSPECIFIED { - //b.logger.Debug(fmt.Sprintf("Invalid metric : %s %+v", metricName, metadata)) - } + metadata = defineInternalMetric(metricName, metadata) + } + //TODO convert it to OtelMetrics ? + ocaMetricType := convToOCAMetricType(metadata.Type) + if ocaMetricType == metricspb.MetricDescriptor_UNSPECIFIED { + //b.logger.Debug(fmt.Sprintf("Invalid metric : %s %+v", metricName, metadata)) + } return &metricFamily{ name: familyName, @@ -85,37 +85,37 @@ func newMetricFamily(metricName string, mc MetadataCache) MetricFamily { } // Define manualy the metadata of prometheus scrapper internal metrics -func defineInternalMetric(metricName string, metadata scrape.MetricMetadata) (scrape.MetricMetadata) { - if metadata.Metric != "" && metadata.Type != "" && metadata.Unit != "" && metadata.Help != "" { - //b.logger.Debug("Internal metric seems already fully defined") - return metadata - } - metadata.Metric = metricName - - switch metricName { - case scrapeUpMetricName: - metadata.Unit = "bool" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The scraping was sucessful" - case "scrape_duration_seconds": - metadata.Unit = "seconds" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "Duration of the scrape" - case "scrape_samples_scraped": - metadata.Unit = "count" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The number of samples the target exposed" - case "scrape_series_added": - metadata.Unit = "count" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The approximate number of new series in this scrape" - case "scrape_samples_post_metric_relabeling": - metadata.Unit = "count" - metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The number of samples remaining after metric relabeling was applied" - } - //b.logger.Info("Internal metric defined", zap.String("metadata", fmt.Sprintf("%+v", metadata))) - return metadata +func defineInternalMetric(metricName string, metadata scrape.MetricMetadata) scrape.MetricMetadata { + if metadata.Metric != "" && metadata.Type != "" && metadata.Unit != "" && metadata.Help != "" { + //b.logger.Debug("Internal metric seems already fully defined") + return metadata + } + metadata.Metric = metricName + + switch metricName { + case scrapeUpMetricName: + metadata.Unit = "bool" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The scraping was sucessful" + case "scrape_duration_seconds": + metadata.Unit = "seconds" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "Duration of the scrape" + case "scrape_samples_scraped": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The number of samples the target exposed" + case "scrape_series_added": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The approximate number of new series in this scrape" + case "scrape_samples_post_metric_relabeling": + metadata.Unit = "count" + metadata.Type = textparse.MetricTypeGauge + metadata.Help = "The number of samples remaining after metric relabeling was applied" + } + //b.logger.Info("Internal metric defined", zap.String("metadata", fmt.Sprintf("%+v", metadata))) + return metadata } func (mf *metricFamily) IsSameFamily(metricName string) bool { diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index 17513d6f6c64..87e45cb4fe22 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -255,8 +255,9 @@ rpc_duration_seconds_count 1001 func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetricsServiceRequest) { verifyNumScrapeResults(t, td, mds) m1 := mds[0] - if l := len(m1.Metrics); l != 4 { - t.Errorf("want 1, but got %v\n", l) + // m1 has 4 metrics + 5 internal scraper metrics + if l := len(m1.Metrics); l != 9 { + t.Errorf("want 9, but got %v\n", l) } ts1 := m1.Metrics[0].Timeseries[0].Points[0].Timestamp @@ -588,8 +589,9 @@ http_requests_total{method="post",code="500"} 5 func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetricsServiceRequest) { verifyNumScrapeResults(t, td, mds) m1 := mds[0] - if l := len(m1.Metrics); l != 2 { - t.Errorf("want 1, but got %v\n", l) + // m1 has 2 metrics + 5 internal scraper metrics + if l := len(m1.Metrics); l != 7 { + t.Errorf("want 7, but got %v\n", l) } ts1 := m1.Metrics[0].Timeseries[0].Points[0].Timestamp @@ -988,8 +990,9 @@ rpc_duration_seconds_count{foo="no_quantile"} 55 func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetricsServiceRequest) { verifyNumScrapeResults(t, td, mds) m1 := mds[0] - if l := len(m1.Metrics); l != 3 { - t.Errorf("want 1, but got %v\n", l) + // m1 has 3 metrics + 5 internal scraper metrics + if l := len(m1.Metrics); l != 8 { + t.Errorf("want 8, but got %v\n", l) } ts1 := m1.Metrics[1].Timeseries[0].Points[0].Timestamp From 6071e60701af1a6f8dc4f96bffa3c8414d16d416 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 14:47:26 +0200 Subject: [PATCH 04/22] Try to fix prometheus exporter tests --- exporter/prometheusexporter/end_to_end_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/exporter/prometheusexporter/end_to_end_test.go b/exporter/prometheusexporter/end_to_end_test.go index 3e728a95a600..3627a450fbc1 100644 --- a/exporter/prometheusexporter/end_to_end_test.go +++ b/exporter/prometheusexporter/end_to_end_test.go @@ -152,6 +152,21 @@ func TestEndToEndSummarySupport(t *testing.T) { `test_jvm_memory_pool_bytes_used.pool="G1 Old Gen". 4.385408e.06.*`, `test_jvm_memory_pool_bytes_used.pool="G1 Survivor Space". 8.388608e.06.*`, `test_jvm_memory_pool_bytes_used.pool="Metaspace". 2.6218176e.07.*`, + `. HELP test_scrape_duration_seconds Duration of the scrape`, + `. TYPE test_scrape_duration_seconds gauge`, + `test_scrape_duration_seconds 0.0*`, + `. HELP test_scrape_samples_post_metric_relabeling The number of samples remaining after metric relabeling was applied`, + `. TYPE test_scrape_samples_post_metric_relabeling gauge`, + `test_scrape_samples_post_metric_relabeling 13`, + `. HELP test_scrape_samples_scraped The number of samples the target exposed`, + `. TYPE test_scrape_samples_scraped gauge`, + `test_scrape_samples_scraped 13`, + `. HELP test_scrape_series_added The approximate number of new series in this scrape`, + `. TYPE test_scrape_series_added gauge`, + `test_scrape_series_added 13`, + `. HELP test_up The scraping was sucessful`, + `. TYPE test_up gauge`, + `test_up 1`, } // 5.5: Perform a complete line by line prefix verification to ensure we extract back the inputs From a2bb22cc6176c3b4c4344382d50e173a5e72b9a7 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 14:54:16 +0200 Subject: [PATCH 05/22] Try to fix prometheus exporter tests --- exporter/prometheusexporter/end_to_end_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/exporter/prometheusexporter/end_to_end_test.go b/exporter/prometheusexporter/end_to_end_test.go index 3627a450fbc1..48efe977819e 100644 --- a/exporter/prometheusexporter/end_to_end_test.go +++ b/exporter/prometheusexporter/end_to_end_test.go @@ -154,19 +154,19 @@ func TestEndToEndSummarySupport(t *testing.T) { `test_jvm_memory_pool_bytes_used.pool="Metaspace". 2.6218176e.07.*`, `. HELP test_scrape_duration_seconds Duration of the scrape`, `. TYPE test_scrape_duration_seconds gauge`, - `test_scrape_duration_seconds 0.0*`, + `test_scrape_duration_seconds 0.0.*`, `. HELP test_scrape_samples_post_metric_relabeling The number of samples remaining after metric relabeling was applied`, `. TYPE test_scrape_samples_post_metric_relabeling gauge`, - `test_scrape_samples_post_metric_relabeling 13`, + `test_scrape_samples_post_metric_relabeling 13 .*`, `. HELP test_scrape_samples_scraped The number of samples the target exposed`, `. TYPE test_scrape_samples_scraped gauge`, - `test_scrape_samples_scraped 13`, + `test_scrape_samples_scraped 13 .*`, `. HELP test_scrape_series_added The approximate number of new series in this scrape`, `. TYPE test_scrape_series_added gauge`, - `test_scrape_series_added 13`, + `test_scrape_series_added 13 .*`, `. HELP test_up The scraping was sucessful`, `. TYPE test_up gauge`, - `test_up 1`, + `test_up 1 .*`, } // 5.5: Perform a complete line by line prefix verification to ensure we extract back the inputs From fe1cf0f7e889d6a6b716840749d36e7644a5cfd2 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 15:32:15 +0200 Subject: [PATCH 06/22] Remove unwanted test about internal metrics --- .../internal/metricsbuilder_test.go | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/receiver/prometheusreceiver/internal/metricsbuilder_test.go b/receiver/prometheusreceiver/internal/metricsbuilder_test.go index 1970af746256..0392b5274a6c 100644 --- a/receiver/prometheusreceiver/internal/metricsbuilder_test.go +++ b/receiver/prometheusreceiver/internal/metricsbuilder_test.go @@ -1198,34 +1198,6 @@ func Test_metricBuilder_summary(t *testing.T) { runBuilderTests(t, tests) } -func Test_metricBuilder_skipped(t *testing.T) { - tests := []buildTestData{ - { - name: "skip-internal-metrics", - inputs: []*testScrapedPage{ - { - pts: []*testDataPoint{ - createDataPoint("scrape_foo", 1), - createDataPoint("up", 1.0), - }, - }, - { - pts: []*testDataPoint{ - createDataPoint("scrape_foo", 2), - createDataPoint("up", 2.0), - }, - }, - }, - wants: [][]*metricspb.Metric{ - {}, - {}, - }, - }, - } - - runBuilderTests(t, tests) -} - func Test_metricBuilder_baddata(t *testing.T) { t.Run("empty-metric-name", func(t *testing.T) { mc := newMockMetadataCache(testMetadata) From 884659542a6fcb08a9e3f374a585f4d6b1f68390 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 15:50:14 +0200 Subject: [PATCH 07/22] Try to fix unittests --- .../metrics_receiver_test.go | 390 ++++++++++++++++++ 1 file changed, 390 insertions(+) diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index 87e45cb4fe22..8ed0c74e843e 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -516,6 +516,71 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_duration_seconds", + Description: "Duration of the scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, }, } @@ -710,6 +775,71 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_duration_seconds", + Description: "Duration of the scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, }, } doCompare("scrape2", t, want2, m2) @@ -777,6 +907,71 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_duration_seconds", + Description: "Duration of the scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, }, } doCompare("scrape3", t, want3, m3) @@ -843,6 +1038,71 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_duration_seconds", + Description: "Duration of the scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, }, } doCompare("scrape4", t, want4, m4) @@ -910,6 +1170,71 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_duration_seconds", + Description: "Duration of the scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, }, } doCompare("scrape5", t, want5, m5) @@ -1251,6 +1576,71 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_duration_seconds", + Description: "Duration of the scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, }, } From 2c765b4c242bb3d5e09979e32475592c80cd5a14 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 16:17:28 +0200 Subject: [PATCH 08/22] Try to fix unittests --- .../metrics_receiver_test.go | 288 ++++++++++++------ 1 file changed, 195 insertions(+), 93 deletions(-) diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index 8ed0c74e843e..279563dcc2eb 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -397,6 +397,21 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri Node: td.node, Resource: td.resource, Metrics: []*metricspb.Metric{ + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "bool", + }, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, { MetricDescriptor: &metricspb.MetricDescriptor{ Name: "go_threads", @@ -520,7 +535,9 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_duration_seconds", Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "seconds", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -531,9 +548,11 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -544,9 +563,11 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -559,24 +580,13 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_series_added", Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, }, }, }, @@ -721,6 +731,21 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri Node: td.node, Resource: td.resource, Metrics: []*metricspb.Metric{ + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "bool", + }, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, { MetricDescriptor: &metricspb.MetricDescriptor{ Name: "go_threads", @@ -779,7 +804,9 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_duration_seconds", Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "seconds", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -790,9 +817,11 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -803,9 +832,11 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -818,24 +849,13 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_series_added", Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, }, }, }, @@ -853,6 +873,21 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri Node: td.node, Resource: td.resource, Metrics: []*metricspb.Metric{ + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "bool", + }, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, { MetricDescriptor: &metricspb.MetricDescriptor{ Name: "go_threads", @@ -1042,7 +1077,9 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_duration_seconds", Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "seconds", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -1053,9 +1090,11 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -1066,9 +1105,11 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -1081,24 +1122,13 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_series_added", Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, }, }, }, @@ -1116,6 +1146,21 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri Node: td.node, Resource: td.resource, Metrics: []*metricspb.Metric{ + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "bool", + }, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, { MetricDescriptor: &metricspb.MetricDescriptor{ Name: "go_threads", @@ -1174,7 +1219,9 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_duration_seconds", Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "seconds", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -1185,9 +1232,11 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -1198,9 +1247,11 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -1213,24 +1264,13 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_series_added", Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, }, }, }, @@ -1457,6 +1497,21 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri Node: td.node, Resource: td.resource, Metrics: []*metricspb.Metric{ + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "up", + Description: "The scraping was sucessful", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "bool", + }, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + }, + }, + }, + }, { MetricDescriptor: &metricspb.MetricDescriptor{ Name: "go_threads", @@ -1580,7 +1635,9 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_duration_seconds", Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "seconds", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -1589,11 +1646,28 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, { MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_samples_post_metric_relabeling", Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -1604,8 +1678,36 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_duration_seconds", + Description: "Duration of the scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Timeseries: []*metricspb.TimeSeries{ + { + Points: []*metricspb.Point{ + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, + }, + }, + }, + }, + { + MetricDescriptor: &metricspb.MetricDescriptor{ + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, Timeseries: []*metricspb.TimeSeries{ { @@ -1617,8 +1719,8 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_series_added", - Description: "The approximate number of new series in this scrape", + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, Timeseries: []*metricspb.TimeSeries{ { @@ -1630,13 +1732,13 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, }, }, }, From 2c0c95386091a272a202f46f060446fc6d82ef05 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 6 May 2021 16:32:38 +0200 Subject: [PATCH 09/22] Try to fix unittests --- .../metrics_receiver_test.go | 131 ++++-------------- 1 file changed, 25 insertions(+), 106 deletions(-) diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index 279563dcc2eb..b69a01ccd571 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -946,7 +946,9 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_duration_seconds", Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "seconds", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -957,9 +959,11 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_scraped", + Description: "The number of samples the target exposed", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -970,9 +974,11 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, + Name: "scrape_samples_post_metric_relabeling", + Description: "The number of samples remaining after metric relabeling was applied", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", + }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ @@ -985,24 +991,13 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri MetricDescriptor: &metricspb.MetricDescriptor{ Name: "scrape_series_added", Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, }, }, }, @@ -1021,54 +1016,30 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri Metrics: []*metricspb.Metric{ { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", + Name: "up", + Description: "The scraping was sucessful", Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "bool", }, Timeseries: []*metricspb.TimeSeries{ { Points: []*metricspb.Point{ - {Timestamp: ts4, Value: &metricspb.Point_DoubleValue{DoubleValue: 16.0}}, + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, }, }, }, }, { MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_requests_total", - Description: "The total number of HTTP requests.", - Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, - LabelKeys: []*metricspb.LabelKey{{Key: "code"}, {Key: "method"}}, + Name: "scrape_series_added", + Description: "The approximate number of new series in this scrape", + Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, + Unit: "count", }, Timeseries: []*metricspb.TimeSeries{ { - StartTimestamp: ts4, - LabelValues: []*metricspb.LabelValue{ - {Value: "200", HasValue: true}, - {Value: "post", HasValue: true}, - }, Points: []*metricspb.Point{ - {Timestamp: ts4, Value: &metricspb.Point_DoubleValue{DoubleValue: 49.0}}, - }, - }, - { - StartTimestamp: ts4, - LabelValues: []*metricspb.LabelValue{ - {Value: "400", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts4, Value: &metricspb.Point_DoubleValue{DoubleValue: 59.0}}, - }, - }, - { - StartTimestamp: ts4, - LabelValues: []*metricspb.LabelValue{ - {Value: "500", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts4, Value: &metricspb.Point_DoubleValue{DoubleValue: 3.0}}, + {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, }, }, }, @@ -1691,58 +1662,6 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_duration_seconds", - Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_series_added", - Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, }, } From dc925edf3b3d19785328a2fdaca8440fceab2906 Mon Sep 17 00:00:00 2001 From: Anthony J Mirabella Date: Wed, 26 May 2021 14:44:19 -0400 Subject: [PATCH 10/22] receiver/prometheus: fix e2e tests Signed-off-by: Anthony J Mirabella --- .../metrics_receiver_test.go | 1519 +++++------------ 1 file changed, 429 insertions(+), 1090 deletions(-) diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index b69a01ccd571..f41e74c328c2 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -32,7 +32,6 @@ import ( metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1" gokitlog "github.com/go-kit/kit/log" - "github.com/golang/protobuf/ptypes/wrappers" promcfg "github.com/prometheus/prometheus/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -106,7 +105,10 @@ func (mp *mockPrometheus) Close() { // EndToEnd Test and related // ------------------------- -var srvPlaceHolder = "__SERVER_ADDRESS__" +var ( + srvPlaceHolder = "__SERVER_ADDRESS__" + expectedScrapeMetricCount = 5 +) type testData struct { name string @@ -183,12 +185,141 @@ func verifyNumScrapeResults(t *testing.T, td *testData, mds []*agentmetricspb.Ex } } -func doCompare(name string, t *testing.T, want, got *agentmetricspb.ExportMetricsServiceRequest) { +func doCompare(name string, t *testing.T, want, got *agentmetricspb.ExportMetricsServiceRequest, expectations []testExpectation) { t.Run(name, func(t *testing.T) { - assert.EqualValues(t, want, got) + num_scrape_metrics := count_scrape_metrics(got) + assert.Equal(t, expectedScrapeMetricCount, num_scrape_metrics) + assert.EqualValues(t, want.Node, got.Node) + assert.EqualValues(t, want.Resource, got.Resource) + for _, e := range expectations { + assert.True(t, e(t, got.Metrics)) + } }) } +func getValidScrapes(t *testing.T, mds []*agentmetricspb.ExportMetricsServiceRequest) []*agentmetricspb.ExportMetricsServiceRequest { + out := make([]*agentmetricspb.ExportMetricsServiceRequest, 0) + for _, md := range mds { + // mds will include scrapes that received no metrics but have internal scrape metrics, filter those out + if expectedScrapeMetricCount < len(md.Metrics) && count_scrape_metrics(md) == expectedScrapeMetricCount { + assertUp(t, 1, md) + out = append(out, md) + } else { + assertUp(t, 0, md) + } + } + return out +} + +func assertUp(t *testing.T, expected float64, md *agentmetricspb.ExportMetricsServiceRequest) { + for _, m := range md.Metrics { + if m.GetMetricDescriptor().Name == "up" { + assert.Equal(t, expected, m.Timeseries[0].Points[0].GetDoubleValue()) + return + } + } + t.Error("No 'up' metric found") +} + +func count_scrape_metrics(in *agentmetricspb.ExportMetricsServiceRequest) int { + n := 0 + for _, m := range in.Metrics { + switch m.MetricDescriptor.Name { + case "up", "scrape_duration_seconds", "scrape_samples_scraped", "scrape_samples_post_metric_relabeling", "scrape_series_added": + n += 1 + default: + } + } + return n +} + +type pointComparator func(*testing.T, *metricspb.Point) bool + +type seriesExpectation struct { + startTimestamp *timestamppb.Timestamp + pointTimestamp *timestamppb.Timestamp + compare pointComparator +} + +type testExpectation func(*testing.T, []*metricspb.Metric) bool + +func assertMetricPresent(name string, metricType metricspb.MetricDescriptor_Type, expectations []seriesExpectation) testExpectation { + return func(t *testing.T, metrics []*metricspb.Metric) bool { + for _, m := range metrics { + if name != m.MetricDescriptor.Name { + continue + } + + if !assert.Equal(t, metricType, m.MetricDescriptor.Type) { + return false + } + if !assert.Equal(t, len(expectations), len(m.Timeseries)) { + return false + } + for i, se := range expectations { + if se.startTimestamp != nil && !assert.Equal(t, se.startTimestamp.String(), m.Timeseries[i].StartTimestamp.String()) { + return false + } + if se.pointTimestamp != nil && !assert.Equal(t, m.Timeseries[i].Points[0].Timestamp.String(), se.pointTimestamp.String()) { + return false + } + if se.compare != nil && !se.compare(t, m.Timeseries[i].Points[0]) { + return false + } + } + return true + } + assert.Failf(t, "Unable to match metric expectation", name) + return false + } +} + +func assertMetricAbsent(name string) testExpectation { + return func(t *testing.T, metrics []*metricspb.Metric) bool { + for _, m := range metrics { + if !assert.NotEqual(t, name, m.MetricDescriptor.Name) { + return false + } + } + return true + } +} + +func compareDoubleVal(cmp float64) pointComparator { + return func(t *testing.T, pt *metricspb.Point) bool { + return assert.Equal(t, cmp, pt.GetDoubleValue()) + } +} + +func compareHistogram(count int64, sum float64, buckets []int64) pointComparator { + return func(t *testing.T, pt *metricspb.Point) bool { + ret := assert.Equal(t, count, pt.GetDistributionValue().Count) + ret = ret && assert.Equal(t, sum, pt.GetDistributionValue().Sum) + + if ret { + for i, b := range buckets { + ret = ret && assert.Equal(t, b, pt.GetDistributionValue().Buckets[i].Count) + } + } + return ret + } +} + +func compareSummary(count int64, sum float64, quantiles map[float64]float64) pointComparator { + return func(t *testing.T, pt *metricspb.Point) bool { + ret := assert.Equal(t, count, pt.GetSummaryValue().Count.Value) + ret = ret && assert.Equal(t, sum, pt.GetSummaryValue().Sum.Value) + + if ret { + assert.Equal(t, len(quantiles), len(pt.GetSummaryValue().Snapshot.PercentileValues)) + for _, q := range pt.GetSummaryValue().Snapshot.PercentileValues { + assert.Equal(t, quantiles[q.Percentile], q.Value) + } + } + return ret + } +} + // Test data and validation functions for EndToEnd test // Make sure every page has a gauge, we are relying on it to figure out the starttime if needed @@ -261,133 +392,51 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri } ts1 := m1.Metrics[0].Timeseries[0].Points[0].Timestamp + e1 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts1, + compare: compareDoubleVal(19), + }, + }), + assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareDoubleVal(100), + }, + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareDoubleVal(5), + }, + }), + assertMetricPresent("http_request_duration_seconds", metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareHistogram(2500, 5000, []int64{1000, 500, 500, 500}), + }, + }), + assertMetricPresent("rpc_duration_seconds", metricspb.MetricDescriptor_SUMMARY, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareSummary(1000, 5000, map[float64]float64{1: 1, 90: 5, 99: 8}), + }, + }), + } + want1 := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts1, Value: &metricspb.Point_DoubleValue{DoubleValue: 19.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_requests_total", - Description: "The total number of HTTP requests.", - Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, - LabelKeys: []*metricspb.LabelKey{{Key: "code"}, {Key: "method"}}, - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "200", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts1, Value: &metricspb.Point_DoubleValue{DoubleValue: 100.0}}, - }, - }, - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "400", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts1, Value: &metricspb.Point_DoubleValue{DoubleValue: 5.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_request_duration_seconds", - Type: metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, - Description: "A histogram of the request duration.", - Unit: "s", - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - Points: []*metricspb.Point{ - { - Timestamp: ts1, - Value: &metricspb.Point_DistributionValue{ - DistributionValue: &metricspb.DistributionValue{ - BucketOptions: &metricspb.DistributionValue_BucketOptions{ - Type: &metricspb.DistributionValue_BucketOptions_Explicit_{ - Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{ - Bounds: []float64{0.05, 0.5, 1}, - }, - }, - }, - Count: 2500, - Sum: 5000.0, - Buckets: []*metricspb.DistributionValue_Bucket{ - {Count: 1000}, - {Count: 500}, - {Count: 500}, - {Count: 500}, - }, - }}, - }, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "rpc_duration_seconds", - Type: metricspb.MetricDescriptor_SUMMARY, - Description: "A summary of the RPC duration in seconds.", - Unit: "s", - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - Points: []*metricspb.Point{ - { - Timestamp: ts1, - Value: &metricspb.Point_SummaryValue{ - SummaryValue: &metricspb.SummaryValue{ - Sum: &wrappers.DoubleValue{Value: 5000}, - Count: &wrappers.Int64Value{Value: 1000}, - Snapshot: &metricspb.SummaryValue_Snapshot{ - PercentileValues: []*metricspb.SummaryValue_Snapshot_ValueAtPercentile{ - { - Percentile: 1, - Value: 1, - }, - { - Percentile: 90, - Value: 5, - }, - { - Percentile: 99, - Value: 8, - }, - }, - }, - }, - }, - }, - }, - }, - }, - }, - }, } - doCompare("scrape1", t, want1, m1) + doCompare("scrape1", t, want1, m1, e1) // verify the 2nd metricData m2 := mds[1] @@ -396,205 +445,48 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri want2 := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "bool", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE}, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 18.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_requests_total", - Description: "The total number of HTTP requests.", - Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, - LabelKeys: []*metricspb.LabelKey{{Key: "code"}, {Key: "method"}}, - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "200", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 199.0}}, - }, - }, - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "400", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 12.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_request_duration_seconds", - Type: metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, - Description: "A histogram of the request duration.", - Unit: "s", - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - Points: []*metricspb.Point{ - { - Timestamp: ts2, - Value: &metricspb.Point_DistributionValue{ - DistributionValue: &metricspb.DistributionValue{ - BucketOptions: &metricspb.DistributionValue_BucketOptions{ - Type: &metricspb.DistributionValue_BucketOptions_Explicit_{ - Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{ - Bounds: []float64{0.05, 0.5, 1}, - }, - }, - }, - Count: 2600, - Sum: 5050.0, - Buckets: []*metricspb.DistributionValue_Bucket{ - {Count: 1100}, - {Count: 500}, - {Count: 500}, - {Count: 500}, - }, - }}, - }, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "rpc_duration_seconds", - Type: metricspb.MetricDescriptor_SUMMARY, - Description: "A summary of the RPC duration in seconds.", - Unit: "s", - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - Points: []*metricspb.Point{ - { - Timestamp: ts2, - Value: &metricspb.Point_SummaryValue{ - SummaryValue: &metricspb.SummaryValue{ - Sum: &wrappers.DoubleValue{Value: 5002}, - Count: &wrappers.Int64Value{Value: 1001}, - Snapshot: &metricspb.SummaryValue_Snapshot{ - PercentileValues: []*metricspb.SummaryValue_Snapshot_ValueAtPercentile{ - { - Percentile: 1, - Value: 1, - }, - { - Percentile: 90, - Value: 6, - }, - { - Percentile: 99, - Value: 8, - }, - }, - }, - }, - }, - }, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_duration_seconds", - Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "seconds", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_series_added", - Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - }, } - doCompare("scrape2", t, want2, m2) + e2 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts2, + compare: compareDoubleVal(18), + }, + }), + assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareDoubleVal(199), + }, + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareDoubleVal(12), + }, + }), + assertMetricPresent("http_request_duration_seconds", metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareHistogram(2600, 5050, []int64{1100, 500, 500, 500}), + }, + }), + assertMetricPresent("rpc_duration_seconds", metricspb.MetricDescriptor_SUMMARY, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareSummary(1001, 5002, map[float64]float64{1: 1, 90: 6, 99: 8}), + }, + }), + } + + doCompare("scrape2", t, want2, m2, e2) } // target2 is going to have 5 pages, and there's a newly appeared item from the 2nd page. we are expecting the new @@ -673,55 +565,32 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri want1 := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts1, Value: &metricspb.Point_DoubleValue{DoubleValue: 18.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_requests_total", - Description: "The total number of HTTP requests.", - Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, - LabelKeys: []*metricspb.LabelKey{{Key: "code"}, {Key: "method"}}, - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "200", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts1, Value: &metricspb.Point_DoubleValue{DoubleValue: 10.0}}, - }, - }, - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "400", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts1, Value: &metricspb.Point_DoubleValue{DoubleValue: 50.0}}, - }, - }, - }, - }, - }, } - doCompare("scrape1", t, want1, m1) + e1 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts1, + compare: compareDoubleVal(18), + }, + }), + assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareDoubleVal(10), + }, + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareDoubleVal(50), + }, + }), + } + + doCompare("scrape1", t, want1, m1, e1) // verify the 2nd metricData m2 := mds[1] @@ -730,139 +599,36 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri want2 := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "bool", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 16.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_requests_total", - Description: "The total number of HTTP requests.", - Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, - LabelKeys: []*metricspb.LabelKey{{Key: "code"}, {Key: "method"}}, - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "200", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 50.0}}, - }, - }, - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "400", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 60.0}}, - }, - }, - { - StartTimestamp: ts2, - LabelValues: []*metricspb.LabelValue{ - {Value: "500", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 3.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_duration_seconds", - Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "seconds", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_series_added", - Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - }, } - doCompare("scrape2", t, want2, m2) + e2 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts2, + compare: compareDoubleVal(16), + }, + }), + assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareDoubleVal(50), + }, + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareDoubleVal(60), + }, + { + startTimestamp: ts2, + pointTimestamp: ts2, + compare: compareDoubleVal(3), + }, + }), + } + + doCompare("scrape2", t, want2, m2, e2) // verify the 3rd metricData, with the new code=500 counter which first appeared on 2nd run m3 := mds[2] @@ -872,141 +638,39 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri want3 := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "bool", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts3, Value: &metricspb.Point_DoubleValue{DoubleValue: 16.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_requests_total", - Description: "The total number of HTTP requests.", - Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, - LabelKeys: []*metricspb.LabelKey{{Key: "code"}, {Key: "method"}}, - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "200", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts3, Value: &metricspb.Point_DoubleValue{DoubleValue: 50.0}}, - }, - }, - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{ - {Value: "400", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts3, Value: &metricspb.Point_DoubleValue{DoubleValue: 60.0}}, - }, - }, - { - StartTimestamp: ts2, - LabelValues: []*metricspb.LabelValue{ - {Value: "500", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts3, Value: &metricspb.Point_DoubleValue{DoubleValue: 5.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_duration_seconds", - Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "seconds", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_series_added", - Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - }, } - doCompare("scrape3", t, want3, m3) - // verify the 4th metricData which reset happens, all cumulative types shall be absent + e3 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts3, + compare: compareDoubleVal(16), + }, + }), + assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts3, + compare: compareDoubleVal(50), + }, + { + startTimestamp: ts1, + pointTimestamp: ts3, + compare: compareDoubleVal(60), + }, + { + startTimestamp: ts2, + pointTimestamp: ts3, + compare: compareDoubleVal(5), + }, + }), + } + + doCompare("scrape3", t, want3, m3, e3) + + // verify the 4th metricData which reset happens m4 := mds[3] ts4 := m4.Metrics[0].Timeseries[0].Points[0].Timestamp @@ -1106,149 +770,76 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri }, }, } - doCompare("scrape4", t, want4, m4) - // verify the 4th metricData which reset happens, all cumulative types shall be absent + e4 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts4, + compare: compareDoubleVal(16), + }, + }), + assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + []seriesExpectation{ + { + startTimestamp: ts4, + pointTimestamp: ts4, + compare: compareDoubleVal(49), + }, + { + startTimestamp: ts4, + pointTimestamp: ts4, + compare: compareDoubleVal(59), + }, + { + startTimestamp: ts4, + pointTimestamp: ts4, + compare: compareDoubleVal(3), + }, + }), + } + + doCompare("scrape4", t, want4, m4, e4) + + // verify the 5th metricData which reset happens m5 := mds[4] - // its start timestamp shall be from the 3rd run + // its start timestamp shall be from the 4th run ts5 := m5.Metrics[0].Timeseries[0].Points[0].Timestamp want5 := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "bool", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts5, Value: &metricspb.Point_DoubleValue{DoubleValue: 16.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_requests_total", - Description: "The total number of HTTP requests.", - Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, - LabelKeys: []*metricspb.LabelKey{{Key: "code"}, {Key: "method"}}, - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts4, - LabelValues: []*metricspb.LabelValue{ - {Value: "200", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts5, Value: &metricspb.Point_DoubleValue{DoubleValue: 50.0}}, - }, - }, - { - StartTimestamp: ts4, - LabelValues: []*metricspb.LabelValue{ - {Value: "400", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts5, Value: &metricspb.Point_DoubleValue{DoubleValue: 59.0}}, - }, - }, - { - StartTimestamp: ts4, - LabelValues: []*metricspb.LabelValue{ - {Value: "500", HasValue: true}, - {Value: "post", HasValue: true}, - }, - Points: []*metricspb.Point{ - {Timestamp: ts5, Value: &metricspb.Point_DoubleValue{DoubleValue: 5.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_duration_seconds", - Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "seconds", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_series_added", - Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - }, } - doCompare("scrape5", t, want5, m5) + + e5 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts5, + compare: compareDoubleVal(16), + }, + }), + assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + []seriesExpectation{ + { + startTimestamp: ts4, + pointTimestamp: ts5, + compare: compareDoubleVal(50), + }, + { + startTimestamp: ts4, + pointTimestamp: ts5, + compare: compareDoubleVal(59), + }, + { + startTimestamp: ts4, + pointTimestamp: ts5, + compare: compareDoubleVal(5), + }, + }), + } + + doCompare("scrape5", t, want5, m5, e5) } // target3 for complicated data types, including summaries and histograms. one of the summary and histogram have only @@ -1335,130 +926,41 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri want1 := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts1, Value: &metricspb.Point_DoubleValue{DoubleValue: 18.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_request_duration_seconds", - Description: "A histogram of the request duration.", - Unit: "s", - Type: metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - Points: []*metricspb.Point{ - { - Timestamp: ts1, - Value: &metricspb.Point_DistributionValue{ - DistributionValue: &metricspb.DistributionValue{ - BucketOptions: &metricspb.DistributionValue_BucketOptions{ - Type: &metricspb.DistributionValue_BucketOptions_Explicit_{ - Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{ - Bounds: []float64{0.2, 0.5, 1}, - }, - }, - }, - Count: 13003, - Sum: 50000, - Buckets: []*metricspb.DistributionValue_Bucket{ - {Count: 10000}, - {Count: 1000}, - {Count: 1001}, - {Count: 1002}, - }, - }, - }, - }, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "rpc_duration_seconds", - Type: metricspb.MetricDescriptor_SUMMARY, - LabelKeys: []*metricspb.LabelKey{{Key: "foo"}}, - Description: "A summary of the RPC duration in seconds.", - Unit: "s", - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{{Value: "bar", HasValue: true}}, - Points: []*metricspb.Point{ - { - Timestamp: ts1, - Value: &metricspb.Point_SummaryValue{ - SummaryValue: &metricspb.SummaryValue{ - Sum: &wrappers.DoubleValue{Value: 8000}, - Count: &wrappers.Int64Value{Value: 900}, - Snapshot: &metricspb.SummaryValue_Snapshot{ - PercentileValues: []*metricspb.SummaryValue_Snapshot_ValueAtPercentile{ - { - Percentile: 1, - Value: 31, - }, - { - Percentile: 5, - Value: 35, - }, - { - Percentile: 50, - Value: 47, - }, - { - Percentile: 90, - Value: 70, - }, - { - Percentile: 99, - Value: 76, - }, - }, - }, - }, - }, - }, - }, - }, - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{{Value: "no_quantile", HasValue: true}}, - Points: []*metricspb.Point{ - { - Timestamp: ts1, - Value: &metricspb.Point_SummaryValue{ - SummaryValue: &metricspb.SummaryValue{ - Sum: &wrappers.DoubleValue{Value: 100}, - Count: &wrappers.Int64Value{Value: 50}, - Snapshot: &metricspb.SummaryValue_Snapshot{ - PercentileValues: nil, - }, - }, - }, - }, - }, - }, - }, - }, - }, } - doCompare("scrape1", t, want1, m1) + e1 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts1, + compare: compareDoubleVal(18), + }, + }), + assertMetricPresent("http_request_duration_seconds", metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareHistogram(13003, 50000, []int64{10000, 1000, 1001, 1002}), + }, + }), + assertMetricAbsent("corrupted_hist"), + assertMetricPresent("rpc_duration_seconds", metricspb.MetricDescriptor_SUMMARY, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareSummary(900, 8000, map[float64]float64{1: 31, 5: 35, 50: 47, 90: 70, 99: 76}), + }, + { + startTimestamp: ts1, + pointTimestamp: ts1, + compare: compareSummary(50, 100, map[float64]float64{}), + }, + }), + } + + doCompare("scrape1", t, want1, m1, e1) // verify the 2nd metricData m2 := mds[1] @@ -1467,205 +969,41 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri want2 := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "up", - Description: "The scraping was sucessful", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "bool", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 1.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 16.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "http_request_duration_seconds", - Description: "A histogram of the request duration.", - Unit: "s", - Type: metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - Points: []*metricspb.Point{ - { - Timestamp: ts2, - Value: &metricspb.Point_DistributionValue{ - DistributionValue: &metricspb.DistributionValue{ - BucketOptions: &metricspb.DistributionValue_BucketOptions{ - Type: &metricspb.DistributionValue_BucketOptions_Explicit_{ - Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{ - Bounds: []float64{0.2, 0.5, 1}, - }, - }, - }, - Count: 14003, - Sum: 50100, - Buckets: []*metricspb.DistributionValue_Bucket{ - {Count: 11000}, - {Count: 1000}, - {Count: 1001}, - {Count: 1002}, - }, - }, - }, - }, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "rpc_duration_seconds", - Type: metricspb.MetricDescriptor_SUMMARY, - LabelKeys: []*metricspb.LabelKey{{Key: "foo"}}, - Description: "A summary of the RPC duration in seconds.", - Unit: "s", - }, - Timeseries: []*metricspb.TimeSeries{ - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{{Value: "bar", HasValue: true}}, - Points: []*metricspb.Point{ - { - Timestamp: ts2, - Value: &metricspb.Point_SummaryValue{ - SummaryValue: &metricspb.SummaryValue{ - Sum: &wrappers.DoubleValue{Value: 8100}, - Count: &wrappers.Int64Value{Value: 950}, - Snapshot: &metricspb.SummaryValue_Snapshot{ - PercentileValues: []*metricspb.SummaryValue_Snapshot_ValueAtPercentile{ - { - Percentile: 1, - Value: 32, - }, - { - Percentile: 5, - Value: 35, - }, - { - Percentile: 50, - Value: 47, - }, - { - Percentile: 90, - Value: 70, - }, - { - Percentile: 99, - Value: 77, - }, - }, - }, - }, - }, - }, - }, - }, - { - StartTimestamp: ts1, - LabelValues: []*metricspb.LabelValue{{Value: "no_quantile", HasValue: true}}, - Points: []*metricspb.Point{ - { - Timestamp: ts2, - Value: &metricspb.Point_SummaryValue{ - SummaryValue: &metricspb.SummaryValue{ - Sum: &wrappers.DoubleValue{Value: 101}, - Count: &wrappers.Int64Value{Value: 55}, - Snapshot: &metricspb.SummaryValue_Snapshot{ - PercentileValues: nil, - }, - }, - }, - }, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_duration_seconds", - Description: "Duration of the scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "seconds", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 0.0123456}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_scraped", - Description: "The number of samples the target exposed", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_samples_post_metric_relabeling", - Description: "The number of samples remaining after metric relabeling was applied", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "scrape_series_added", - Description: "The approximate number of new series in this scrape", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - Unit: "count", - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Timestamp: ts2, Value: &metricspb.Point_DoubleValue{DoubleValue: 14.0}}, - }, - }, - }, - }, - }, } - doCompare("scrape2", t, want2, m2) + e2 := []testExpectation{ + assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + []seriesExpectation{ + { + pointTimestamp: ts2, + compare: compareDoubleVal(16), + }, + }), + assertMetricPresent("http_request_duration_seconds", metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareHistogram(14003, 50100, []int64{11000, 1000, 1001, 1002}), + }, + }), + assertMetricAbsent("corrupted_hist"), + assertMetricPresent("rpc_duration_seconds", metricspb.MetricDescriptor_SUMMARY, + []seriesExpectation{ + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareSummary(950, 8100, map[float64]float64{1: 32, 5: 35, 50: 47, 90: 70, 99: 77}), + }, + { + startTimestamp: ts1, + pointTimestamp: ts2, + compare: compareSummary(55, 101, map[float64]float64{}), + }, + }), + } + + doCompare("scrape2", t, want2, m2, e2) } // TestEndToEnd end to end test executor @@ -1811,7 +1149,8 @@ func testEndToEnd(t *testing.T, targets []*testData, useStartTimeMetric bool) { // loop to validate outputs for each targets for _, target := range targets { t.Run(target.name, func(t *testing.T) { - target.validateFunc(t, target, results[target.name]) + mds := getValidScrapes(t, results[target.name]) + target.validateFunc(t, target, mds) }) } } From cec43a7230bbae3c9b0e2b6d8a26af5e5473c118 Mon Sep 17 00:00:00 2001 From: Anthony J Mirabella Date: Wed, 26 May 2021 15:59:11 -0400 Subject: [PATCH 11/22] receiver/prometheus: add e2e tests for label keys and values Signed-off-by: Anthony J Mirabella --- .../metrics_receiver_test.go | 582 +++++++++++++----- .../metrics_reciever_external_labels_test.go | 18 +- 2 files changed, 448 insertions(+), 152 deletions(-) diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index f41e74c328c2..4c54b82ce5a1 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -234,37 +234,42 @@ func count_scrape_metrics(in *agentmetricspb.ExportMetricsServiceRequest) int { } type pointComparator func(*testing.T, *metricspb.Point) bool +type seriesComparator func(*testing.T, *metricspb.TimeSeries) bool +type descriptorComparator func(*testing.T, *metricspb.MetricDescriptor) bool type seriesExpectation struct { - startTimestamp *timestamppb.Timestamp - pointTimestamp *timestamppb.Timestamp - compare pointComparator + series []seriesComparator + points []pointComparator } type testExpectation func(*testing.T, []*metricspb.Metric) bool -func assertMetricPresent(name string, metricType metricspb.MetricDescriptor_Type, expectations []seriesExpectation) testExpectation { +func assertMetricPresent(name string, descriptorExpectations []descriptorComparator, seriesExpectations []seriesExpectation) testExpectation { return func(t *testing.T, metrics []*metricspb.Metric) bool { for _, m := range metrics { if name != m.MetricDescriptor.Name { continue } - if !assert.Equal(t, metricType, m.MetricDescriptor.Type) { - return false + for _, de := range descriptorExpectations { + if !de(t, m.MetricDescriptor) { + return false + } } - if !assert.Equal(t, len(expectations), len(m.Timeseries)) { + + if !assert.Equal(t, len(seriesExpectations), len(m.Timeseries)) { return false } - for i, se := range expectations { - if se.startTimestamp != nil && !assert.Equal(t, se.startTimestamp.String(), m.Timeseries[i].StartTimestamp.String()) { - return false - } - if se.pointTimestamp != nil && !assert.Equal(t, m.Timeseries[i].Points[0].Timestamp.String(), se.pointTimestamp.String()) { - return false + for i, se := range seriesExpectations { + for _, sc := range se.series { + if !sc(t, m.Timeseries[i]) { + return false + } } - if se.compare != nil && !se.compare(t, m.Timeseries[i].Points[0]) { - return false + for _, pc := range se.points { + if !pc(t, m.Timeseries[i].Points[0]) { + return false + } } } return true @@ -285,6 +290,52 @@ func assertMetricAbsent(name string) testExpectation { } } +func compareMetricType(typ metricspb.MetricDescriptor_Type) descriptorComparator { + return func(t *testing.T, descriptor *metricspb.MetricDescriptor) bool { + return assert.Equal(t, typ, descriptor.Type) + } +} + +func compareMetricLabelKeys(keys []string) descriptorComparator { + return func(t *testing.T, descriptor *metricspb.MetricDescriptor) bool { + if !assert.Equal(t, len(keys), len(descriptor.LabelKeys)) { + return false + } + for i, k := range keys { + if !assert.Equal(t, k, descriptor.LabelKeys[i].Key) { + return false + } + } + return true + } +} + +func compareSeriesLabelValues(values []string) seriesComparator { + return func(t *testing.T, series *metricspb.TimeSeries) bool { + if !assert.Equal(t, len(values), len(series.LabelValues)) { + return false + } + for i, v := range values { + if !assert.Equal(t, v, series.LabelValues[i].Value) { + return false + } + } + return true + } +} + +func compareSeriesTimestamp(ts *timestamppb.Timestamp) seriesComparator { + return func(t *testing.T, series *metricspb.TimeSeries) bool { + return assert.Equal(t, ts.String(), series.StartTimestamp.String()) + } +} + +func comparePointTimestamp(ts *timestamppb.Timestamp) pointComparator { + return func(t *testing.T, point *metricspb.Point) bool { + return assert.Equal(t, ts.String(), point.Timestamp.String()) + } +} + func compareDoubleVal(cmp float64) pointComparator { return func(t *testing.T, pt *metricspb.Point) bool { return assert.Equal(t, cmp, pt.GetDoubleValue()) @@ -321,12 +372,10 @@ func compareSummary(count int64, sum float64, quantiles map[float64]float64) poi } // Test data and validation functions for EndToEnd test -// Make sure every page has a gauge, we are relying on it to figure out the starttime if needed +// Make sure every page has a gauge, we are relying on it to figure out the start time if needed -// target1 has one gague, two counts of a same family, one histogram and one summary. We are expecting the first -// successful scrape will produce a metric with only the gauge metric, and the 2nd successful scrape will produce all -// the metrics, with the cumulative types using the firstpage's timestamp as starttime, and values are deltas with the -// one from the first page (summary quantiles will be as it is) +// target1 has one gauge, two counts of a same family, one histogram and one summary. We are expecting the both +// successful scrapes will produce all metrics using the first scrape's timestamp as start time. var target1Page1 = ` # HELP go_threads Number of OS threads created # TYPE go_threads gauge @@ -393,40 +442,73 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri ts1 := m1.Metrics[0].Timeseries[0].Points[0].Timestamp e1 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts1, - compare: compareDoubleVal(19), + points: []pointComparator{ + comparePointTimestamp(ts1), + compareDoubleVal(19), + }, }, }), - assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + assertMetricPresent("http_requests_total", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DOUBLE), + compareMetricLabelKeys([]string{"code", "method"}), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareDoubleVal(100), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"200", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareDoubleVal(100), + }, }, { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareDoubleVal(5), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"400", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareDoubleVal(5), + }, }, }), - assertMetricPresent("http_request_duration_seconds", metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, + assertMetricPresent("http_request_duration_seconds", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareHistogram(2500, 5000, []int64{1000, 500, 500, 500}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareHistogram(2500, 5000, []int64{1000, 500, 500, 500}), + }, }, }), - assertMetricPresent("rpc_duration_seconds", metricspb.MetricDescriptor_SUMMARY, + assertMetricPresent("rpc_duration_seconds", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_SUMMARY), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareSummary(1000, 5000, map[float64]float64{1: 1, 90: 5, 99: 8}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareSummary(1000, 5000, map[float64]float64{1: 1, 90: 5, 99: 8}), + }, }, }), } @@ -448,40 +530,73 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri } e2 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts2, - compare: compareDoubleVal(18), + points: []pointComparator{ + comparePointTimestamp(ts2), + compareDoubleVal(18), + }, }, }), - assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + assertMetricPresent("http_requests_total", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DOUBLE), + compareMetricLabelKeys([]string{"code", "method"}), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareDoubleVal(199), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"200", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareDoubleVal(199), + }, }, { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareDoubleVal(12), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"400", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareDoubleVal(12), + }, }, }), - assertMetricPresent("http_request_duration_seconds", metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, + assertMetricPresent("http_request_duration_seconds", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareHistogram(2600, 5050, []int64{1100, 500, 500, 500}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareHistogram(2600, 5050, []int64{1100, 500, 500, 500}), + }, }, }), - assertMetricPresent("rpc_duration_seconds", metricspb.MetricDescriptor_SUMMARY, + assertMetricPresent("rpc_duration_seconds", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_SUMMARY), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareSummary(1001, 5002, map[float64]float64{1: 1, 90: 6, 99: 8}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareSummary(1001, 5002, map[float64]float64{1: 1, 90: 6, 99: 8}), + }, }, }), } @@ -489,11 +604,9 @@ func verifyTarget1(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri doCompare("scrape2", t, want2, m2, e2) } -// target2 is going to have 5 pages, and there's a newly appeared item from the 2nd page. we are expecting the new -// metric will appears on the 3rd scrape, and it uses the timestamp from the 2nd page as starttime, and value is delta -// from the 2nd page. -// with the 4th page, we are simulating a reset (values smaller than previous), cumulative types shall be skipped at -// this run. with the 5th page, cumulative types will be delta with the 4th one +// target2 is going to have 5 pages, and there's a newly added item on the 2nd page. +// with the 4th page, we are simulating a reset (values smaller than previous), start times should be from +// this run for the 4th and 5th scrapes. var target2Page1 = ` # HELP go_threads Number of OS threads created # TYPE go_threads gauge @@ -568,24 +681,43 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri } e1 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts1, - compare: compareDoubleVal(18), + points: []pointComparator{ + comparePointTimestamp(ts1), + compareDoubleVal(18), + }, }, }), - assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + assertMetricPresent("http_requests_total", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DOUBLE), + compareMetricLabelKeys([]string{"code", "method"}), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareDoubleVal(10), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"200", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareDoubleVal(10), + }, }, { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareDoubleVal(50), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"400", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareDoubleVal(50), + }, }, }), } @@ -601,29 +733,53 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri Resource: td.resource, } e2 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts2, - compare: compareDoubleVal(16), + points: []pointComparator{ + comparePointTimestamp(ts2), + compareDoubleVal(16), + }, }, }), - assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + assertMetricPresent("http_requests_total", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DOUBLE), + compareMetricLabelKeys([]string{"code", "method"}), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareDoubleVal(50), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"200", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareDoubleVal(50), + }, }, { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareDoubleVal(60), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"400", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareDoubleVal(60), + }, }, { - startTimestamp: ts2, - pointTimestamp: ts2, - compare: compareDoubleVal(3), + series: []seriesComparator{ + compareSeriesTimestamp(ts2), + compareSeriesLabelValues([]string{"500", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareDoubleVal(3), + }, }, }), } @@ -641,29 +797,53 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri } e3 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts3, - compare: compareDoubleVal(16), + points: []pointComparator{ + comparePointTimestamp(ts3), + compareDoubleVal(16), + }, }, }), - assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + assertMetricPresent("http_requests_total", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DOUBLE), + compareMetricLabelKeys([]string{"code", "method"}), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts3, - compare: compareDoubleVal(50), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"200", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts3), + compareDoubleVal(50), + }, }, { - startTimestamp: ts1, - pointTimestamp: ts3, - compare: compareDoubleVal(60), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"400", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts3), + compareDoubleVal(60), + }, }, { - startTimestamp: ts2, - pointTimestamp: ts3, - compare: compareDoubleVal(5), + series: []seriesComparator{ + compareSeriesTimestamp(ts2), + compareSeriesLabelValues([]string{"500", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts3), + compareDoubleVal(5), + }, }, }), } @@ -772,29 +952,53 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri } e4 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts4, - compare: compareDoubleVal(16), + points: []pointComparator{ + comparePointTimestamp(ts4), + compareDoubleVal(16), + }, }, }), - assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + assertMetricPresent("http_requests_total", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DOUBLE), + compareMetricLabelKeys([]string{"code", "method"}), + }, []seriesExpectation{ { - startTimestamp: ts4, - pointTimestamp: ts4, - compare: compareDoubleVal(49), + series: []seriesComparator{ + compareSeriesTimestamp(ts4), + compareSeriesLabelValues([]string{"200", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts4), + compareDoubleVal(49), + }, }, { - startTimestamp: ts4, - pointTimestamp: ts4, - compare: compareDoubleVal(59), + series: []seriesComparator{ + compareSeriesTimestamp(ts4), + compareSeriesLabelValues([]string{"400", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts4), + compareDoubleVal(59), + }, }, { - startTimestamp: ts4, - pointTimestamp: ts4, - compare: compareDoubleVal(3), + series: []seriesComparator{ + compareSeriesTimestamp(ts4), + compareSeriesLabelValues([]string{"500", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts4), + compareDoubleVal(3), + }, }, }), } @@ -812,29 +1016,53 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri } e5 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts5, - compare: compareDoubleVal(16), + points: []pointComparator{ + comparePointTimestamp(ts5), + compareDoubleVal(16), + }, }, }), - assertMetricPresent("http_requests_total", metricspb.MetricDescriptor_CUMULATIVE_DOUBLE, + assertMetricPresent("http_requests_total", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DOUBLE), + compareMetricLabelKeys([]string{"code", "method"}), + }, []seriesExpectation{ { - startTimestamp: ts4, - pointTimestamp: ts5, - compare: compareDoubleVal(50), + series: []seriesComparator{ + compareSeriesTimestamp(ts4), + compareSeriesLabelValues([]string{"200", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts5), + compareDoubleVal(50), + }, }, { - startTimestamp: ts4, - pointTimestamp: ts5, - compare: compareDoubleVal(59), + series: []seriesComparator{ + compareSeriesTimestamp(ts4), + compareSeriesLabelValues([]string{"400", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts5), + compareDoubleVal(59), + }, }, { - startTimestamp: ts4, - pointTimestamp: ts5, - compare: compareDoubleVal(5), + series: []seriesComparator{ + compareSeriesTimestamp(ts4), + compareSeriesLabelValues([]string{"500", "post"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts5), + compareDoubleVal(5), + }, }, }), } @@ -929,33 +1157,59 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri } e1 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts1, - compare: compareDoubleVal(18), + points: []pointComparator{ + comparePointTimestamp(ts1), + compareDoubleVal(18), + }, }, }), - assertMetricPresent("http_request_duration_seconds", metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, + assertMetricPresent("http_request_duration_seconds", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareHistogram(13003, 50000, []int64{10000, 1000, 1001, 1002}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareHistogram(13003, 50000, []int64{10000, 1000, 1001, 1002}), + }, }, }), assertMetricAbsent("corrupted_hist"), - assertMetricPresent("rpc_duration_seconds", metricspb.MetricDescriptor_SUMMARY, + assertMetricPresent("rpc_duration_seconds", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_SUMMARY), + compareMetricLabelKeys([]string{"foo"}), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareSummary(900, 8000, map[float64]float64{1: 31, 5: 35, 50: 47, 90: 70, 99: 76}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"bar"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareSummary(900, 8000, map[float64]float64{1: 31, 5: 35, 50: 47, 90: 70, 99: 76}), + }, }, { - startTimestamp: ts1, - pointTimestamp: ts1, - compare: compareSummary(50, 100, map[float64]float64{}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"no_quantile"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts1), + compareSummary(50, 100, map[float64]float64{}), + }, }, }), } @@ -972,33 +1226,59 @@ func verifyTarget3(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri } e2 := []testExpectation{ - assertMetricPresent("go_threads", metricspb.MetricDescriptor_GAUGE_DOUBLE, + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + }, []seriesExpectation{ { - pointTimestamp: ts2, - compare: compareDoubleVal(16), + points: []pointComparator{ + comparePointTimestamp(ts2), + compareDoubleVal(16), + }, }, }), - assertMetricPresent("http_request_duration_seconds", metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION, + assertMetricPresent("http_request_duration_seconds", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareHistogram(14003, 50100, []int64{11000, 1000, 1001, 1002}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareHistogram(14003, 50100, []int64{11000, 1000, 1001, 1002}), + }, }, }), assertMetricAbsent("corrupted_hist"), - assertMetricPresent("rpc_duration_seconds", metricspb.MetricDescriptor_SUMMARY, + assertMetricPresent("rpc_duration_seconds", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_SUMMARY), + compareMetricLabelKeys([]string{"foo"}), + }, []seriesExpectation{ { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareSummary(950, 8100, map[float64]float64{1: 32, 5: 35, 50: 47, 90: 70, 99: 77}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"bar"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareSummary(950, 8100, map[float64]float64{1: 32, 5: 35, 50: 47, 90: 70, 99: 77}), + }, }, { - startTimestamp: ts1, - pointTimestamp: ts2, - compare: compareSummary(55, 101, map[float64]float64{}), + series: []seriesComparator{ + compareSeriesTimestamp(ts1), + compareSeriesLabelValues([]string{"no_quantile"}), + }, + points: []pointComparator{ + comparePointTimestamp(ts2), + compareSummary(55, 101, map[float64]float64{}), + }, }, }), } diff --git a/receiver/prometheusreceiver/metrics_reciever_external_labels_test.go b/receiver/prometheusreceiver/metrics_reciever_external_labels_test.go index 786d135bc36e..7ea8f25aa838 100644 --- a/receiver/prometheusreceiver/metrics_reciever_external_labels_test.go +++ b/receiver/prometheusreceiver/metrics_reciever_external_labels_test.go @@ -109,5 +109,21 @@ func verifyExternalLabels(t *testing.T, td *testData, mds []*agentmetricspb.Expo }, } want.Metrics[0].Timeseries[0].Points[0].Timestamp = mds[0].Metrics[0].Timeseries[0].Points[0].Timestamp - doCompare("scrape-externalLabels", t, want, mds[0]) + doCompare("scrape-externalLabels", t, want, mds[0], []testExpectation{ + assertMetricPresent("go_threads", + []descriptorComparator{ + compareMetricType(metricspb.MetricDescriptor_GAUGE_DOUBLE), + compareMetricLabelKeys([]string{"key"}), + }, + []seriesExpectation{ + { + series: []seriesComparator{ + compareSeriesLabelValues([]string{"value"}), + }, + points: []pointComparator{ + compareDoubleVal(19), + }, + }, + }), + }) } From 651d07ac1c6bb7c2e77af666ce6612f4721bbcbd Mon Sep 17 00:00:00 2001 From: Anthony J Mirabella Date: Wed, 26 May 2021 16:05:37 -0400 Subject: [PATCH 12/22] receiver/prometheus: remove unused test metric spec Signed-off-by: Anthony J Mirabella --- .../metrics_reciever_external_labels_test.go | 22 +------------------ 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/receiver/prometheusreceiver/metrics_reciever_external_labels_test.go b/receiver/prometheusreceiver/metrics_reciever_external_labels_test.go index 7ea8f25aa838..658913de0b37 100644 --- a/receiver/prometheusreceiver/metrics_reciever_external_labels_test.go +++ b/receiver/prometheusreceiver/metrics_reciever_external_labels_test.go @@ -87,28 +87,7 @@ func verifyExternalLabels(t *testing.T, td *testData, mds []*agentmetricspb.Expo want := &agentmetricspb.ExportMetricsServiceRequest{ Node: td.node, Resource: td.resource, - Metrics: []*metricspb.Metric{ - { - MetricDescriptor: &metricspb.MetricDescriptor{ - Name: "go_threads", - Description: "Number of OS threads created", - Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, - LabelKeys: []*metricspb.LabelKey{{Key: "key"}}, - }, - Timeseries: []*metricspb.TimeSeries{ - { - Points: []*metricspb.Point{ - {Value: &metricspb.Point_DoubleValue{DoubleValue: 19.0}}, - }, - LabelValues: []*metricspb.LabelValue{ - {Value: "value", HasValue: true}, - }, - }, - }, - }, - }, } - want.Metrics[0].Timeseries[0].Points[0].Timestamp = mds[0].Metrics[0].Timeseries[0].Points[0].Timestamp doCompare("scrape-externalLabels", t, want, mds[0], []testExpectation{ assertMetricPresent("go_threads", []descriptorComparator{ @@ -121,6 +100,7 @@ func verifyExternalLabels(t *testing.T, td *testData, mds []*agentmetricspb.Expo compareSeriesLabelValues([]string{"value"}), }, points: []pointComparator{ + comparePointTimestamp(mds[0].Metrics[0].Timeseries[0].Points[0].Timestamp), compareDoubleVal(19), }, }, From fb3faf1f5e0dfff13239a525ae25995b02531a0f Mon Sep 17 00:00:00 2001 From: Guillaume GILL Date: Sun, 30 May 2021 22:55:23 +0200 Subject: [PATCH 13/22] Add useful logs --- .../prometheusreceiver/internal/metricfamily.go | 14 ++++++++------ .../prometheusreceiver/internal/metricsbuilder.go | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/receiver/prometheusreceiver/internal/metricfamily.go b/receiver/prometheusreceiver/internal/metricfamily.go index ebdc6bc500ad..23f304d1da45 100644 --- a/receiver/prometheusreceiver/internal/metricfamily.go +++ b/receiver/prometheusreceiver/internal/metricfamily.go @@ -15,6 +15,7 @@ package internal import ( + "fmt" "sort" "strings" @@ -22,6 +23,7 @@ import ( "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/textparse" "github.com/prometheus/prometheus/scrape" + "go.uber.org/zap" "google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/wrapperspb" ) @@ -46,7 +48,7 @@ type metricFamily struct { groups map[string]*metricGroup } -func newMetricFamily(metricName string, mc MetadataCache) MetricFamily { +func newMetricFamily(metricName string, mc MetadataCache, logger *zap.Logger) MetricFamily { familyName := normalizeMetricName(metricName) // lookup metadata based on familyName @@ -63,12 +65,12 @@ func newMetricFamily(metricName string, mc MetadataCache) MetricFamily { metadata.Type = textparse.MetricTypeUnknown } } else if !ok && isInternalMetric(metricName) { - metadata = defineInternalMetric(metricName, metadata) + metadata = defineInternalMetric(metricName, metadata, logger) } //TODO convert it to OtelMetrics ? ocaMetricType := convToOCAMetricType(metadata.Type) if ocaMetricType == metricspb.MetricDescriptor_UNSPECIFIED { - //b.logger.Debug(fmt.Sprintf("Invalid metric : %s %+v", metricName, metadata)) + logger.Debug(fmt.Sprintf("Invalid metric : %s %+v", metricName, metadata)) } return &metricFamily{ @@ -85,9 +87,9 @@ func newMetricFamily(metricName string, mc MetadataCache) MetricFamily { } // Define manualy the metadata of prometheus scrapper internal metrics -func defineInternalMetric(metricName string, metadata scrape.MetricMetadata) scrape.MetricMetadata { +func defineInternalMetric(metricName string, metadata scrape.MetricMetadata, logger *zap.Logger) scrape.MetricMetadata { if metadata.Metric != "" && metadata.Type != "" && metadata.Unit != "" && metadata.Help != "" { - //b.logger.Debug("Internal metric seems already fully defined") + logger.Debug("Internal metric seems already fully defined") return metadata } metadata.Metric = metricName @@ -114,7 +116,7 @@ func defineInternalMetric(metricName string, metadata scrape.MetricMetadata) scr metadata.Type = textparse.MetricTypeGauge metadata.Help = "The number of samples remaining after metric relabeling was applied" } - //b.logger.Info("Internal metric defined", zap.String("metadata", fmt.Sprintf("%+v", metadata))) + logger.Info("Internal metric defined", zap.String("metadata", fmt.Sprintf("%+v", metadata))) return metadata } diff --git a/receiver/prometheusreceiver/internal/metricsbuilder.go b/receiver/prometheusreceiver/internal/metricsbuilder.go index 3268a5a4ed4a..7dfef42b6914 100644 --- a/receiver/prometheusreceiver/internal/metricsbuilder.go +++ b/receiver/prometheusreceiver/internal/metricsbuilder.go @@ -141,9 +141,9 @@ func (b *metricBuilder) AddDataPoint(ls labels.Labels, t int64, v float64) error if m != nil { b.metrics = append(b.metrics, m) } - b.currentMf = newMetricFamily(metricName, b.mc) + b.currentMf = newMetricFamily(metricName, b.mc, b.logger) } else if b.currentMf == nil { - b.currentMf = newMetricFamily(metricName, b.mc) + b.currentMf = newMetricFamily(metricName, b.mc, b.logger) } return b.currentMf.Add(metricName, ls, t, v) From 97bf229a78f28831726f403c4814fd01ec23ae0b Mon Sep 17 00:00:00 2001 From: Guillaume GILL Date: Wed, 2 Jun 2021 23:31:42 +0200 Subject: [PATCH 14/22] Fix useless units and comments --- receiver/prometheusreceiver/internal/metricfamily.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/receiver/prometheusreceiver/internal/metricfamily.go b/receiver/prometheusreceiver/internal/metricfamily.go index 23f304d1da45..59e42d3590e1 100644 --- a/receiver/prometheusreceiver/internal/metricfamily.go +++ b/receiver/prometheusreceiver/internal/metricfamily.go @@ -67,7 +67,6 @@ func newMetricFamily(metricName string, mc MetadataCache, logger *zap.Logger) Me } else if !ok && isInternalMetric(metricName) { metadata = defineInternalMetric(metricName, metadata, logger) } - //TODO convert it to OtelMetrics ? ocaMetricType := convToOCAMetricType(metadata.Type) if ocaMetricType == metricspb.MetricDescriptor_UNSPECIFIED { logger.Debug(fmt.Sprintf("Invalid metric : %s %+v", metricName, metadata)) @@ -96,7 +95,6 @@ func defineInternalMetric(metricName string, metadata scrape.MetricMetadata, log switch metricName { case scrapeUpMetricName: - metadata.Unit = "bool" metadata.Type = textparse.MetricTypeGauge metadata.Help = "The scraping was sucessful" case "scrape_duration_seconds": @@ -104,15 +102,12 @@ func defineInternalMetric(metricName string, metadata scrape.MetricMetadata, log metadata.Type = textparse.MetricTypeGauge metadata.Help = "Duration of the scrape" case "scrape_samples_scraped": - metadata.Unit = "count" metadata.Type = textparse.MetricTypeGauge metadata.Help = "The number of samples the target exposed" case "scrape_series_added": - metadata.Unit = "count" metadata.Type = textparse.MetricTypeGauge metadata.Help = "The approximate number of new series in this scrape" case "scrape_samples_post_metric_relabeling": - metadata.Unit = "count" metadata.Type = textparse.MetricTypeGauge metadata.Help = "The number of samples remaining after metric relabeling was applied" } From 408c80c549b72ce16e62ac3f584267af0d27e07a Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Mon, 7 Jun 2021 14:21:52 +0200 Subject: [PATCH 15/22] Fix too verbose log and bad test --- receiver/prometheusreceiver/internal/metricfamily.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/receiver/prometheusreceiver/internal/metricfamily.go b/receiver/prometheusreceiver/internal/metricfamily.go index 59e42d3590e1..61136e3fee64 100644 --- a/receiver/prometheusreceiver/internal/metricfamily.go +++ b/receiver/prometheusreceiver/internal/metricfamily.go @@ -87,7 +87,7 @@ func newMetricFamily(metricName string, mc MetadataCache, logger *zap.Logger) Me // Define manualy the metadata of prometheus scrapper internal metrics func defineInternalMetric(metricName string, metadata scrape.MetricMetadata, logger *zap.Logger) scrape.MetricMetadata { - if metadata.Metric != "" && metadata.Type != "" && metadata.Unit != "" && metadata.Help != "" { + if metadata.Metric != "" && metadata.Type != "" && metadata.Help != "" { logger.Debug("Internal metric seems already fully defined") return metadata } @@ -111,7 +111,6 @@ func defineInternalMetric(metricName string, metadata scrape.MetricMetadata, log metadata.Type = textparse.MetricTypeGauge metadata.Help = "The number of samples remaining after metric relabeling was applied" } - logger.Info("Internal metric defined", zap.String("metadata", fmt.Sprintf("%+v", metadata))) return metadata } From 701fdbb559f3906b9b927a815bccd4979c99b570 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Tue, 8 Jun 2021 09:51:16 +0200 Subject: [PATCH 16/22] Fix prom exporter test if scrap duration is instant --- exporter/prometheusexporter/end_to_end_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/prometheusexporter/end_to_end_test.go b/exporter/prometheusexporter/end_to_end_test.go index 48efe977819e..b99c8ece3021 100644 --- a/exporter/prometheusexporter/end_to_end_test.go +++ b/exporter/prometheusexporter/end_to_end_test.go @@ -154,7 +154,7 @@ func TestEndToEndSummarySupport(t *testing.T) { `test_jvm_memory_pool_bytes_used.pool="Metaspace". 2.6218176e.07.*`, `. HELP test_scrape_duration_seconds Duration of the scrape`, `. TYPE test_scrape_duration_seconds gauge`, - `test_scrape_duration_seconds 0.0.*`, + `test_scrape_duration_seconds 0.* .*` `. HELP test_scrape_samples_post_metric_relabeling The number of samples remaining after metric relabeling was applied`, `. TYPE test_scrape_samples_post_metric_relabeling gauge`, `test_scrape_samples_post_metric_relabeling 13 .*`, From 1018140f3d99bf34d2e9c9098964058b77c979bc Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Tue, 8 Jun 2021 09:57:43 +0200 Subject: [PATCH 17/22] Fix lint --- exporter/prometheusexporter/end_to_end_test.go | 2 +- receiver/prometheusreceiver/internal/metricfamily.go | 4 ++-- receiver/prometheusreceiver/metrics_receiver_test.go | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/exporter/prometheusexporter/end_to_end_test.go b/exporter/prometheusexporter/end_to_end_test.go index b99c8ece3021..71dcc09f8ac7 100644 --- a/exporter/prometheusexporter/end_to_end_test.go +++ b/exporter/prometheusexporter/end_to_end_test.go @@ -164,7 +164,7 @@ func TestEndToEndSummarySupport(t *testing.T) { `. HELP test_scrape_series_added The approximate number of new series in this scrape`, `. TYPE test_scrape_series_added gauge`, `test_scrape_series_added 13 .*`, - `. HELP test_up The scraping was sucessful`, + `. HELP test_up The scraping was successful`, `. TYPE test_up gauge`, `test_up 1 .*`, } diff --git a/receiver/prometheusreceiver/internal/metricfamily.go b/receiver/prometheusreceiver/internal/metricfamily.go index 61136e3fee64..e8f200a46f9c 100644 --- a/receiver/prometheusreceiver/internal/metricfamily.go +++ b/receiver/prometheusreceiver/internal/metricfamily.go @@ -85,7 +85,7 @@ func newMetricFamily(metricName string, mc MetadataCache, logger *zap.Logger) Me } } -// Define manualy the metadata of prometheus scrapper internal metrics +// Define manually the metadata of prometheus scrapper internal metrics func defineInternalMetric(metricName string, metadata scrape.MetricMetadata, logger *zap.Logger) scrape.MetricMetadata { if metadata.Metric != "" && metadata.Type != "" && metadata.Help != "" { logger.Debug("Internal metric seems already fully defined") @@ -96,7 +96,7 @@ func defineInternalMetric(metricName string, metadata scrape.MetricMetadata, log switch metricName { case scrapeUpMetricName: metadata.Type = textparse.MetricTypeGauge - metadata.Help = "The scraping was sucessful" + metadata.Help = "The scraping was successful" case "scrape_duration_seconds": metadata.Unit = "seconds" metadata.Type = textparse.MetricTypeGauge diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index 4c54b82ce5a1..c8d202d1b19b 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -187,8 +187,8 @@ func verifyNumScrapeResults(t *testing.T, td *testData, mds []*agentmetricspb.Ex func doCompare(name string, t *testing.T, want, got *agentmetricspb.ExportMetricsServiceRequest, expectations []testExpectation) { t.Run(name, func(t *testing.T) { - num_scrape_metrics := count_scrape_metrics(got) - assert.Equal(t, expectedScrapeMetricCount, num_scrape_metrics) + numScrapeMetrics := countScrapeMetrics(got) + assert.Equal(t, expectedScrapeMetricCount, numScrapeMetrics) assert.EqualValues(t, want.Node, got.Node) assert.EqualValues(t, want.Resource, got.Resource) for _, e := range expectations { @@ -201,7 +201,7 @@ func getValidScrapes(t *testing.T, mds []*agentmetricspb.ExportMetricsServiceReq out := make([]*agentmetricspb.ExportMetricsServiceRequest, 0) for _, md := range mds { // mds will include scrapes that received no metrics but have internal scrape metrics, filter those out - if expectedScrapeMetricCount < len(md.Metrics) && count_scrape_metrics(md) == expectedScrapeMetricCount { + if expectedScrapeMetricCount < len(md.Metrics) && countScrapeMetrics(md) == expectedScrapeMetricCount { assertUp(t, 1, md) out = append(out, md) } else { @@ -221,12 +221,12 @@ func assertUp(t *testing.T, expected float64, md *agentmetricspb.ExportMetricsSe t.Error("No 'up' metric found") } -func count_scrape_metrics(in *agentmetricspb.ExportMetricsServiceRequest) int { +func countScrapeMetrics(in *agentmetricspb.ExportMetricsServiceRequest) int { n := 0 for _, m := range in.Metrics { switch m.MetricDescriptor.Name { case "up", "scrape_duration_seconds", "scrape_samples_scraped", "scrape_samples_post_metric_relabeling", "scrape_series_added": - n += 1 + n++ default: } } From 789737d1e66e95ccad881bae49c73b520bbb4376 Mon Sep 17 00:00:00 2001 From: Guillaume GILL Date: Tue, 8 Jun 2021 19:58:48 +0200 Subject: [PATCH 18/22] Update exporter/prometheusexporter/end_to_end_test.go Co-authored-by: Anthony Mirabella --- exporter/prometheusexporter/end_to_end_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exporter/prometheusexporter/end_to_end_test.go b/exporter/prometheusexporter/end_to_end_test.go index 71dcc09f8ac7..5ab744f98c57 100644 --- a/exporter/prometheusexporter/end_to_end_test.go +++ b/exporter/prometheusexporter/end_to_end_test.go @@ -154,7 +154,7 @@ func TestEndToEndSummarySupport(t *testing.T) { `test_jvm_memory_pool_bytes_used.pool="Metaspace". 2.6218176e.07.*`, `. HELP test_scrape_duration_seconds Duration of the scrape`, `. TYPE test_scrape_duration_seconds gauge`, - `test_scrape_duration_seconds 0.* .*` + `test_scrape_duration_seconds 0.* .*`, `. HELP test_scrape_samples_post_metric_relabeling The number of samples remaining after metric relabeling was applied`, `. TYPE test_scrape_samples_post_metric_relabeling gauge`, `test_scrape_samples_post_metric_relabeling 13 .*`, From 1e56159529d01069d282ea2ef57c9dea929bf896 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 10 Jun 2021 14:22:54 +0200 Subject: [PATCH 19/22] Try to fix exporter test --- .../prometheusexporter/end_to_end_test.go | 2 +- .../end_to_end_test.go.orig | 213 ++++++++++++++++++ 2 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 exporter/prometheusexporter/end_to_end_test.go.orig diff --git a/exporter/prometheusexporter/end_to_end_test.go b/exporter/prometheusexporter/end_to_end_test.go index 5ab744f98c57..8b1b8b116610 100644 --- a/exporter/prometheusexporter/end_to_end_test.go +++ b/exporter/prometheusexporter/end_to_end_test.go @@ -154,7 +154,7 @@ func TestEndToEndSummarySupport(t *testing.T) { `test_jvm_memory_pool_bytes_used.pool="Metaspace". 2.6218176e.07.*`, `. HELP test_scrape_duration_seconds Duration of the scrape`, `. TYPE test_scrape_duration_seconds gauge`, - `test_scrape_duration_seconds 0.* .*`, + `test_scrape_duration_seconds [0-9.e-]+ [0-9]+`, `. HELP test_scrape_samples_post_metric_relabeling The number of samples remaining after metric relabeling was applied`, `. TYPE test_scrape_samples_post_metric_relabeling gauge`, `test_scrape_samples_post_metric_relabeling 13 .*`, diff --git a/exporter/prometheusexporter/end_to_end_test.go.orig b/exporter/prometheusexporter/end_to_end_test.go.orig new file mode 100644 index 000000000000..cfa4172d35a7 --- /dev/null +++ b/exporter/prometheusexporter/end_to_end_test.go.orig @@ -0,0 +1,213 @@ +// Copyright The OpenTelemetry 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 prometheusexporter + +import ( + "bytes" + "context" + "fmt" + "io/ioutil" + "net/http" + "net/http/httptest" + "net/url" + "regexp" + "testing" + "time" + + promconfig "github.com/prometheus/prometheus/config" + "github.com/stretchr/testify/require" + "go.uber.org/zap" + "gopkg.in/yaml.v2" + + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/receiver/prometheusreceiver" +) + +func TestEndToEndSummarySupport(t *testing.T) { + if testing.Short() { + t.Skip("This test can take a couple of seconds") + } + + // 1. Create the Prometheus scrape endpoint. + waitForScrape := make(chan bool, 1) + shutdown := make(chan bool, 1) + dropWizardServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + select { + case <-shutdown: + return + case waitForScrape <- true: + // Serve back the metrics as if they were from DropWizard. + _, err := rw.Write([]byte(dropWizardResponse)) + require.NoError(t, err) + } + })) + defer dropWizardServer.Close() + defer close(shutdown) + + srvURL, err := url.Parse(dropWizardServer.URL) + if err != nil { + t.Fatal(err) + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + // 2. Create the Prometheus metrics exporter that'll receive and verify the metrics produced. + exporterCfg := &Config{ + ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)), + Namespace: "test", + Endpoint: ":8787", + SendTimestamps: true, + MetricExpiration: 2 * time.Hour, + } + exporterFactory := NewFactory() + creationParams := component.ExporterCreateParams{Logger: zap.NewNop()} + exporter, err := exporterFactory.CreateMetricsExporter(ctx, creationParams, exporterCfg) + if err != nil { + t.Fatal(err) + } + if err = exporter.Start(ctx, nil); err != nil { + t.Fatalf("Failed to start the Prometheus receiver: %v", err) + } + t.Cleanup(func() { require.NoError(t, exporter.Shutdown(ctx)) }) + + // 3. Create the Prometheus receiver scraping from the DropWizard mock server and + // it'll feed scraped and converted metrics then pass them to the Prometheus exporter. + yamlConfig := []byte(fmt.Sprintf(` + global: + scrape_interval: 2ms + + scrape_configs: + - job_name: 'otel-collector' + scrape_interval: 2ms + static_configs: + - targets: ['%s'] + `, srvURL.Host)) + receiverConfig := new(promconfig.Config) + if err = yaml.Unmarshal(yamlConfig, receiverConfig); err != nil { + t.Fatal(err) + } + + receiverFactory := prometheusreceiver.NewFactory() + receiverCreateParams := component.ReceiverCreateParams{ + Logger: zap.NewNop(), + } + rcvCfg := &prometheusreceiver.Config{ + PrometheusConfig: receiverConfig, + ReceiverSettings: config.NewReceiverSettings(config.NewID("prometheus")), + } + // 3.5 Create the Prometheus receiver and pass in the preivously created Prometheus exporter. + prometheusReceiver, err := receiverFactory.CreateMetricsReceiver(ctx, receiverCreateParams, rcvCfg, exporter) + if err != nil { + t.Fatal(err) + } + if err = prometheusReceiver.Start(ctx, nil); err != nil { + t.Fatalf("Failed to start the Prometheus receiver: %v", err) + } + t.Cleanup(func() { require.NoError(t, prometheusReceiver.Shutdown(ctx)) }) + + // 4. Scrape from the Prometheus exporter to ensure that we export summary metrics + // We shall let the Prometheus exporter scrape the DropWizard mock server, at least 9 times. + for i := 0; i < 8; i++ { + <-waitForScrape + } + res, err := http.Get("http://localhost" + exporterCfg.Endpoint + "/metrics") + if err != nil { + t.Fatalf("Failed to scrape from the exporter: %v", err) + } + prometheusExporterScrape, err := ioutil.ReadAll(res.Body) + res.Body.Close() + if err != nil { + t.Fatal(err) + } + + // 5. Verify that we have the summary metrics and that their values make sense. + wantLineRegexps := []string{ + `. HELP test_jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.`, + `. TYPE test_jvm_gc_collection_seconds summary`, + `test_jvm_gc_collection_seconds_sum.gc="G1 Old Generation". 0.*`, + `test_jvm_gc_collection_seconds_count.gc="G1 Old Generation". 0.*`, + `test_jvm_gc_collection_seconds_sum.gc="G1 Young Generation". 0.*`, + `test_jvm_gc_collection_seconds_count.gc="G1 Young Generation". 9.*`, + `. HELP test_jvm_info JVM version info`, + `. TYPE test_jvm_info gauge`, + `test_jvm_info.vendor="Oracle Corporation",version="9.0.4.11". 1.*`, + `. HELP test_jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool.`, + `. TYPE test_jvm_memory_pool_bytes_used gauge`, + `test_jvm_memory_pool_bytes_used.pool="CodeHeap 'non.nmethods'". 1.277952e.06.*`, + `test_jvm_memory_pool_bytes_used.pool="CodeHeap 'non.profiled nmethods'". 2.869376e.06.*`, + `test_jvm_memory_pool_bytes_used.pool="CodeHeap 'profiled nmethods'". 6.871168e.06.*`, + `test_jvm_memory_pool_bytes_used.pool="Compressed Class Space". 2.751312e.06.*`, + `test_jvm_memory_pool_bytes_used.pool="G1 Eden Space". 4.4040192e.07.*`, + `test_jvm_memory_pool_bytes_used.pool="G1 Old Gen". 4.385408e.06.*`, + `test_jvm_memory_pool_bytes_used.pool="G1 Survivor Space". 8.388608e.06.*`, + `test_jvm_memory_pool_bytes_used.pool="Metaspace". 2.6218176e.07.*`, + `. HELP test_scrape_duration_seconds Duration of the scrape`, + `. TYPE test_scrape_duration_seconds gauge`, +<<<<<<< Updated upstream + `test_scrape_duration_seconds 0.* .*`, +======= + `test_scrape_duration_seconds [0-9.e-]* [0-9]+`, +>>>>>>> Stashed changes + `. HELP test_scrape_samples_post_metric_relabeling The number of samples remaining after metric relabeling was applied`, + `. TYPE test_scrape_samples_post_metric_relabeling gauge`, + `test_scrape_samples_post_metric_relabeling 13 .*`, + `. HELP test_scrape_samples_scraped The number of samples the target exposed`, + `. TYPE test_scrape_samples_scraped gauge`, + `test_scrape_samples_scraped 13 .*`, + `. HELP test_scrape_series_added The approximate number of new series in this scrape`, + `. TYPE test_scrape_series_added gauge`, + `test_scrape_series_added 13 .*`, + `. HELP test_up The scraping was successful`, + `. TYPE test_up gauge`, + `test_up 1 .*`, + } + + // 5.5: Perform a complete line by line prefix verification to ensure we extract back the inputs + // we'd expect after scraping Prometheus. + for _, wantLineRegexp := range wantLineRegexps { + reg := regexp.MustCompile(wantLineRegexp) + prometheusExporterScrape = reg.ReplaceAll(prometheusExporterScrape, []byte("")) + } + // After this replacement, there should ONLY be newlines present. + prometheusExporterScrape = bytes.ReplaceAll(prometheusExporterScrape, []byte("\n"), []byte("")) + // Now assert that NO output was left over. + if len(prometheusExporterScrape) != 0 { + t.Fatalf("Left-over unmatched Prometheus scrape content: %q\n", prometheusExporterScrape) + } + +} + +const dropWizardResponse = ` +# HELP jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool. +# TYPE jvm_memory_pool_bytes_used gauge +jvm_memory_pool_bytes_used{pool="CodeHeap 'non-nmethods'",} 1277952.0 +jvm_memory_pool_bytes_used{pool="Metaspace",} 2.6218176E7 +jvm_memory_pool_bytes_used{pool="CodeHeap 'profiled nmethods'",} 6871168.0 +jvm_memory_pool_bytes_used{pool="Compressed Class Space",} 2751312.0 +jvm_memory_pool_bytes_used{pool="G1 Eden Space",} 4.4040192E7 +jvm_memory_pool_bytes_used{pool="G1 Old Gen",} 4385408.0 +jvm_memory_pool_bytes_used{pool="G1 Survivor Space",} 8388608.0 +jvm_memory_pool_bytes_used{pool="CodeHeap 'non-profiled nmethods'",} 2869376.0 +# HELP jvm_info JVM version info +# TYPE jvm_info gauge +jvm_info{version="9.0.4+11",vendor="Oracle Corporation",} 1.0 +# HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds. +# TYPE jvm_gc_collection_seconds summary +jvm_gc_collection_seconds_count{gc="G1 Young Generation",} 9.0 +jvm_gc_collection_seconds_sum{gc="G1 Young Generation",} 0.229 +jvm_gc_collection_seconds_count{gc="G1 Old Generation",} 0.0 +jvm_gc_collection_seconds_sum{gc="G1 Old Generation",} 0.0` From d979b1f40d823e2ac062c5c3ad158a68cc5d2633 Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Thu, 10 Jun 2021 14:23:39 +0200 Subject: [PATCH 20/22] Remove unwanted file --- .../end_to_end_test.go.orig | 213 ------------------ 1 file changed, 213 deletions(-) delete mode 100644 exporter/prometheusexporter/end_to_end_test.go.orig diff --git a/exporter/prometheusexporter/end_to_end_test.go.orig b/exporter/prometheusexporter/end_to_end_test.go.orig deleted file mode 100644 index cfa4172d35a7..000000000000 --- a/exporter/prometheusexporter/end_to_end_test.go.orig +++ /dev/null @@ -1,213 +0,0 @@ -// Copyright The OpenTelemetry 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 prometheusexporter - -import ( - "bytes" - "context" - "fmt" - "io/ioutil" - "net/http" - "net/http/httptest" - "net/url" - "regexp" - "testing" - "time" - - promconfig "github.com/prometheus/prometheus/config" - "github.com/stretchr/testify/require" - "go.uber.org/zap" - "gopkg.in/yaml.v2" - - "go.opentelemetry.io/collector/component" - "go.opentelemetry.io/collector/config" - "go.opentelemetry.io/collector/receiver/prometheusreceiver" -) - -func TestEndToEndSummarySupport(t *testing.T) { - if testing.Short() { - t.Skip("This test can take a couple of seconds") - } - - // 1. Create the Prometheus scrape endpoint. - waitForScrape := make(chan bool, 1) - shutdown := make(chan bool, 1) - dropWizardServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { - select { - case <-shutdown: - return - case waitForScrape <- true: - // Serve back the metrics as if they were from DropWizard. - _, err := rw.Write([]byte(dropWizardResponse)) - require.NoError(t, err) - } - })) - defer dropWizardServer.Close() - defer close(shutdown) - - srvURL, err := url.Parse(dropWizardServer.URL) - if err != nil { - t.Fatal(err) - } - - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - // 2. Create the Prometheus metrics exporter that'll receive and verify the metrics produced. - exporterCfg := &Config{ - ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)), - Namespace: "test", - Endpoint: ":8787", - SendTimestamps: true, - MetricExpiration: 2 * time.Hour, - } - exporterFactory := NewFactory() - creationParams := component.ExporterCreateParams{Logger: zap.NewNop()} - exporter, err := exporterFactory.CreateMetricsExporter(ctx, creationParams, exporterCfg) - if err != nil { - t.Fatal(err) - } - if err = exporter.Start(ctx, nil); err != nil { - t.Fatalf("Failed to start the Prometheus receiver: %v", err) - } - t.Cleanup(func() { require.NoError(t, exporter.Shutdown(ctx)) }) - - // 3. Create the Prometheus receiver scraping from the DropWizard mock server and - // it'll feed scraped and converted metrics then pass them to the Prometheus exporter. - yamlConfig := []byte(fmt.Sprintf(` - global: - scrape_interval: 2ms - - scrape_configs: - - job_name: 'otel-collector' - scrape_interval: 2ms - static_configs: - - targets: ['%s'] - `, srvURL.Host)) - receiverConfig := new(promconfig.Config) - if err = yaml.Unmarshal(yamlConfig, receiverConfig); err != nil { - t.Fatal(err) - } - - receiverFactory := prometheusreceiver.NewFactory() - receiverCreateParams := component.ReceiverCreateParams{ - Logger: zap.NewNop(), - } - rcvCfg := &prometheusreceiver.Config{ - PrometheusConfig: receiverConfig, - ReceiverSettings: config.NewReceiverSettings(config.NewID("prometheus")), - } - // 3.5 Create the Prometheus receiver and pass in the preivously created Prometheus exporter. - prometheusReceiver, err := receiverFactory.CreateMetricsReceiver(ctx, receiverCreateParams, rcvCfg, exporter) - if err != nil { - t.Fatal(err) - } - if err = prometheusReceiver.Start(ctx, nil); err != nil { - t.Fatalf("Failed to start the Prometheus receiver: %v", err) - } - t.Cleanup(func() { require.NoError(t, prometheusReceiver.Shutdown(ctx)) }) - - // 4. Scrape from the Prometheus exporter to ensure that we export summary metrics - // We shall let the Prometheus exporter scrape the DropWizard mock server, at least 9 times. - for i := 0; i < 8; i++ { - <-waitForScrape - } - res, err := http.Get("http://localhost" + exporterCfg.Endpoint + "/metrics") - if err != nil { - t.Fatalf("Failed to scrape from the exporter: %v", err) - } - prometheusExporterScrape, err := ioutil.ReadAll(res.Body) - res.Body.Close() - if err != nil { - t.Fatal(err) - } - - // 5. Verify that we have the summary metrics and that their values make sense. - wantLineRegexps := []string{ - `. HELP test_jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.`, - `. TYPE test_jvm_gc_collection_seconds summary`, - `test_jvm_gc_collection_seconds_sum.gc="G1 Old Generation". 0.*`, - `test_jvm_gc_collection_seconds_count.gc="G1 Old Generation". 0.*`, - `test_jvm_gc_collection_seconds_sum.gc="G1 Young Generation". 0.*`, - `test_jvm_gc_collection_seconds_count.gc="G1 Young Generation". 9.*`, - `. HELP test_jvm_info JVM version info`, - `. TYPE test_jvm_info gauge`, - `test_jvm_info.vendor="Oracle Corporation",version="9.0.4.11". 1.*`, - `. HELP test_jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool.`, - `. TYPE test_jvm_memory_pool_bytes_used gauge`, - `test_jvm_memory_pool_bytes_used.pool="CodeHeap 'non.nmethods'". 1.277952e.06.*`, - `test_jvm_memory_pool_bytes_used.pool="CodeHeap 'non.profiled nmethods'". 2.869376e.06.*`, - `test_jvm_memory_pool_bytes_used.pool="CodeHeap 'profiled nmethods'". 6.871168e.06.*`, - `test_jvm_memory_pool_bytes_used.pool="Compressed Class Space". 2.751312e.06.*`, - `test_jvm_memory_pool_bytes_used.pool="G1 Eden Space". 4.4040192e.07.*`, - `test_jvm_memory_pool_bytes_used.pool="G1 Old Gen". 4.385408e.06.*`, - `test_jvm_memory_pool_bytes_used.pool="G1 Survivor Space". 8.388608e.06.*`, - `test_jvm_memory_pool_bytes_used.pool="Metaspace". 2.6218176e.07.*`, - `. HELP test_scrape_duration_seconds Duration of the scrape`, - `. TYPE test_scrape_duration_seconds gauge`, -<<<<<<< Updated upstream - `test_scrape_duration_seconds 0.* .*`, -======= - `test_scrape_duration_seconds [0-9.e-]* [0-9]+`, ->>>>>>> Stashed changes - `. HELP test_scrape_samples_post_metric_relabeling The number of samples remaining after metric relabeling was applied`, - `. TYPE test_scrape_samples_post_metric_relabeling gauge`, - `test_scrape_samples_post_metric_relabeling 13 .*`, - `. HELP test_scrape_samples_scraped The number of samples the target exposed`, - `. TYPE test_scrape_samples_scraped gauge`, - `test_scrape_samples_scraped 13 .*`, - `. HELP test_scrape_series_added The approximate number of new series in this scrape`, - `. TYPE test_scrape_series_added gauge`, - `test_scrape_series_added 13 .*`, - `. HELP test_up The scraping was successful`, - `. TYPE test_up gauge`, - `test_up 1 .*`, - } - - // 5.5: Perform a complete line by line prefix verification to ensure we extract back the inputs - // we'd expect after scraping Prometheus. - for _, wantLineRegexp := range wantLineRegexps { - reg := regexp.MustCompile(wantLineRegexp) - prometheusExporterScrape = reg.ReplaceAll(prometheusExporterScrape, []byte("")) - } - // After this replacement, there should ONLY be newlines present. - prometheusExporterScrape = bytes.ReplaceAll(prometheusExporterScrape, []byte("\n"), []byte("")) - // Now assert that NO output was left over. - if len(prometheusExporterScrape) != 0 { - t.Fatalf("Left-over unmatched Prometheus scrape content: %q\n", prometheusExporterScrape) - } - -} - -const dropWizardResponse = ` -# HELP jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool. -# TYPE jvm_memory_pool_bytes_used gauge -jvm_memory_pool_bytes_used{pool="CodeHeap 'non-nmethods'",} 1277952.0 -jvm_memory_pool_bytes_used{pool="Metaspace",} 2.6218176E7 -jvm_memory_pool_bytes_used{pool="CodeHeap 'profiled nmethods'",} 6871168.0 -jvm_memory_pool_bytes_used{pool="Compressed Class Space",} 2751312.0 -jvm_memory_pool_bytes_used{pool="G1 Eden Space",} 4.4040192E7 -jvm_memory_pool_bytes_used{pool="G1 Old Gen",} 4385408.0 -jvm_memory_pool_bytes_used{pool="G1 Survivor Space",} 8388608.0 -jvm_memory_pool_bytes_used{pool="CodeHeap 'non-profiled nmethods'",} 2869376.0 -# HELP jvm_info JVM version info -# TYPE jvm_info gauge -jvm_info{version="9.0.4+11",vendor="Oracle Corporation",} 1.0 -# HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds. -# TYPE jvm_gc_collection_seconds summary -jvm_gc_collection_seconds_count{gc="G1 Young Generation",} 9.0 -jvm_gc_collection_seconds_sum{gc="G1 Young Generation",} 0.229 -jvm_gc_collection_seconds_count{gc="G1 Old Generation",} 0.0 -jvm_gc_collection_seconds_sum{gc="G1 Old Generation",} 0.0` From e2c4d6d952ca1a020d1f50cfae34e171518b77cb Mon Sep 17 00:00:00 2001 From: David Ashpole Date: Tue, 15 Jun 2021 09:13:43 -0700 Subject: [PATCH 21/22] fix tests after rebasing --- receiver/prometheusreceiver/internal/otlp_metricfamily_test.go | 3 ++- receiver/prometheusreceiver/metrics_receiver_test.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/receiver/prometheusreceiver/internal/otlp_metricfamily_test.go b/receiver/prometheusreceiver/internal/otlp_metricfamily_test.go index 6f8cc98136ed..3e394f90f201 100644 --- a/receiver/prometheusreceiver/internal/otlp_metricfamily_test.go +++ b/receiver/prometheusreceiver/internal/otlp_metricfamily_test.go @@ -21,6 +21,7 @@ import ( "github.com/prometheus/prometheus/pkg/textparse" "github.com/prometheus/prometheus/scrape" "github.com/stretchr/testify/assert" + "go.uber.org/zap" ) type byLookupMetadataCache map[string]scrape.MetricMetadata @@ -89,7 +90,7 @@ func TestIsCumulativeEquivalence(t *testing.T) { for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - mf := newMetricFamily(tt.name, mc).(*metricFamily) + mf := newMetricFamily(tt.name, mc, zap.NewNop()).(*metricFamily) mfp := newMetricFamilyPdata(tt.name, mc).(*metricFamilyPdata) assert.Equal(t, mf.isCumulativeType(), mfp.isCumulativeTypePdata(), "mismatch in isCumulative") assert.Equal(t, mf.isCumulativeType(), tt.want, "isCumulative does not match for regular metricFamily") diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index c8d202d1b19b..56acb7b8d661 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -1355,7 +1355,8 @@ process_start_time_seconds 400.8 var startTimeMetricPageStartTimestamp = ×tamppb.Timestamp{Seconds: 400, Nanos: 800000000} -const numStartTimeMetricPageTimeseries = 6 +// 6 metrics + 5 internal metrics +const numStartTimeMetricPageTimeseries = 11 func verifyStartTimeMetricPage(t *testing.T, _ *testData, mds []*agentmetricspb.ExportMetricsServiceRequest) { numTimeseries := 0 From 3ac8fdfcc116d579babad8e25a540e5434caedad Mon Sep 17 00:00:00 2001 From: Guillaume Gill Date: Wed, 16 Jun 2021 21:13:57 +0200 Subject: [PATCH 22/22] Fix lint --- receiver/prometheusreceiver/metrics_receiver_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/receiver/prometheusreceiver/metrics_receiver_test.go b/receiver/prometheusreceiver/metrics_receiver_test.go index 56acb7b8d661..411e6960fe73 100644 --- a/receiver/prometheusreceiver/metrics_receiver_test.go +++ b/receiver/prometheusreceiver/metrics_receiver_test.go @@ -861,7 +861,7 @@ func verifyTarget2(t *testing.T, td *testData, mds []*agentmetricspb.ExportMetri { MetricDescriptor: &metricspb.MetricDescriptor{ Name: "up", - Description: "The scraping was sucessful", + Description: "The scraping was successful", Type: metricspb.MetricDescriptor_GAUGE_DOUBLE, Unit: "bool", },