File Coverage

blib/lib/Net/Prometheus/Registry.pm
Criterion Covered Total %
statement 39 39 100.0
branch 5 8 62.5
condition 1 3 33.3
subroutine 9 9 100.0
pod 4 4 100.0
total 58 63 92.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2020-2026 -- leonerd@leonerd.org.uk
5              
6             package Net::Prometheus::Registry 0.16;
7              
8 10     10   132 use v5.20;
  10         41  
9 10     10   63 use warnings;
  10         21  
  10         727  
10              
11 10     10   63 use feature qw( signatures );
  10         20  
  10         1347  
12 10     10   85 no warnings qw( experimental::signatures );
  10         50  
  10         506  
13              
14 10     10   77 use Carp;
  10         17  
  10         4824  
15              
16             =head1 NAME
17              
18             C - a collection of metrics collectors
19              
20             =head1 DESCRIPTION
21              
22             This class, or instances of it, acts as a simple storage array for instances
23             derived from L, known as "collectors".
24              
25             A single global collection is stored by the module, accessible via the class
26             methods. Additional collections may be made with the constructor and then
27             accessed by instance methods.
28              
29             =cut
30              
31             # These are the global ones
32             my @COLLECTORS;
33              
34             =head1 CONSTRUCTOR
35              
36             =for highlighter language=perl
37              
38             =cut
39              
40             =head2 new
41              
42             $registry = Net::Prometheus::Registry->new;
43              
44             Returns a new registry instance.
45              
46             =cut
47              
48             sub new ( $class )
49 11     11 1 27 {
  11         24  
  11         22  
50 11         68 return bless [], $class;
51             }
52              
53             =head1 METHODS
54              
55             =cut
56              
57             =head2 register
58              
59             $collector = Net::Prometheus::Registry->register( $collector );
60             $collector = $registry->register( $collector );
61              
62             Adds a new collector to the registry. The collector instance itself is
63             returned, for convenience of chaining method calls on it.
64              
65             =cut
66              
67 21         36 sub register ( $self, $collector )
68 21     21 1 42 {
  21         36  
  21         37  
69 21 50       65 my $collectors = ( ref $self ) ? $self : \@COLLECTORS;
70              
71             # TODO: ban duplicate registration
72 21         67 push @$collectors, $collector;
73              
74 21         87 return $collector;
75             }
76              
77             =head2 unregister
78              
79             Net::Prometheus::Registry->unregister( $collector );
80             $registry->unregister( $collector );
81              
82             Removes a previously-registered collector.
83              
84             =cut
85              
86 2         5 sub unregister ( $self, $collector )
87 2     2 1 5 {
  2         4  
  2         4  
88 2 50       8 my $collectors = ( ref $self ) ? $self : \@COLLECTORS;
89              
90 2         5 my $found;
91             @$collectors = grep {
92 2   33     5 not( $_ == $collector and ++$found )
  2         81  
93             } @$collectors;
94              
95 2 50       27 $found or
96             croak "No such collector";
97             }
98              
99             =head2 collectors
100              
101             @collectors = Net::Prometheus::Registry->collectors;
102             @collectors = $registry->collectors;
103              
104             Returns a list of the currently-registered collectors.
105              
106             =cut
107              
108             sub collectors ( $self )
109 44     44 1 1710 {
  44         88  
  44         72  
110 44 100       136 my $collectors = ( ref $self ) ? $self : \@COLLECTORS;
111 44         271 return @$collectors;
112             }
113              
114             =head1 AUTHOR
115              
116             Paul Evans
117              
118             =cut
119              
120             0x55AA;