Skip to content

Commit 659a222

Browse files
committed
cleanup and new test
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
1 parent 159329b commit 659a222

File tree

3 files changed

+170
-4
lines changed

3 files changed

+170
-4
lines changed

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/Collector.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ default MetricSnapshot collect(
6060
* This is called in two places:
6161
*
6262
* <ol>
63-
* <li>During registration to check if a metric with that name already exists.
6463
* <li>During scrape to check if this collector can be skipped because a name filter is present
6564
* and the metric name is excluded.
6665
* </ol>

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MultiCollector.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ default MetricSnapshots collect(
5555
* This is called in two places:
5656
*
5757
* <ol>
58-
* <li>During registration to check if a metric with that name already exists.
5958
* <li>During scrape to check if the collector can be skipped because a name filter is present
6059
* and all names are excluded.
6160
* </ol>

prometheus-metrics-model/src/test/java/io/prometheus/metrics/model/registry/PrometheusRegistryTest.java

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package io.prometheus.metrics.model.registry;
22

33
import static java.util.Arrays.asList;
4-
import static java.util.Collections.emptyList;
4+
import static java.util.Collections.singletonList;
55
import static org.assertj.core.api.Assertions.assertThat;
66
import static org.assertj.core.api.Assertions.assertThatCode;
77
import static org.assertj.core.api.Assertions.assertThatThrownBy;
88

99
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
1010
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
11-
import io.prometheus.metrics.model.snapshots.HistogramSnapshot;
1211
import io.prometheus.metrics.model.snapshots.Labels;
1312
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
1413
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
@@ -538,4 +537,173 @@ public String getPrometheusName() {
538537
assertThat(registry.scrape().size()).isEqualTo(0);
539538
}
540539

540+
@Test
541+
void testPartialUnregister_scrapeStillWorks() {
542+
PrometheusRegistry registry = new PrometheusRegistry();
543+
544+
Collector counter1 =
545+
new Collector() {
546+
@Override
547+
public MetricSnapshot collect() {
548+
return CounterSnapshot.builder()
549+
.name("api_requests")
550+
.dataPoint(
551+
CounterSnapshot.CounterDataPointSnapshot.builder()
552+
.labels(Labels.of("endpoint", "/api/v1"))
553+
.value(100)
554+
.build())
555+
.build();
556+
}
557+
558+
@Override
559+
public String getPrometheusName() {
560+
return "api_requests_total";
561+
}
562+
};
563+
564+
Collector counter2 =
565+
new Collector() {
566+
@Override
567+
public MetricSnapshot collect() {
568+
return CounterSnapshot.builder()
569+
.name("api_requests")
570+
.dataPoint(
571+
CounterSnapshot.CounterDataPointSnapshot.builder()
572+
.labels(Labels.of("endpoint", "/api/v2"))
573+
.value(200)
574+
.build())
575+
.build();
576+
}
577+
578+
@Override
579+
public String getPrometheusName() {
580+
return "api_requests_total";
581+
}
582+
};
583+
584+
Collector counter3 =
585+
new Collector() {
586+
@Override
587+
public MetricSnapshot collect() {
588+
return CounterSnapshot.builder()
589+
.name("api_requests")
590+
.dataPoint(
591+
CounterSnapshot.CounterDataPointSnapshot.builder()
592+
.labels(Labels.of("endpoint", "/api/v3"))
593+
.value(300)
594+
.build())
595+
.build();
596+
}
597+
598+
@Override
599+
public String getPrometheusName() {
600+
return "api_requests_total";
601+
}
602+
};
603+
604+
registry.register(counter1);
605+
registry.register(counter2);
606+
registry.register(counter3);
607+
assertThat(registry.scrape().size()).isEqualTo(3);
608+
609+
registry.unregister(counter2);
610+
611+
MetricSnapshots snapshots = registry.scrape();
612+
assertThat(snapshots.size()).isEqualTo(2);
613+
614+
int totalDataPoints = snapshots.stream().mapToInt(s -> s.getDataPoints().size()).sum();
615+
assertThat(totalDataPoints).isEqualTo(2);
616+
}
617+
618+
@Test
619+
void testCollectorAndMultiCollectorNameOverlap_sameType() {
620+
PrometheusRegistry registry = new PrometheusRegistry();
621+
622+
Collector singleCounter =
623+
new Collector() {
624+
@Override
625+
public MetricSnapshot collect() {
626+
return CounterSnapshot.builder()
627+
.name("shared_metric")
628+
.dataPoint(
629+
CounterSnapshot.CounterDataPointSnapshot.builder()
630+
.labels(Labels.of("source", "collector"))
631+
.value(100)
632+
.build())
633+
.build();
634+
}
635+
636+
@Override
637+
public String getPrometheusName() {
638+
return "shared_metric_total";
639+
}
640+
};
641+
642+
MultiCollector multi =
643+
new MultiCollector() {
644+
@Override
645+
public MetricSnapshots collect() {
646+
return new MetricSnapshots(
647+
CounterSnapshot.builder()
648+
.name("shared_metric")
649+
.dataPoint(
650+
CounterSnapshot.CounterDataPointSnapshot.builder()
651+
.labels(Labels.of("source", "multicollector"))
652+
.value(200)
653+
.build())
654+
.build(),
655+
GaugeSnapshot.builder().name("other_metric").build());
656+
}
657+
658+
@Override
659+
public List<String> getPrometheusNames() {
660+
return asList("shared_metric_total", "other_metric");
661+
}
662+
};
663+
664+
registry.register(singleCounter);
665+
registry.register(multi);
666+
667+
MetricSnapshots snapshots = registry.scrape();
668+
assertThat(snapshots.size()).isEqualTo(3);
669+
}
670+
671+
@Test
672+
void testCollectorAndMultiCollectorNameOverlap_differentType() {
673+
PrometheusRegistry registry = new PrometheusRegistry();
674+
675+
Collector counter =
676+
new Collector() {
677+
@Override
678+
public MetricSnapshot collect() {
679+
return CounterSnapshot.builder().name("conflict").build();
680+
}
681+
682+
@Override
683+
public String getPrometheusName() {
684+
return "conflict_metric";
685+
}
686+
};
687+
688+
MultiCollector multi =
689+
new MultiCollector() {
690+
@Override
691+
public MetricSnapshots collect() {
692+
return new MetricSnapshots(GaugeSnapshot.builder().name("conflict").build());
693+
}
694+
695+
@Override
696+
public List<String> getPrometheusNames() {
697+
return singletonList("conflict_metric");
698+
}
699+
};
700+
701+
registry.register(counter);
702+
registry.register(multi);
703+
704+
assertThatThrownBy(registry::scrape)
705+
.isInstanceOf(IllegalArgumentException.class)
706+
.hasMessageContaining("Conflicting metric types")
707+
.hasMessageContaining("conflict");
708+
}
541709
}

0 commit comments

Comments
 (0)