Any.pm

filter: Statementstmt 100% 22 / 22 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 66.6% 34 / 51 CC 6 SCAR 16.1
CC
6
Cov
74%
import_into
8.3
BEGIN
1.0
BEGIN
1.0
CRAP 5.0
« Any/Collector.pm ↑ Summary Any/Adapter/Test.pm »
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-2021 -- leonerd@leonerd.org.uk
5  
6 package Metrics::Any 0.10;
7  
8 12 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 ▶use Metrics::Any::Collector;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 C<Metrics::Any> - abstract collection of monitoring metrics
16  
17 =head1 SYNOPSIS
18  
19 In a module:
20  
21 use Metrics::Any '$metrics',
22 strict => 0,
23 name_prefix => [ 'my_module_name' ];
24  
25 sub do_thing {
26 $metrics->inc_counter( 'things_done' );
27 }
28  
29 In a program or top-level program-like module:
30  
31 use Metrics::Any::Adapter 'Prometheus';
32  
33 =head1 DESCRIPTION
34  
35 Provides a central location for modules to report monitoring metrics, such as
36 counters of the number of times interesting events have happened, and programs
37 to collect up and send those metrics to monitoring services.
38  
39 Inspired by L<Log::Any>, this module splits the overall problem into two
40 sides. Modules wishing to provide metrics for monitoring purposes can use the
41 C<use Metrics::Any> statement to obtain a I<collector> into which they can
42 report metric events. By default this collector doesn't actually do anything,
43 so modules can easily use it without adding extra specific dependencies for
44 specific reporting.
45  
46 A program using one or more such modules can apply a different policy and
47 request a particular I<adapter> implementation in order to actually report
48 these metrics to some external system, by using the
49 C<use Metrics::Any::Adapter> statement.
50  
51 This separation of concerns allows module authors to write code which will
52 report metrics without needing to care about the exact mechanism of that
53 reporting (as well as to write code which does not itself depend on the code
54 required to perform that reporting).
55  
56 =head2 Future Direction
57  
58 At present this interface is in an early state of experimentation. The API
59 is fairly specifically-shaped for L<Net::Prometheus> at present, but it is
60 hoped with more adapter implementations (such as for statsd or OpenTelemetry)
61 the API shapes can be expanded and made more generic to support a wider
62 variety of reporting mechanisms.
63  
64 As a result, any API details for now should be considered experimental and
65 subject to change in later versions.
66  
67 =cut
68  
69 =head1 USE STATEMENT
70  
71 For a module to use this facility, the C<use> statement importing it should
72 give the name of a variable (as a plain string) to store the collector for
73 that package.
74  
75 use Metrics::Any '$metrics';
76  
77 This variable will be created in the calling package and populated with an
78 instance of L<Metrics::Any::Collector>. The module can then use the collector
79 API to declare new metrics, and eventually report values into them.
80  
81 Note that the variable is created at the package level; any other packages
82 within the same file will not see it and will have to declare their own.
83  
84 =cut
85  
86 sub import
87 14 ▶{
Subroutine import: called CC=1 SCAR=0.0
88 14 my $pkg = shift;
89 14 my $caller = caller;
90 14 $pkg->import_into( $caller, @_ );
91 }
92  
93 12 my %collector_for_package;
94  
95 sub import_into
96 14 ▶{
Subroutine import_into: called CC=6 SCAR=21.2
Pod undocumented
97 14 my ( $pkg, $caller, @args ) = @_;
98  
99 14 ▶ my $varname = $1 and shift @args if @args and $args[0] =~ m/^\$(.*)$/;
Branch: 1 branch
BranchTrueFalseTotal
if @args and $args[0] =~ /^\$(.*)$/u 14 0 14
Condition33%3/9
@args && $args[0] =~ /^\$(.*)$/u && ((my $varname = $1) && shift @args)
!ll&&!rl&&r
0140
@args and $args[0] =~ /^\$(.*)$/u
!ll&&!rl&&r
0014
my $varname = $1 and shift @args
!ll&&!rl&&r
0140
MC/DC0%0/4
@args && $args[0] =~ /^\$(.*)$/u && ((my $varname = $1) && shift @args)
@args $args[0] =~ /^\$(.*)$/u my $varname = $1 shift @args
Truth table0%0/5
A and B && C and D
A= @argsB= $args[0] =~ /^\$(.*)$/uC= my $varname = $1D= shift @args
ABCDresult
0XXX0
10XX0
110X0
11100
11111
100  
101 14 ▶ my $collector = $collector_for_package{$caller} //= Metrics::Any::Collector->new( $caller, @args );
Condition33%1/3
$collector_for_package{$caller} //= "Metrics::Any::Collector"->new($caller, @args)
l!l&&r!l&&!r
0140
MC/DC0%0/2
$collector_for_package{$caller} //= "Metrics::Any::Collector"->new($caller, @args)
$collector_for_package{$caller} "Metrics::Any::Collector"->new($caller, @args)
Truth table33%1/3
A //= B
A= $collector_for_package{$caller}B= "Metrics::Any::Collector"->new($caller, @args)
ABresult
1X1
011
000
102  
103 14 ▶ if( defined $varname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $varname) 14 0 14
104 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
105 14 *{"${caller}::${varname}"} = \$collector;
106 }
107 }
108  
109 =head1 SEE ALSO
110  
111 =over 4
112  
113 =item *
114  
115 The name and overall concept of this module is heavily influenced by a similar
116 module for logging, L<Log::Any>.
117  
118 =item *
119  
120 For reporting metrics to one specific target collector, there are specific
121 modules for systems like F<statsd> (L<Net::Statsd>, L<Net::Async::Statsd>) or
122 F<prometheus> (L<Prometheus::Tiny>, L<Net::Prometheus>).
123  
124 =item *
125  
126 There is also a similar concept in L<Measure::Everything>.
127  
128 =back
129  
130 =cut
131  
132 =head1 AUTHOR
133  
134 Paul Evans <leonerd@leonerd.org.uk>
135  
136 =cut
137  
138 12 0x55AA;
« Any/Collector.pm ↑ Summary Any/Adapter/Test.pm »