File Coverage

blib/lib/Net/Prometheus/ProcessCollector.pm
Criterion Covered Total %
statement 44 45 97.7
branch 3 4 75.0
condition 4 4 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 62 64 96.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, 2016-2026 -- leonerd@leonerd.org.uk
5              
6             package Net::Prometheus::ProcessCollector 0.16;
7              
8 12     12   242978 use v5.20;
  12         49  
9 12     12   64 use warnings;
  12         22  
  12         738  
10              
11 12     12   70 use feature qw( signatures );
  12         19  
  12         1759  
12 12     12   81 no warnings qw( experimental::signatures );
  12         32  
  12         619  
13              
14 12     12   1046 use Net::Prometheus::Types qw( MetricSamples Sample );
  12         28  
  12         8062  
15              
16             =head1 NAME
17              
18             C - obtain a process collector for the OS
19              
20             =head1 SYNOPSIS
21              
22             =for highlighter language=perl
23              
24             use Net::Prometheus::ProcessCollector;
25              
26             my $collector = Net::Prometheus::ProcessCollector->new;
27              
28             =head1 DESCRIPTION
29              
30             This module-loading package provides a method that attempts to load a process
31             collector appropriate for the host OS it is running on.
32              
33             The following OS-specific modules are provided with this distribution:
34              
35             =over 2
36              
37             =item *
38              
39             L
40              
41             =back
42              
43             Other OSes may be supported by 3rd-party CPAN modules by following this naming
44             pattern based on the value of the C<$^O> variable on the OS concerned.
45              
46             =cut
47              
48             =head1 MAGIC CONSTRUCTORS
49              
50             =cut
51              
52             =head2 new
53              
54             $collector = Net::Prometheus::ProcessCollector->new( %args );
55              
56             Attempts to construct a new process collector for the OS named by C<$^O>,
57             passing in any extra arguments into the C constructor for the specific
58             class.
59              
60             If no perl module is found under the appropriate file name, C is
61             returned. If any other error occurs while loading or constructing the
62             instance, the exception is thrown as normal.
63              
64             Typically a process exporter should support the following named arguments:
65              
66             =over
67              
68             =item prefix => STR
69              
70             A prefix to prepend on all the exported variable names. If not provided, the
71             default should be C<"process">.
72              
73             =item labels => ARRAY
74              
75             Additional labels to set on exported variables. If not provided, no extra
76             labels will be set.
77              
78             =back
79              
80             =cut
81              
82 4         12 sub new ( $class, @args )
83 4     4 1 11 {
  4         10  
  4         119  
84 4         80 $class->for_OS( $^O, @args );
85             }
86              
87             =head2 for_OS
88              
89             $collector = Net::Prometheus::ProcessCollector->for_OS( $os, @args );
90              
91             Attempts to construct a new process collector for the named OS. Except under
92             especially-exceptional circumstances, you don't want to call this method.
93             Call L instead.
94              
95             =cut
96              
97 5         16 sub for_OS ( $class, $os, @args )
  5         20  
98 5     5 1 283125 {
  5         11  
  5         28  
99 5         21 my $pkg = __PACKAGE__ . "::$os";
100              
101 5         65 ( my $file = "$pkg.pm" ) =~ s{::}{/}g;
102 5 100       13 if( !eval { require $file } ) {
  5         3462  
103 1 50       39 return if $@ =~ m/^Can't locate \Q$file\E in \@INC/;
104 0         0 die $@;
105             }
106              
107 4         47 return $pkg->new( @args );
108             }
109              
110             # Methods for subclasses
111              
112 6         16 sub __new ( $class, %args )
113 6     6   20 {
  6         15  
  6         11  
114             return bless {
115             prefix => $args{prefix} || "process",
116 6   100     95 labels => $args{labels} || [],
      100        
117             }, $class;
118             }
119              
120 108         186 sub _make_metric ( $self, $varname, $value, $type, $help )
  108         197  
  108         185  
  108         177  
121 108     108   9405 {
  108         174  
  108         239  
122 108         254 my $prefix = $self->{prefix};
123              
124             return MetricSamples( "${prefix}_$varname", $type, $help,
125 108         525 [ Sample( "${prefix}_$varname", $self->{labels}, $value ) ] );
126             }
127              
128             =head1 AUTHOR
129              
130             Paul Evans
131              
132             =cut
133              
134             0x55AA;