|
| 1 | +package io.prometheus.metrics.expositionformats; |
| 2 | + |
| 3 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 6 | + |
| 7 | +import io.prometheus.metrics.model.registry.Collector; |
| 8 | +import io.prometheus.metrics.model.registry.PrometheusRegistry; |
| 9 | +import io.prometheus.metrics.model.snapshots.CounterSnapshot; |
| 10 | +import io.prometheus.metrics.model.snapshots.Labels; |
| 11 | +import io.prometheus.metrics.model.snapshots.MetricSnapshot; |
| 12 | +import io.prometheus.metrics.model.snapshots.MetricSnapshots; |
| 13 | +import java.io.ByteArrayOutputStream; |
| 14 | +import java.io.IOException; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +class DuplicateNamesExpositionTest { |
| 18 | + |
| 19 | + private static PrometheusRegistry getPrometheusRegistry() { |
| 20 | + PrometheusRegistry registry = new PrometheusRegistry(); |
| 21 | + |
| 22 | + registry.register( |
| 23 | + new Collector() { |
| 24 | + @Override |
| 25 | + public MetricSnapshot collect() { |
| 26 | + return CounterSnapshot.builder() |
| 27 | + .name("api_responses") |
| 28 | + .help("API responses") |
| 29 | + .dataPoint( |
| 30 | + CounterSnapshot.CounterDataPointSnapshot.builder() |
| 31 | + .labels(Labels.of("uri", "/hello", "outcome", "SUCCESS")) |
| 32 | + .value(100) |
| 33 | + .build()) |
| 34 | + .build(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getPrometheusName() { |
| 39 | + return "api_responses_total"; |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + registry.register( |
| 44 | + new Collector() { |
| 45 | + @Override |
| 46 | + public MetricSnapshot collect() { |
| 47 | + return CounterSnapshot.builder() |
| 48 | + .name("api_responses") |
| 49 | + .help("API responses") |
| 50 | + .dataPoint( |
| 51 | + CounterSnapshot.CounterDataPointSnapshot.builder() |
| 52 | + .labels( |
| 53 | + Labels.of("uri", "/hello", "outcome", "FAILURE", "error", "TIMEOUT")) |
| 54 | + .value(10) |
| 55 | + .build()) |
| 56 | + .build(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String getPrometheusName() { |
| 61 | + return "api_responses_total"; |
| 62 | + } |
| 63 | + }); |
| 64 | + return registry; |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testDuplicateNames_differentLabels_producesValidOutput() throws IOException { |
| 69 | + PrometheusRegistry registry = getPrometheusRegistry(); |
| 70 | + |
| 71 | + MetricSnapshots snapshots = registry.scrape(); |
| 72 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 73 | + PrometheusTextFormatWriter writer = PrometheusTextFormatWriter.create(); |
| 74 | + writer.write(out, snapshots); |
| 75 | + String output = out.toString(UTF_8); |
| 76 | + |
| 77 | + String expected = |
| 78 | + """ |
| 79 | + # HELP api_responses_total API responses |
| 80 | + # TYPE api_responses_total counter |
| 81 | + api_responses_total{error="TIMEOUT",outcome="FAILURE",uri="/hello"} 10.0 |
| 82 | + api_responses_total{outcome="SUCCESS",uri="/hello"} 100.0 |
| 83 | + """; |
| 84 | + |
| 85 | + assertThat(output).isEqualTo(expected); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testDuplicateNames_multipleDataPoints_producesValidOutput() throws IOException { |
| 90 | + PrometheusRegistry registry = new PrometheusRegistry(); |
| 91 | + |
| 92 | + registry.register( |
| 93 | + new Collector() { |
| 94 | + @Override |
| 95 | + public MetricSnapshot collect() { |
| 96 | + return CounterSnapshot.builder() |
| 97 | + .name("api_responses") |
| 98 | + .help("API responses") |
| 99 | + .dataPoint( |
| 100 | + CounterSnapshot.CounterDataPointSnapshot.builder() |
| 101 | + .labels(Labels.of("uri", "/hello", "outcome", "SUCCESS")) |
| 102 | + .value(100) |
| 103 | + .build()) |
| 104 | + .dataPoint( |
| 105 | + CounterSnapshot.CounterDataPointSnapshot.builder() |
| 106 | + .labels(Labels.of("uri", "/world", "outcome", "SUCCESS")) |
| 107 | + .value(200) |
| 108 | + .build()) |
| 109 | + .build(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String getPrometheusName() { |
| 114 | + return "api_responses_total"; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + registry.register( |
| 119 | + new Collector() { |
| 120 | + @Override |
| 121 | + public MetricSnapshot collect() { |
| 122 | + return CounterSnapshot.builder() |
| 123 | + .name("api_responses") |
| 124 | + .help("API responses") |
| 125 | + .dataPoint( |
| 126 | + CounterSnapshot.CounterDataPointSnapshot.builder() |
| 127 | + .labels( |
| 128 | + Labels.of("uri", "/hello", "outcome", "FAILURE", "error", "TIMEOUT")) |
| 129 | + .value(10) |
| 130 | + .build()) |
| 131 | + .dataPoint( |
| 132 | + CounterSnapshot.CounterDataPointSnapshot.builder() |
| 133 | + .labels( |
| 134 | + Labels.of("uri", "/world", "outcome", "FAILURE", "error", "NOT_FOUND")) |
| 135 | + .value(5) |
| 136 | + .build()) |
| 137 | + .build(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public String getPrometheusName() { |
| 142 | + return "api_responses_total"; |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + MetricSnapshots snapshots = registry.scrape(); |
| 147 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 148 | + PrometheusTextFormatWriter writer = PrometheusTextFormatWriter.create(); |
| 149 | + writer.write(out, snapshots); |
| 150 | + String output = out.toString(UTF_8); |
| 151 | + |
| 152 | + String expected = |
| 153 | + """ |
| 154 | + # HELP api_responses_total API responses |
| 155 | + # TYPE api_responses_total counter |
| 156 | + api_responses_total{error="NOT_FOUND",outcome="FAILURE",uri="/world"} 5.0 |
| 157 | + api_responses_total{error="TIMEOUT",outcome="FAILURE",uri="/hello"} 10.0 |
| 158 | + api_responses_total{outcome="SUCCESS",uri="/hello"} 100.0 |
| 159 | + api_responses_total{outcome="SUCCESS",uri="/world"} 200.0 |
| 160 | + """; |
| 161 | + assertThat(output).isEqualTo(expected); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + void testOpenMetricsFormat_withDuplicateNames() throws IOException { |
| 166 | + PrometheusRegistry registry = getPrometheusRegistry(); |
| 167 | + |
| 168 | + MetricSnapshots snapshots = registry.scrape(); |
| 169 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 170 | + OpenMetricsTextFormatWriter writer = new OpenMetricsTextFormatWriter(false, false); |
| 171 | + writer.write(out, snapshots); |
| 172 | + String output = out.toString(UTF_8); |
| 173 | + |
| 174 | + String expected = |
| 175 | + """ |
| 176 | + # TYPE api_responses counter |
| 177 | + # HELP api_responses API responses |
| 178 | + api_responses_total{error="TIMEOUT",outcome="FAILURE",uri="/hello"} 10.0 |
| 179 | + api_responses_total{outcome="SUCCESS",uri="/hello"} 100.0 |
| 180 | + # EOF |
| 181 | + """; |
| 182 | + assertThat(output).isEqualTo(expected); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + void testDuplicateNames_sameLabels_throwsException() { |
| 187 | + PrometheusRegistry registry = new PrometheusRegistry(); |
| 188 | + |
| 189 | + registry.register( |
| 190 | + new Collector() { |
| 191 | + @Override |
| 192 | + public MetricSnapshot collect() { |
| 193 | + return CounterSnapshot.builder() |
| 194 | + .name("api_responses") |
| 195 | + .help("API responses") |
| 196 | + .dataPoint( |
| 197 | + CounterSnapshot.CounterDataPointSnapshot.builder() |
| 198 | + .labels(Labels.of("uri", "/hello", "outcome", "SUCCESS")) |
| 199 | + .value(100) |
| 200 | + .build()) |
| 201 | + .build(); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public String getPrometheusName() { |
| 206 | + return "api_responses_total"; |
| 207 | + } |
| 208 | + }); |
| 209 | + |
| 210 | + registry.register( |
| 211 | + new Collector() { |
| 212 | + @Override |
| 213 | + public MetricSnapshot collect() { |
| 214 | + return CounterSnapshot.builder() |
| 215 | + .name("api_responses") |
| 216 | + .help("API responses") |
| 217 | + .dataPoint( |
| 218 | + CounterSnapshot.CounterDataPointSnapshot.builder() |
| 219 | + .labels(Labels.of("uri", "/hello", "outcome", "SUCCESS")) |
| 220 | + .value(50) |
| 221 | + .build()) |
| 222 | + .build(); |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public String getPrometheusName() { |
| 227 | + return "api_responses_total"; |
| 228 | + } |
| 229 | + }); |
| 230 | + |
| 231 | + // Scrape should throw exception due to duplicate time series (same name + same labels) |
| 232 | + assertThatThrownBy(registry::scrape) |
| 233 | + .isInstanceOf(IllegalStateException.class) |
| 234 | + .hasMessageContaining("duplicate metric name with identical label schema [uri, outcome]") |
| 235 | + .hasMessageContaining("api_responses"); |
| 236 | + } |
| 237 | +} |
0 commit comments