From b37822be242e59189c8271ee2c6dbdd99381f476 Mon Sep 17 00:00:00 2001 From: Robert Wu <94589791+tongoss@users.noreply.github.com> Date: Thu, 16 Oct 2025 03:14:45 -0400 Subject: [PATCH] Prometheus exporter tests: iterate through all scopes rather than looking only at the first (#7510) **Current State:** Only inspected the first ScopeMetrics[0]. **New:** Iterates all ScopeMetrics to look for the four expected metrics anywhere. Co-authored-by: Damien Mathieu <42@dmathieu.com> --- exporters/prometheus/exporter_test.go | 36 +++++++++++++++++++-------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/exporters/prometheus/exporter_test.go b/exporters/prometheus/exporter_test.go index 840f968ff..2c28bb1db 100644 --- a/exporters/prometheus/exporter_test.go +++ b/exporters/prometheus/exporter_test.go @@ -2497,10 +2497,15 @@ func TestExporterSelfInstrumentation(t *testing.T) { var rm metricdata.ResourceMetrics err := observReader.Collect(t.Context(), &rm) require.NoError(t, err) - if len(rm.ScopeMetrics) == 0 { - return metricdata.ScopeMetrics{} + + // Find the Prometheus exporter observability scope specifically. + for _, sm := range rm.ScopeMetrics { + if sm.Scope.Name == observ.ScopeName { + return sm + } } - return rm.ScopeMetrics[0] + // Exporter scope not found (e.g., if disabled or no scrape yet). + return metricdata.ScopeMetrics{} } } @@ -2811,14 +2816,25 @@ func TestExporterSelfInstrumentationExemplarHandling(t *testing.T) { require.NoError(t, err) if len(observMetrics.ScopeMetrics) > 0 { - scopeMetrics := observMetrics.ScopeMetrics[0] + expectedMetrics := map[string]bool{ + "otel.sdk.exporter.metric_data_point.inflight": false, + "otel.sdk.exporter.metric_data_point.exported": false, + "otel.sdk.exporter.operation.duration": false, + "otel.sdk.metric_reader.collection.duration": false, + } + + // Check all scope metrics, not just the first one + for _, scopeMetrics := range observMetrics.ScopeMetrics { + for _, m := range scopeMetrics.Metrics { + if _, exists := expectedMetrics[m.Name]; exists { + expectedMetrics[m.Name] = true + } + } + } + foundObservabilityMetrics := 0 - for _, m := range scopeMetrics.Metrics { - switch m.Name { - case "otel.sdk.exporter.metric_data_point.inflight", - "otel.sdk.exporter.metric_data_point.exported", - "otel.sdk.exporter.operation.duration", - "otel.sdk.metric_reader.collection.duration": + for _, found := range expectedMetrics { + if found { foundObservabilityMetrics++ } }