|
| 1 | +package io.prometheus.metrics.it.exporter.test; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import io.prometheus.client.it.common.ExporterTest; |
| 6 | +import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_4_33_1.Metrics; |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.URISyntaxException; |
| 9 | +import java.util.List; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +/** |
| 13 | + * Integration test for duplicate metric names with different label sets. |
| 14 | + * |
| 15 | + * <p>This test validates that: |
| 16 | + * |
| 17 | + * <ul> |
| 18 | + * <li>Multiple metrics with the same Prometheus name but different labels can be registered |
| 19 | + * <li>All exposition formats (text, OpenMetrics, protobuf) correctly merge and expose them |
| 20 | + * <li>The merged output is valid and scrapeable by Prometheus |
| 21 | + * </ul> |
| 22 | + */ |
| 23 | +class DuplicateMetricsIT extends ExporterTest { |
| 24 | + |
| 25 | + public DuplicateMetricsIT() throws IOException, URISyntaxException { |
| 26 | + super("exporter-duplicate-metrics-sample"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void testDuplicateMetricsInPrometheusTextFormat() throws IOException { |
| 31 | + start(); |
| 32 | + Response response = scrape("GET", ""); |
| 33 | + assertThat(response.status).isEqualTo(200); |
| 34 | + assertContentType( |
| 35 | + "text/plain; version=0.0.4; charset=utf-8", response.getHeader("Content-Type")); |
| 36 | + |
| 37 | + String body = response.stringBody(); |
| 38 | + |
| 39 | + assertThat(body).contains("# TYPE http_requests_total counter"); |
| 40 | + assertThat(body).contains("# HELP http_requests_total Total HTTP requests by status"); |
| 41 | + |
| 42 | + // Verify all data points from both collectors are present |
| 43 | + assertThat(body) |
| 44 | + .contains("http_requests_total{method=\"GET\",status=\"success\"} 150.0") |
| 45 | + .contains("http_requests_total{method=\"POST\",status=\"success\"} 45.0") |
| 46 | + .contains("http_requests_total{endpoint=\"/api\",status=\"error\"} 5.0") |
| 47 | + .contains("http_requests_total{endpoint=\"/health\",status=\"error\"} 2.0"); |
| 48 | + |
| 49 | + assertThat(body).contains("# TYPE active_connections gauge"); |
| 50 | + assertThat(body).contains("# HELP active_connections Active connections"); |
| 51 | + |
| 52 | + assertThat(body) |
| 53 | + .contains("active_connections{protocol=\"http\",region=\"us-east\"} 42.0") |
| 54 | + .contains("active_connections{protocol=\"http\",region=\"us-west\"} 38.0") |
| 55 | + .contains("active_connections{protocol=\"https\",region=\"eu-west\"} 55.0") |
| 56 | + .contains("active_connections{pool=\"primary\",type=\"read\"} 30.0") |
| 57 | + .contains("active_connections{pool=\"replica\",type=\"write\"} 10.0"); |
| 58 | + |
| 59 | + assertThat(body).contains("unique_metric_bytes_total 1024.0"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testDuplicateMetricsInOpenMetricsTextFormat() throws IOException { |
| 64 | + start(); |
| 65 | + Response response = |
| 66 | + scrape("GET", "", "Accept", "application/openmetrics-text; version=1.0.0; charset=utf-8"); |
| 67 | + assertThat(response.status).isEqualTo(200); |
| 68 | + assertContentType( |
| 69 | + "application/openmetrics-text; version=1.0.0; charset=utf-8", |
| 70 | + response.getHeader("Content-Type")); |
| 71 | + |
| 72 | + String body = response.stringBody(); |
| 73 | + |
| 74 | + // Verify http_requests_total is properly merged |
| 75 | + assertThat(body).contains("# TYPE http_requests counter"); |
| 76 | + assertThat(body) |
| 77 | + .contains("http_requests_total{method=\"GET\",status=\"success\"} 150.0") |
| 78 | + .contains("http_requests_total{method=\"POST\",status=\"success\"} 45.0") |
| 79 | + .contains("http_requests_total{endpoint=\"/api\",status=\"error\"} 5.0") |
| 80 | + .contains("http_requests_total{endpoint=\"/health\",status=\"error\"} 2.0"); |
| 81 | + |
| 82 | + // Verify active_connections is properly merged |
| 83 | + assertThat(body).contains("# TYPE active_connections gauge"); |
| 84 | + assertThat(body) |
| 85 | + .contains("active_connections{protocol=\"http\",region=\"us-east\"} 42.0") |
| 86 | + .contains("active_connections{protocol=\"http\",region=\"us-west\"} 38.0") |
| 87 | + .contains("active_connections{protocol=\"https\",region=\"eu-west\"} 55.0") |
| 88 | + .contains("active_connections{pool=\"primary\",type=\"read\"} 30.0") |
| 89 | + .contains("active_connections{pool=\"replica\",type=\"write\"} 10.0"); |
| 90 | + |
| 91 | + // OpenMetrics format should have UNIT for unique_metric_bytes (base name without _total) |
| 92 | + assertThat(body) |
| 93 | + .contains("unique_metric_bytes_total 1024.0") |
| 94 | + .contains("# UNIT unique_metric_bytes bytes"); |
| 95 | + |
| 96 | + assertThat(body).endsWith("# EOF\n"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void testDuplicateMetricsInPrometheusProtobufFormat() throws IOException { |
| 101 | + start();2 |
| 102 | + Response response = |
| 103 | + scrape( |
| 104 | + "GET", |
| 105 | + "", |
| 106 | + "Accept", |
| 107 | + "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily;" |
| 108 | + + " encoding=delimited"); |
| 109 | + assertThat(response.status).isEqualTo(200); |
| 110 | + assertContentType( |
| 111 | + "application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily;" |
| 112 | + + " encoding=delimited", |
| 113 | + response.getHeader("Content-Type")); |
| 114 | + |
| 115 | + List<Metrics.MetricFamily> metrics = response.protoBody(); |
| 116 | + |
| 117 | + // Should have exactly 3 metric families (active_connections, http_requests_total, |
| 118 | + // unique_metric_bytes_total) |
| 119 | + assertThat(metrics).hasSize(3); |
| 120 | + |
| 121 | + // Metrics are sorted by name |
| 122 | + assertThat(metrics.get(0).getName()).isEqualTo("active_connections"); |
| 123 | + assertThat(metrics.get(1).getName()).isEqualTo("http_requests_total"); |
| 124 | + assertThat(metrics.get(2).getName()).isEqualTo("unique_metric_bytes_total"); |
| 125 | + |
| 126 | + // Verify active_connections has all 5 data points merged |
| 127 | + Metrics.MetricFamily activeConnections = metrics.get(0); |
| 128 | + assertThat(activeConnections.getType()).isEqualTo(Metrics.MetricType.GAUGE); |
| 129 | + assertThat(activeConnections.getHelp()).isEqualTo("Active connections"); |
| 130 | + assertThat(activeConnections.getMetricList()).hasSize(5); |
| 131 | + |
| 132 | + // Verify http_requests_total has all 4 data points merged |
| 133 | + Metrics.MetricFamily httpRequests = metrics.get(1); |
| 134 | + assertThat(httpRequests.getType()).isEqualTo(Metrics.MetricType.COUNTER); |
| 135 | + assertThat(httpRequests.getHelp()).isEqualTo("Total HTTP requests by status"); |
| 136 | + assertThat(httpRequests.getMetricList()).hasSize(4); |
| 137 | + |
| 138 | + // Verify each data point has the expected labels |
| 139 | + boolean foundSuccessGet = false; |
| 140 | + boolean foundSuccessPost = false; |
| 141 | + boolean foundErrorApi = false; |
| 142 | + boolean foundErrorHealth = false; |
| 143 | + |
| 144 | + for (Metrics.Metric metric : httpRequests.getMetricList()) { |
| 145 | + List<Metrics.LabelPair> labels = metric.getLabelList(); |
| 146 | + if (hasLabel(labels, "status", "success") && hasLabel(labels, "method", "GET")) { |
| 147 | + assertThat(metric.getCounter().getValue()).isEqualTo(150.0); |
| 148 | + foundSuccessGet = true; |
| 149 | + } else if (hasLabel(labels, "status", "success") && hasLabel(labels, "method", "POST")) { |
| 150 | + assertThat(metric.getCounter().getValue()).isEqualTo(45.0); |
| 151 | + foundSuccessPost = true; |
| 152 | + } else if (hasLabel(labels, "status", "error") && hasLabel(labels, "endpoint", "/api")) { |
| 153 | + assertThat(metric.getCounter().getValue()).isEqualTo(5.0); |
| 154 | + foundErrorApi = true; |
| 155 | + } else if (hasLabel(labels, "status", "error") && hasLabel(labels, "endpoint", "/health")) { |
| 156 | + assertThat(metric.getCounter().getValue()).isEqualTo(2.0); |
| 157 | + foundErrorHealth = true; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + assertThat(foundSuccessGet).isTrue(); |
| 162 | + assertThat(foundSuccessPost).isTrue(); |
| 163 | + assertThat(foundErrorApi).isTrue(); |
| 164 | + assertThat(foundErrorHealth).isTrue(); |
| 165 | + |
| 166 | + // Verify unique metric |
| 167 | + Metrics.MetricFamily uniqueMetric = metrics.get(2); |
| 168 | + assertThat(uniqueMetric.getType()).isEqualTo(Metrics.MetricType.COUNTER); |
| 169 | + assertThat(uniqueMetric.getMetricList()).hasSize(1); |
| 170 | + assertThat(uniqueMetric.getMetric(0).getCounter().getValue()).isEqualTo(1024.0); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void testDuplicateMetricsWithNameFilter() throws IOException { |
| 175 | + start(); |
| 176 | + // Only scrape http_requests_total |
| 177 | + Response response = scrape("GET", nameParam()); |
| 178 | + assertThat(response.status).isEqualTo(200); |
| 179 | + |
| 180 | + String body = response.stringBody(); |
| 181 | + |
| 182 | + assertThat(body) |
| 183 | + .contains("http_requests_total{method=\"GET\",status=\"success\"} 150.0") |
| 184 | + .contains("http_requests_total{endpoint=\"/api\",status=\"error\"} 5.0"); |
| 185 | + |
| 186 | + // Should NOT contain active_connections or unique_metric_total |
| 187 | + assertThat(body).doesNotContain("active_connections").doesNotContain("unique_metric_total"); |
| 188 | + } |
| 189 | + |
| 190 | + private boolean hasLabel(List<Metrics.LabelPair> labels, String name, String value) { |
| 191 | + return labels.stream() |
| 192 | + .anyMatch(label -> label.getName().equals(name) && label.getValue().equals(value)); |
| 193 | + } |
| 194 | + |
| 195 | + private String nameParam() { |
| 196 | + return "name[]=" + "http_requests_total"; |
| 197 | + } |
| 198 | +} |
| 199 | + |
0 commit comments