blib/lib/Metrics/Any/Adapter/File.pm

filter: Statementstmt 86.3% 57 / 66 Branchbran 41.6% 5 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 10 totaltotal 73.7% 76 / 103 SCAR 21.5
CC
8
Cov
79%
new
5.6
_key
4.1
_make
2.0
CRAP 8.6
« blib/lib/Metrics/Any/Adapter/Test.pm ↑ Summary Build »
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::File 0.08;
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 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 C<Metrics::Any::Adapter::File> - write metrics to a file
16  
17 =head1 SYNOPSIS
18  
19 use Metrics::Any::Adapter 'File', path => "metrics.log";
20  
21 =head1 DESCRIPTION
22  
23 This L<Metrics::Any> adapter type writes observations of metric values into a
24 file. This may be helpful while debugging or otherwise testing code that
25 reports metrics.
26  
27 For example, by setting the C<METRICS_ANY_ADAPTER> environment variable to
28 configure the adapter, a metric log will be written as a side-effect of
29 running a unit test:
30  
31 $ METRICS_ANY_ADAPTER=File:path=metrics.log perl -Mblib t/01test.t
32  
33 The generated file can then be inspected to see what metric values were
34 reported while the program was running.
35  
36 In particular, specifying the file F</dev/null> allows the full metrics
37 generation path to be tested with the code under test seeing a "real" adapter
38 even though the output goes nowhere.
39  
40 $ METRICS_ANY_ADAPTER=File:path=/dev/null ./Build test
41  
42 Distribution and timing metrics are tracked with a running total and count of
43 observations.
44  
45 =head1 ARGUMENTS
46  
47 The following additional arguments are recognised
48  
49 =head2 path
50  
51 The path to the file to write to.
52  
53 =cut
54  
55 2 my %metrics;
56  
57 sub new
58 {
59 1 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=17.3
Pod undocumented
60 1 my %args = @_;
61  
62 1 my $fh;
63 1 ▶ if( $args{fh} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'fh'}) { } 1 0 1
elsif ($args{'path'}) { } 0 0 0
64 # fh isn't documented but useful for unit testing
65 1 $fh = $args{fh};
66 }
67 elsif( $args{path} ) {
68 0 ▶ open $fh, ">>", $args{path} or die "Cannot open $args{path} for writing - $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">>", $args{"path"} or die qq[Cannot open $args{"path"} for writing - $!\n] 0 0 0
69 }
70 else {
71 0 croak "Require a 'path' argument";
72 }
73  
74 1 $fh->autoflush;
75  
76 1 return bless {
77 __fh => $fh,
78 }, $class;
79 }
80  
81 sub _make
82 {
83 4 ▶ my $self = shift;
Subroutine _make: called CC=2 SCAR=7.0
84 4 my ( $type, $handle, %args ) = @_;
85  
86 4 my $name = $args{name};
87 4 ▶ $name = join "_", @$name if ref $name eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $name eq "ARRAY" 0 4 4
88  
89 $self->{$handle} = {
90 type => $type,
91 name => $name,
92 labels => $args{labels},
93 4 };
94 }
95  
96 sub _key
97 {
98 16 ▶ my $self = shift;
Subroutine _key: called CC=4 SCAR=14.0
99 16 my ( $handle, $suffix, @labelvalues ) = @_;
100  
101 16 my $meta = $self->{$handle};
102  
103 16 my $key = $meta->{name};
104 16 ▶ $key .= $suffix if defined $suffix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $suffix 8 8 16
105  
106 16 ▶ if( my $labels = $meta->{labels} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $labels = $meta->{"labels"}) 0 16 16
107 0 $key .= " $labels->[$_]:$labelvalues[$_]" for 0 .. $#$labels;
108 }
109  
110 16 return $key;
111 }
112  
113 1 ▶sub make_counter { shift->_make( counter => @_ ) }
Subroutine make_counter: called CC=1 SCAR=0.0
Pod undocumented
114  
115 sub inc_counter_by
116 {
117 2 ▶ my $self = shift;
Subroutine inc_counter_by: called CC=1 SCAR=0.0
Pod undocumented
118 2 my ( $handle, $amount, @labelvalues ) = @_;
119 2 my $fh = $self->{__fh};
120  
121 2 my $key = $self->_key( $handle, undef, @labelvalues );
122 2 my $current = $metrics{$key} += $amount;
123  
124 2 printf $fh "METRIC COUNTER %s %+g => %g\n",
125 $key, $amount, $current;
126 }
127  
128 1 ▶sub make_distribution { shift->_make( distribution => @_ ) }
Subroutine make_distribution: called CC=1 SCAR=0.0
Pod undocumented
129  
130 sub report_distribution
131 {
132 2 ▶ my $self = shift;
Subroutine report_distribution: called CC=1 SCAR=0.0
Pod undocumented
133 2 my ( $handle, $amount, @labelvalues ) = @_;
134 2 my $fh = $self->{__fh};
135  
136 2 my $count = $metrics{ $self->_key( $handle, "_count", @labelvalues ) } += 1;
137 2 my $total = $metrics{ $self->_key( $handle, "_total", @labelvalues ) } += $amount;
138  
139 2 printf $fh "METRIC DISTRIBUTION %s +%g => %g/%d [avg=%g]\n",
140 $self->_key( $handle, undef, @labelvalues ), $amount, $total, $count, $total/$count;
141 }
142  
143 1 ▶sub make_gauge { shift->_make( gauge => @_ ) }
Subroutine make_gauge: called CC=1 SCAR=0.0
Pod undocumented
144  
145 sub inc_gauge_by
146 {
147 2 ▶ my $self = shift;
Subroutine inc_gauge_by: called CC=1 SCAR=0.0
Pod undocumented
148 2 my ( $handle, $amount, @labelvalues ) = @_;
149 2 my $fh = $self->{__fh};
150  
151 2 my $key = $self->_key( $handle, undef, @labelvalues );
152 2 my $current = $metrics{$key} += $amount;
153  
154 2 printf $fh "METRIC GAUGE %s %+g => %g\n",
155 $key, $amount, $current;
156 }
157  
158 sub set_gauge_to
159 {
160 0 ▶ my $self = shift;
Subroutine set_gauge_to: not called CC=1 SCAR=6.9
Pod undocumented
161 0 my ( $handle, $amount, @labelvalues ) = @_;
162 0 my $fh = $self->{__fh};
163  
164 0 my $key = $self->_key( $handle, undef, @labelvalues );
165 0 my $current = $metrics{$key} = $amount;
166  
167 0 printf $fh "METRIC GAUGE %s => %g\n",
168 $key, $current;
169 }
170  
171 1 ▶sub make_timer { shift->_make( timer => @_ ) }
Subroutine make_timer: called CC=1 SCAR=0.0
Pod undocumented
172  
173 sub report_timer
174 {
175 2 ▶ my $self = shift;
Subroutine report_timer: called CC=1 SCAR=0.0
Pod undocumented
176 2 my ( $handle, $duration, @labelvalues ) = @_;
177 2 my $fh = $self->{__fh};
178  
179 2 my $count = $metrics{ $self->_key( $handle, "_count", @labelvalues ) } += 1;
180 2 my $total = $metrics{ $self->_key( $handle, "_total", @labelvalues ) } += $duration;
181  
182 2 printf $fh "METRIC TIMER %s +%.3g => %.3g/%d [avg=%g]\n",
183 $self->_key( $handle, undef, @labelvalues ), $duration, $total, $count, $total/$count;
184 }
185  
186 =head1 AUTHOR
187  
188 Paul Evans <leonerd@leonerd.org.uk>
189  
190 =cut
191  
192 2 0x55AA;
« blib/lib/Metrics/Any/Adapter/Test.pm ↑ Summary Build »