| 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, 2020 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Metrics::Any::Adapter 0.10; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 13 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 13 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 13 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | C<Metrics::Any::Adapter> - set the C<Metrics::Any> adapter for the program | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | In a program or top-level program-like module: | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | use Metrics::Any::Adapter 'Prometheus'; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | The C<use> statement which loads this module sets the adapter module for | ||||||||||||||||||||
| 26 | L<Metrics::Any> to report metrics generated by an modules the program uses. | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | The first value passed should be a string giving the name of an adapter | ||||||||||||||||||||
| 29 | module, which will be expected under the C<Metrics::Any::Adapter::>-prefix. | ||||||||||||||||||||
| 30 | This module will be loaded and set as the adapter to use. Any additional | ||||||||||||||||||||
| 31 | arguments will be passed to the constructor of the adapter instance. | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | use Metrics::Any::Adapter Custom => "another arg"; | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # implies | ||||||||||||||||||||
| 36 | $adapter = Metrics::Any::Adapter::Custom->new( "another arg" ); | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =head1 ENVIRONMENT | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =head2 METRICS_ANY_ADAPTER | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | I<Since version 0.04.> | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | Sets the default adapter type to use if the program has not otherwise | ||||||||||||||||||||
| 45 | requested one. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | Normally this is set to C<Null>, which loads L<Metrics::Any::Adapter::Null>. | ||||||||||||||||||||
| 48 | By overriding this to a different value, a default adapter can be loaded | ||||||||||||||||||||
| 49 | without modifying the program. This may be useful for example, when running | ||||||||||||||||||||
| 50 | unit tests: | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | $ METRICS_ANY_ADAPTER=Stderr ./Build test | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | Additional arguments can be specified after a colon, separated by commas or | ||||||||||||||||||||
| 55 | equals signs. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | $ METRICS_ANY_ADAPTER=File:path=metrics.log ./program.pl | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | Note that if a program requests a specific adapter that will override this | ||||||||||||||||||||
| 60 | variable. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | A limited attempt is made at supporting nested arguments wrapped in square | ||||||||||||||||||||
| 63 | brackets, to allow basic operation of the L<Metrics::Any::Adapter::Tee> | ||||||||||||||||||||
| 64 | adapter via this variable to itself pass arguments into child adapters: | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | $ METRICS_ANY_ADAPTER=Tee:Prometheus,[File:path=metrics.log] perl ... | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | This should be considered a best-effort scenario useful for short-term testing | ||||||||||||||||||||
| 69 | and debugging. For more complex requirements in your script or program, it is | ||||||||||||||||||||
| 70 | better to use the import arguments directly as then any perl data structures | ||||||||||||||||||||
| 71 | can be passed around. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | sub import | ||||||||||||||||||||
| 76 | 23 | ▶ | { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | 23 | my $pkg = shift; | |||||||||||||||||||
| 78 | 23 | my $caller = caller; | |||||||||||||||||||
| 79 | 23 | $pkg->import_into( $caller, @_ ); | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # Class method so Metrics::Any::Adapter::Tee can share it | ||||||||||||||||||||
| 83 | sub split_type_string | ||||||||||||||||||||
| 84 | 4 | ▶ | { | ||||||||||||||||||
|
Subroutine
split_type_string: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 85 | 4 | shift; | |||||||||||||||||||
| 86 | 4 | my ( $str ) = @_; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 4 | my ( $type, $argstr ) = split m/[:,]/, $str, 2; | |||||||||||||||||||
| 89 | 4 | my @args; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 4 | ▶ | while( length $argstr ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 7 | ▶ | if( $argstr =~ m/^\[/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | # Extract the entire contents of the [...] bracket | ||||||||||||||||||||
| 94 | # TODO: Support deeper nesting somehow? Currently this is only used | ||||||||||||||||||||
| 95 | # for using the Tee adapter via the $METRICS_ANY_ADAPTER variable | ||||||||||||||||||||
| 96 | 1 | ▶ | $argstr =~ s/^\[(.*?)\](?:,|=|$)// or | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | croak "Missing close bracket ] in adapter type string"; | ||||||||||||||||||||
| 98 | 1 | push @args, $1; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | else { | ||||||||||||||||||||
| 101 | # All up to the next , = or endofstring | ||||||||||||||||||||
| 102 | 6 | $argstr =~ s/^(.*?)(?:,|=|$)//; | |||||||||||||||||||
| 103 | 6 | push @args, $1; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 4 | return ( $type, @args ); | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | sub class_for_type | ||||||||||||||||||||
| 111 | 13 | ▶ | { | ||||||||||||||||||
|
Subroutine
class_for_type: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 112 | 13 | shift; | |||||||||||||||||||
| 113 | 13 | my ( $type ) = @_; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 13 | my $class = "Metrics::Any::Adapter::$type"; | |||||||||||||||||||
| 116 | 13 | ▶ | unless( $class->can( 'new' ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 7 | ( my $file = "$class.pm" ) =~ s{::}{/}g; | |||||||||||||||||||
| 118 | 7 | require $file; | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | 13 | return $class; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 13 | my $adaptertype = "Null"; | |||||||||||||||||||
| 124 | 13 | my @adapterargs; | |||||||||||||||||||
| 125 | 13 | ▶ | if( my $val = $ENV{METRICS_ANY_ADAPTER} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 0 | ( $adaptertype, @adapterargs ) = __PACKAGE__->split_type_string( $val ); | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | sub import_into | ||||||||||||||||||||
| 130 | 23 | ▶ | { | ||||||||||||||||||
|
Subroutine
import_into: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 131 | 23 | my ( $pkg, $caller, @args ) = @_; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 23 | ▶ | ( $adaptertype, @adapterargs ) = @args if @args; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 13 | my $adapter; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | sub adapter | ||||||||||||||||||||
| 139 | 14 | ▶ | { | ||||||||||||||||||
|
Subroutine
adapter: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 140 | 14 | shift; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 14 | ▶ | return $adapter //= __PACKAGE__->class_for_type( $adaptertype )->new( @adapterargs ); | ||||||||||||||||||
|
Condition66%2/3
$adapter //= __PACKAGE__->class_for_type($adaptertype)->new(@adapterargs)
MC/DC0%0/2
$adapter //= __PACKAGE__->class_for_type($adaptertype)->new(@adapterargs)
$adapter
__PACKAGE__->class_for_type($adaptertype)->new(@adapterargs)
Truth table66%2/3
A //= B
A= $adapterB= __PACKAGE__->class_for_type($adaptertype)->new(@adapterargs)
| |||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =head1 AUTHOR | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 13 | 0x55AA; | |||||||||||||||||||