| 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::Tee 0.10; | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | C<Metrics::Any::Adapter::Tee> - send metrics to multiple adapters | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | use Metrics::Any::Adapter 'Tee', | ||||||||||
| 18 | "Prometheus", | ||||||||||
| 19 | [ "File", path => "metrics.log" ], | ||||||||||
| 20 | "Statsd"; | ||||||||||
| 21 | |||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||
| 23 | |||||||||||
| 24 | This L<Metrics::Any> adapter type acts as a container for multiple other | ||||||||||
| 25 | adapters, allowing an application to report its metrics via multiple different | ||||||||||
| 26 | mechanisms at the same time. | ||||||||||
| 27 | |||||||||||
| 28 | =head1 ARGUMENTS | ||||||||||
| 29 | |||||||||||
| 30 | Each value passed in the import list should either be an adapter type string | ||||||||||
| 31 | or an array reference containing the name and additional arguments. | ||||||||||
| 32 | |||||||||||
| 33 | Adapters specified by string are split in the same way as | ||||||||||
| 34 | L<Metrics::Any::Adapter> splits the C<METRICS_ANY_ADAPTER> environment | ||||||||||
| 35 | variable; namely by parsing optional arguments after a colon, separated by | ||||||||||
| 36 | commas or equals signs. E.g. | ||||||||||
| 37 | |||||||||||
| 38 | "File:path=metrics.log" | ||||||||||
| 39 | |||||||||||
| 40 | would be equivalent to the version given in the synopsis above. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | sub new | ||||||||||
| 45 | 1 | ▶ | { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 46 | 1 | my $class = shift; | |||||||||
| 47 | |||||||||||
| 48 | 1 | my @adapters; | |||||||||
| 49 | 1 | foreach ( @_ ) { | |||||||||
| 50 | # Adapters are probably specified by ARRAYref | ||||||||||
| 51 | 2 | ▶ | my ( $type, @args ) = ref $_ eq "ARRAY" ? @$_ : Metrics::Any::Adapter->split_type_string( $_ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 2 | push @adapters, Metrics::Any::Adapter->class_for_type( $type )->new( @args ); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 1 | return bless { | |||||||||
| 56 | adapters => \@adapters, | ||||||||||
| 57 | }, $class; | ||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # Distribute each method call across all the adapters | ||||||||||
| 61 | 2 | foreach my $method (qw( | |||||||||
| 62 | make_counter inc_counter_by | ||||||||||
| 63 | make_distribution report_distribution | ||||||||||
| 64 | make_gauge inc_gauge_by set_gauge | ||||||||||
| 65 | make_timer report_timer | ||||||||||
| 66 | )) { | ||||||||||
| 67 | 8 | ▶ | my $code = sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||
| 68 | 8 | my $self = shift; | |||||||||
| 69 | |||||||||||
| 70 | 8 | my @e; | |||||||||
| 71 | 8 | foreach my $adapter ( @{ $self->{adapters} } ) { | |||||||||
| 72 | 16 | ▶ | defined eval { $adapter->$method( @_ ); 1 } or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | push @e, $@; | ||||||||||
| 74 | } | ||||||||||
| 75 | 8 | ▶ | die $e[0] if @e; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 18 | }; | |||||||||
| 77 | |||||||||||
| 78 | 2 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 18 | *$method = $code; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | =head1 AUTHOR | ||||||||||
| 83 | |||||||||||
| 84 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 85 | |||||||||||
| 86 | =cut | ||||||||||
| 87 | |||||||||||
| 88 | 2 | 0x55AA; | |||||||||