|
1 | 1 | package io.prometheus.metrics.model.registry; |
2 | 2 |
|
3 | 3 | import static java.util.Arrays.asList; |
4 | | -import static java.util.Collections.emptyList; |
| 4 | +import static java.util.Collections.singletonList; |
5 | 5 | import static org.assertj.core.api.Assertions.assertThat; |
6 | 6 | import static org.assertj.core.api.Assertions.assertThatCode; |
7 | 7 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
8 | 8 |
|
9 | 9 | import io.prometheus.metrics.model.snapshots.CounterSnapshot; |
10 | 10 | import io.prometheus.metrics.model.snapshots.GaugeSnapshot; |
11 | | -import io.prometheus.metrics.model.snapshots.HistogramSnapshot; |
12 | 11 | import io.prometheus.metrics.model.snapshots.Labels; |
13 | 12 | import io.prometheus.metrics.model.snapshots.MetricSnapshot; |
14 | 13 | import io.prometheus.metrics.model.snapshots.MetricSnapshots; |
@@ -538,4 +537,173 @@ public String getPrometheusName() { |
538 | 537 | assertThat(registry.scrape().size()).isEqualTo(0); |
539 | 538 | } |
540 | 539 |
|
| 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 | + } |
541 | 709 | } |
0 commit comments