File Coverage

blib/lib/Net/Prometheus/Registry.pm
Criterion Covered Total %
statement 23 23 100.0
branch 5 8 62.5
condition 1 3 33.3
subroutine 7 7 100.0
pod 4 4 100.0
total 40 45 88.8


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