File: Registry.pm

package info (click to toggle)
libnet-prometheus-perl 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 304 kB
  • sloc: perl: 1,847; makefile: 8
file content (120 lines) | stat: -rw-r--r-- 2,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2020-2024 -- leonerd@leonerd.org.uk

package Net::Prometheus::Registry 0.14;

use v5.14;
use warnings;

use Carp;

=head1 NAME

C<Net::Prometheus::Registry> - a collection of metrics collectors

=head1 DESCRIPTION

This class, or instances of it, acts as a simple storage array for instances
derived from L<Net::Prometheus::Metric>, known as "collectors".

A single global collection is stored by the module, accessible via the class
methods. Additional collections may be made with the constructor and then
accessed by instance methods.

=cut

# These are the global ones
my @COLLECTORS;

=head1 CONSTRUCTOR

=for highlighter language=perl

=cut

=head2 new

   $registry = Net::Prometheus::Registry->new;

Returns a new registry instance.

=cut

sub new
{
   my $class = shift;
   return bless [], $class;
}

=head1 METHODS

=cut

=head2 register

   $collector = Net::Prometheus::Registry->register( $collector );
   $collector = $registry->register( $collector );

Adds a new collector to the registry. The collector instance itself is
returned, for convenience of chaining method calls on it.

=cut

sub register
{
   my $collectors = ( ref $_[0] ) ? $_[0] : \@COLLECTORS;
   my ( undef, $collector ) = @_;

   # TODO: ban duplicate registration
   push @$collectors, $collector;

   return $collector;
}

=head2 unregister

   Net::Prometheus::Registry->unregister( $collector );
   $registry->unregister( $collector );

Removes a previously-registered collector.

=cut

sub unregister
{
   my $collectors = ( ref $_[0] ) ? $_[0] : \@COLLECTORS;
   my ( undef, $collector ) = @_;

   my $found;
   @$collectors = grep {
      not( $_ == $collector and ++$found )
   } @$collectors;

   $found or
      croak "No such collector";
}

=head2 collectors

   @collectors = Net::Prometheus::Registry->collectors;
   @collectors = $registry->collectors;

Returns a list of the currently-registered collectors.

=cut

sub collectors
{
   my $collectors = ( ref $_[0] ) ? $_[0] : \@COLLECTORS;
   return @$collectors;
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;