File: Tee.pm

package info (click to toggle)
libmetrics-any-perl 0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 220 kB
  • sloc: perl: 1,295; makefile: 2
file content (88 lines) | stat: -rw-r--r-- 2,137 bytes parent folder | download
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
#  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, 2020 -- leonerd@leonerd.org.uk

package Metrics::Any::Adapter::Tee 0.10;

use v5.14;
use warnings;

=head1 NAME

C<Metrics::Any::Adapter::Tee> - send metrics to multiple adapters

=head1 SYNOPSIS

   use Metrics::Any::Adapter 'Tee',
      "Prometheus",
      [ "File", path => "metrics.log" ],
      "Statsd";

=head1 DESCRIPTION

This L<Metrics::Any> adapter type acts as a container for multiple other
adapters, allowing an application to report its metrics via multiple different
mechanisms at the same time.

=head1 ARGUMENTS

Each value passed in the import list should either be an adapter type string
or an array reference containing the name and additional arguments.

Adapters specified by string are split in the same way as
L<Metrics::Any::Adapter> splits the C<METRICS_ANY_ADAPTER> environment
variable; namely by parsing optional arguments after a colon, separated by
commas or equals signs. E.g.

   "File:path=metrics.log"

would be equivalent to the version given in the synopsis above.

=cut

sub new
{
   my $class = shift;

   my @adapters;
   foreach ( @_ ) {
      # Adapters are probably specified by ARRAYref
      my ( $type, @args ) = ref $_ eq "ARRAY" ? @$_ : Metrics::Any::Adapter->split_type_string( $_ );
      push @adapters, Metrics::Any::Adapter->class_for_type( $type )->new( @args );
   }

   return bless {
      adapters => \@adapters,
   }, $class;
}

# Distribute each method call across all the adapters
foreach my $method (qw(
   make_counter      inc_counter_by
   make_distribution report_distribution
   make_gauge        inc_gauge_by         set_gauge
   make_timer        report_timer
)) {
   my $code = sub {
      my $self = shift;

      my @e;
      foreach my $adapter ( @{ $self->{adapters} } ) {
         defined eval { $adapter->$method( @_ ); 1 } or
            push @e, $@;
      }
      die $e[0] if @e;
   };

   no strict 'refs';
   *$method = $code;
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;