File Coverage

blib/lib/Monitor/MetricsAPI/Metric/Callback.pm
Criterion Covered Total %
statement 24 27 88.8
branch 1 2 50.0
condition 1 3 33.3
subroutine 8 9 88.8
pod 2 2 100.0
total 36 43 83.7


line stmt bran cond sub pod time code
1 13     13   95 use strict;
  13         21  
  13         506  
2 13     13   78 use warnings;
  13         18  
  13         899  
3              
4             package Monitor::MetricsAPI::Metric::Callback;
5             $Monitor::MetricsAPI::Metric::Callback::VERSION = '0.900';
6 13     13   66 use namespace::autoclean;
  13         27  
  13         112  
7 13     13   1056 use Moose;
  13         41  
  13         88  
8 13     13   85907 use Try::Tiny;
  13         25  
  13         4875  
9              
10             extends 'Monitor::MetricsAPI::Metric';
11              
12             =head1 NAME
13              
14             Monitor::MetricsAPI::Metric::Callback - Callback metric class for Monitor::MetricsAPI
15              
16             =head1 SYNOPSIS
17              
18             use Monitor::MetricsAPI;
19             use DateTime;
20              
21             # Put into scope first so we can capture it as part of the callback sub.
22             my $collector;
23             $collector = Monitor::MetricsAPI->new(
24             metrics => {
25             process => {
26             started => 'timestamp'
27             },
28             messages => {
29             incoming => {
30             total => 'counter',
31             per_min => sub {
32             $collector->metric('messages/incoming/total')->value
33             /
34             DateTime->now()->delta_ms(
35             $collector->metric('process/started')->dt
36             )->in_units('minutes')
37             },
38             }
39             }
40             }
41             );
42              
43             $collector->metric('process/started')->now;
44              
45             =head1 DESCRIPTION
46              
47             Boolean metrics allow you to track the true/false/unknown state of something
48             in your application. All boolean metrics are initialized as unknown and must
49             be explicitly set to either true or false.
50              
51             =cut
52              
53             has 'cb' => (
54             is => 'ro',
55             isa => 'CodeRef',
56             predicate => '_has_cb',
57             writer => '_set_cb',
58             );
59              
60             =head1 METHODS
61              
62             Callback metrics do not provide any additional methods not already offered by
63             L<Monitor::MetricsAPI::Metric>. You may call the set() method at any time to
64             updated the callback function that will be used when the metric is displayed.
65              
66             =cut
67              
68             =head2 callback (sub { ... })
69              
70             Allows you to provide a new subroutine to be invoked for the callback metric.
71              
72             =cut
73              
74             sub callback {
75 1     1 1 2 my ($self, $sub) = @_;
76              
77 1 50 33     11 unless (defined $sub && ref($sub) eq 'CODE') {
78 0         0 warn "must provide subroutine for callback";
79 0         0 return;
80             }
81              
82 1         39 return $self->_set_cb($sub);
83             }
84              
85             =head2 value
86              
87             Overrides the value() method provided by the base Metric class. Invoking this
88             method will run the callback subroutine. For expensive callbacks, you are very
89             strongly advised to consider incorporating some reasonable caching mechanism
90             so that unnecessary computations can be avoided during metrics reporting.
91              
92             =cut
93              
94             sub value {
95 3     3 1 6 my ($self) = @_;
96              
97 3         4 my $v;
98              
99             try {
100 3     3   106 $v = &{$self->cb};
  3         110  
101             } catch {
102 0     0   0 warn "could not invoke callback metric's subroutine";
103 3         27 };
104              
105 3         64 return $v;
106             }
107              
108             =head1 AUTHORS
109              
110             Jon Sime <jonsime@gmail.com>
111              
112             =head1 LICENSE AND COPYRIGHT
113              
114             This software is copyright (c) 2015 by OmniTI Computer Consulting, Inc.
115              
116             This module is free software; you can redistribute it and/or
117             modify it under the same terms as Perl itself. See L<perlartistic>.
118              
119             This program is distributed in the hope that it will be useful,
120             but WITHOUT ANY WARRANTY; without even the implied warranty of
121             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
122              
123             =cut
124              
125             __PACKAGE__->meta->make_immutable;
126             1;