File Coverage

blib/lib/Net/Prometheus/ProcessCollector.pm
Criterion Covered Total %
statement 26 27 96.3
branch 3 4 75.0
condition 4 4 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 42 44 95.4


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