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 7     7   59 use strict;
  7         20  
  7         204  
9 7     7   41 use warnings;
  7         13  
  7         163  
10              
11 7     7   37 use Carp;
  7         13  
  7         2066  
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 7     7 1 19 my $class = shift;
46 7         36 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 13 50   13 1 48 my $collectors = ( ref $_[0] ) ? $_[0] : \@COLLECTORS;
66 13         30 my ( undef, $collector ) = @_;
67              
68             # TODO: ban duplicate registration
69 13         46 push @$collectors, $collector;
70              
71 13         63 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 9 my $collectors = ( ref $_[0] ) ? $_[0] : \@COLLECTORS;
86 2         5 my ( undef, $collector ) = @_;
87              
88 2         3 my $found;
89             @$collectors = grep {
90 2   33     5 not( $_ == $collector and ++$found )
  2         14  
91             } @$collectors;
92              
93 2 50       14 $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 30 100   30 1 79 my $collectors = ( ref $_[0] ) ? $_[0] : \@COLLECTORS;
109 30         91 return @$collectors;
110             }
111              
112             =head1 AUTHOR
113              
114             Paul Evans
115              
116             =cut
117              
118             0x55AA;