|
1 | 1 | package io.prometheus.metrics.model.registry; |
2 | 2 |
|
3 | | -import io.prometheus.metrics.model.snapshots.DataPointSnapshot; |
4 | 3 | import io.prometheus.metrics.model.snapshots.MetricSnapshot; |
5 | 4 | import io.prometheus.metrics.model.snapshots.MetricSnapshots; |
6 | 5 | import java.util.ArrayList; |
7 | 6 | import java.util.Collections; |
8 | | -import java.util.HashMap; |
9 | 7 | import java.util.HashSet; |
10 | 8 | import java.util.List; |
11 | | -import java.util.Map; |
12 | 9 | import java.util.Set; |
13 | 10 | import java.util.concurrent.ConcurrentHashMap; |
14 | 11 | import java.util.function.Predicate; |
@@ -254,8 +251,6 @@ public MetricSnapshots scrape(@Nullable PrometheusScrapeRequest scrapeRequest) { |
254 | 251 | } |
255 | 252 | } |
256 | 253 |
|
257 | | - validateNoDuplicateLabelSchemas(allSnapshots); |
258 | | - |
259 | 254 | MetricSnapshots.Builder result = MetricSnapshots.builder(); |
260 | 255 | for (MetricSnapshot snapshot : allSnapshots) { |
261 | 256 | result.metricSnapshot(snapshot); |
@@ -316,78 +311,10 @@ public MetricSnapshots scrape( |
316 | 311 | } |
317 | 312 | } |
318 | 313 |
|
319 | | - validateNoDuplicateLabelSchemas(allSnapshots); |
320 | | - |
321 | 314 | MetricSnapshots.Builder result = MetricSnapshots.builder(); |
322 | 315 | for (MetricSnapshot snapshot : allSnapshots) { |
323 | 316 | result.metricSnapshot(snapshot); |
324 | 317 | } |
325 | 318 | return result.build(); |
326 | 319 | } |
327 | | - |
328 | | - /** |
329 | | - * Validates that snapshots with the same metric name don't have identical label schemas. This |
330 | | - * prevents duplicate time series which would occur if two snapshots produce data points with |
331 | | - * identical label sets. |
332 | | - */ |
333 | | - private void validateNoDuplicateLabelSchemas(List<MetricSnapshot> snapshots) { |
334 | | - Map<String, List<MetricSnapshot>> snapshotsByName = new HashMap<>(); |
335 | | - for (MetricSnapshot snapshot : snapshots) { |
336 | | - String name = snapshot.getMetadata().getPrometheusName(); |
337 | | - snapshotsByName.computeIfAbsent(name, k -> new ArrayList<>()).add(snapshot); |
338 | | - } |
339 | | - |
340 | | - // For each group with the same name, check for duplicate label schemas |
341 | | - for (Map.Entry<String, List<MetricSnapshot>> entry : snapshotsByName.entrySet()) { |
342 | | - List<MetricSnapshot> group = entry.getValue(); |
343 | | - if (group.size() <= 1) { |
344 | | - continue; |
345 | | - } |
346 | | - |
347 | | - List<Set<String>> labelSchemas = new ArrayList<>(); |
348 | | - for (MetricSnapshot snapshot : group) { |
349 | | - Set<String> labelSchema = extractLabelSchema(snapshot); |
350 | | - if (labelSchema != null) { |
351 | | - if (labelSchemas.contains(labelSchema)) { |
352 | | - throw new IllegalStateException( |
353 | | - snapshot.getMetadata().getPrometheusName() |
354 | | - + ": duplicate metric name with identical label schema " |
355 | | - + labelSchema); |
356 | | - } |
357 | | - labelSchemas.add(labelSchema); |
358 | | - } |
359 | | - } |
360 | | - } |
361 | | - } |
362 | | - |
363 | | - /** |
364 | | - * Extracts the label schema (set of label names) from a snapshot's data points. Returns null if |
365 | | - * the snapshot has no data points or if data points have inconsistent label schemas. |
366 | | - */ |
367 | | - @Nullable |
368 | | - private Set<String> extractLabelSchema(MetricSnapshot snapshot) { |
369 | | - if (snapshot.getDataPoints().isEmpty()) { |
370 | | - return null; |
371 | | - } |
372 | | - |
373 | | - DataPointSnapshot firstDataPoint = snapshot.getDataPoints().get(0); |
374 | | - Set<String> labelNames = new HashSet<>(); |
375 | | - for (int i = 0; i < firstDataPoint.getLabels().size(); i++) { |
376 | | - labelNames.add(firstDataPoint.getLabels().getName(i)); |
377 | | - } |
378 | | - |
379 | | - for (DataPointSnapshot dataPoint : snapshot.getDataPoints()) { |
380 | | - Set<String> currentLabelNames = new HashSet<>(); |
381 | | - for (int i = 0; i < dataPoint.getLabels().size(); i++) { |
382 | | - currentLabelNames.add(dataPoint.getLabels().getName(i)); |
383 | | - } |
384 | | - if (!currentLabelNames.equals(labelNames)) { |
385 | | - // Data points have inconsistent label schemas - this is unusual but valid |
386 | | - // We can't determine a single label schema, so return null |
387 | | - return null; |
388 | | - } |
389 | | - } |
390 | | - |
391 | | - return labelNames; |
392 | | - } |
393 | 320 | } |
0 commit comments