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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
# 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, 2016-2024 -- leonerd@leonerd.org.uk
package Net::Prometheus::ProcessCollector 0.14;
use v5.14;
use warnings;
use Net::Prometheus::Types qw( MetricSamples Sample );
=head1 NAME
C<Net::Prometheus::ProcessCollector> - obtain a process collector for the OS
=head1 SYNOPSIS
=for highlighter language=perl
use Net::Prometheus::ProcessCollector;
my $collector = Net::Prometheus::ProcessCollector->new;
=head1 DESCRIPTION
This module-loading package provides a method that attempts to load a process
collector appropriate for the host OS it is running on.
The following OS-specific modules are provided with this distribution:
=over 2
=item *
L<Net::Prometheus::ProcessCollector::linux>
=back
Other OSes may be supported by 3rd-party CPAN modules by following this naming
pattern based on the value of the C<$^O> variable on the OS concerned.
=cut
=head1 MAGIC CONSTRUCTORS
=cut
=head2 new
$collector = Net::Prometheus::ProcessCollector->new( %args );
Attempts to construct a new process collector for the OS named by C<$^O>,
passing in any extra arguments into the C<new> constructor for the specific
class.
If no perl module is found under the appropriate file name, C<undef> is
returned. If any other error occurs while loading or constructing the
instance, the exception is thrown as normal.
Typically a process exporter should support the following named arguments:
=over
=item prefix => STR
A prefix to prepend on all the exported variable names. If not provided, the
default should be C<"process">.
=item labels => ARRAY
Additional labels to set on exported variables. If not provided, no extra
labels will be set.
=back
=cut
sub new
{
my $class = shift;
$class->for_OS( $^O, @_ );
}
=head2 for_OS
$collector = Net::Prometheus::ProcessCollector->for_OS( $os, @args );
Attempts to construct a new process collector for the named OS. Except under
especially-exceptional circumstances, you don't want to call this method.
Call L</new> instead.
=cut
sub for_OS
{
shift; # class
my ( $os, @args ) = @_;
my $pkg = __PACKAGE__ . "::$os";
( my $file = "$pkg.pm" ) =~ s{::}{/}g;
if( !eval { require $file } ) {
return if $@ =~ m/^Can't locate \Q$file\E in \@INC/;
die $@;
}
return $pkg->new( @args );
}
# Methods for subclasses
sub __new
{
my $class = shift;
my %args = @_;
return bless {
prefix => $args{prefix} || "process",
labels => $args{labels} || [],
}, $class;
}
sub _make_metric
{
my $self = shift;
my ( $varname, $value, $type, $help ) = @_;
my $prefix = $self->{prefix};
return MetricSamples( "${prefix}_$varname", $type, $help,
[ Sample( "${prefix}_$varname", $self->{labels}, $value ) ] );
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|