| 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 | ▶ | use v5.20; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 12 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 12 | ▶ | use feature qw( signatures ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 12 | ▶ | no warnings qw( experimental::signatures ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 12 | ▶ | use Net::Prometheus::Types qw( MetricSamples Sample ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | C<Net::Prometheus::ProcessCollector> - 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<Net::Prometheus::ProcessCollector::linux> | ||||||||||||||||||||||
| 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<new> constructor for the specific | ||||||||||||||||||||||
| 58 | class. | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | If no perl module is found under the appropriate file name, C<undef> 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 | ▶ | sub new ( $class, @args ) | ||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||
| 84 | 4 | $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</new> instead. | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | 5 | ▶ | sub for_OS ( $class, $os, @args ) | ||||||||||||||||||||
|
Subroutine
for_OS: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||
| 99 | 5 | my $pkg = __PACKAGE__ . "::$os"; | |||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | 5 | ( my $file = "$pkg.pm" ) =~ s{::}{/}g; | |||||||||||||||||||||
| 102 | 5 | ▶ | if( !eval { require $file } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 103 | 1 | ▶ | return if $@ =~ m/^Can't locate \Q$file\E in \@INC/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 104 | 0 | die $@; | |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 4 | return $pkg->new( @args ); | |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | # Methods for subclasses | ||||||||||||||||||||||
| 111 | |||||||||||||||||||||||
| 112 | 6 | ▶ | sub __new ( $class, %args ) | ||||||||||||||||||||
|
Subroutine
__new: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||
| 114 | return bless { | ||||||||||||||||||||||
| 115 | prefix => $args{prefix} || "process", | ||||||||||||||||||||||
| 116 | 6 | ▶ | labels => $args{labels} || [], | ||||||||||||||||||||
|
Condition100%4/4
$args{'prefix'} || 'process'
$args{'labels'} || []
MC/DC100%1/1
$args{'prefix'} || 'process'
$args{'prefix'}
MC/DC100%1/1
$args{'labels'} || []
$args{'labels'}
Truth table100%2/2
A
A= $args{'prefix'}
Truth table100%2/2
A
A= $args{'labels'}
| |||||||||||||||||||||||
| 117 | }, $class; | ||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | 108 | ▶ | sub _make_metric ( $self, $varname, $value, $type, $help ) | ||||||||||||||||||||
|
Subroutine
_make_metric: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||
| 122 | 108 | my $prefix = $self->{prefix}; | |||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | return MetricSamples( "${prefix}_$varname", $type, $help, | ||||||||||||||||||||||
| 125 | 108 | [ Sample( "${prefix}_$varname", $self->{labels}, $value ) ] ); | |||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | =head1 AUTHOR | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | 12 | 0x55AA; | |||||||||||||||||||||