File Coverage

blib/lib/Monitor/MetricsAPI/MetricFactory.pm
Criterion Covered Total %
statement 40 40 100.0
branch 3 6 50.0
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 56 59 94.9


line stmt bran cond sub pod time code
1 13     13   66 use strict;
  13         19  
  13         478  
2 13     13   71 use warnings;
  13         23  
  13         842  
3              
4             package Monitor::MetricsAPI::MetricFactory;
5             $Monitor::MetricsAPI::MetricFactory::VERSION = '0.900';
6 13     13   7036 use Module::Loaded;
  13         8807  
  13         996  
7              
8 13     13   5847 use Monitor::MetricsAPI::Metric;
  13         65  
  13         848  
9              
10 13     13   9083 use Monitor::MetricsAPI::Metric::Boolean;
  13         35  
  13         610  
11 13     13   8773 use Monitor::MetricsAPI::Metric::Callback;
  13         42  
  13         644  
12 13     13   8309 use Monitor::MetricsAPI::Metric::Counter;
  13         40  
  13         614  
13 13     13   8511 use Monitor::MetricsAPI::Metric::Gauge;
  13         45  
  13         648  
14 13     13   8137 use Monitor::MetricsAPI::Metric::List;
  13         54  
  13         641  
15 13     13   9055 use Monitor::MetricsAPI::Metric::String;
  13         42  
  13         618  
16 13     13   7874 use Monitor::MetricsAPI::Metric::Timestamp;
  13         46  
  13         2601  
17              
18             =head1 NAME
19              
20             Monitor::MetricsAPI::MetricFactory - Factory for creating Metric::* objects.
21              
22             =head1 SYNOPSIS
23              
24             You should not interact with this module directly in your code. Please refer to
25             L<Monitor::MetricsAPI> for how to integrate this service with your application.
26              
27             =head1 DESCRIPTION
28              
29             This module provides a factory pattern for creating individual metric objects.
30             The intent is for L<Monitor::MetricsAPI::Collector> to use this when building
31             the collector, as well as any time new metrics are added to an existing
32             collector.
33              
34             =head1 METHODS
35              
36             =head2 create (%options)
37              
38             Accepts a hash of options describing a metric, and returns the appropriate type
39             of metric object. Valid entries in the options hash are:
40              
41             =over
42              
43             =item * name
44              
45             A string containing the full name (including all parent groups) for the metric.
46             As detailed in L<Monitor::MetricsAPI::Tutorial>, full metric names are
47             slash-delimited strings of the form "<group>/<subgroup>/<metric>" and there may
48             be as many or as few subgroups as you choose.
49              
50             Example:
51              
52             process/workers/current_threads
53              
54             =item * type
55              
56             A string naming the type of metric to be created. This will match the last
57             component of the metric class's name, in lowercase. Thus, a metric of the class
58             L<Monitor::MetricsAPI::Metric::Boolean> would have a type of "boolean".
59              
60             =item * callback
61              
62             A subroutine to be invoked every time the metric is included in API output, or
63             when the metric's value() method is called. Relevant only for metrics of type
64             'callback'.
65              
66             =back
67              
68             =cut
69              
70             sub create {
71 17     17 1 69 my ($class, %options) = @_;
72              
73 17 50       57 die "must pass metric options as a hash" unless %options;
74 17 50       61 die "must indicate the type of metric to create" unless exists $options{'type'};
75              
76 17         66 my $type = ucfirst($options{'type'});
77 17         52 my $pkg = "Monitor::MetricsAPI::Metric::$type";
78              
79 17 50       78 die $options{'type'} . " is not a valid metric type" unless is_loaded($pkg);
80              
81 17         1211 return $pkg->new(%options);
82             }
83              
84             =head1 AUTHORS
85              
86             Jon Sime <jonsime@gmail.com>
87              
88             =head1 LICENSE AND COPYRIGHT
89              
90             This software is copyright (c) 2015 by OmniTI Computer Consulting, Inc.
91              
92             This module is free software; you can redistribute it and/or
93             modify it under the same terms as Perl itself. See L<perlartistic>.
94              
95             This program is distributed in the hope that it will be useful,
96             but WITHOUT ANY WARRANTY; without even the implied warranty of
97             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
98              
99             =cut
100              
101             1;