| 1 | package Graphite::Enumerator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.14.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Scalar::Util 'reftype'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # Recognized constructor options: | ||||||||||||||||||||||||||||
| 12 | # - host (base URL) | ||||||||||||||||||||||||||||
| 13 | # - basepath (top-level metric to scan) | ||||||||||||||||||||||||||||
| 14 | # - lwp_options (hashref) | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=21.3
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 18 | 1 | ▶ | $args{host} or croak "No host provided"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | $args{host} =~ m{^https?://} or $args{host} = "http://".$args{host}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | $args{host} =~ m{/$} or $args{host} .= '/'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | if (defined $args{basepath}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | $args{basepath} =~ /\.$/ or $args{basepath} .= '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | else { | ||||||||||||||||||||||||||||
| 25 | 0 | $args{basepath} = ''; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | 1 | $args{_finder} = $args{host} . 'metrics/find?format=completer&query='; | |||||||||||||||||||||||||||
| 28 | 1 | ▶ | $args{_ua} = LWP::UserAgent->new( %{ $args{lwp_options} || {} } ); | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'lwp_options'} || {}
MC/DC0%0/1
$args{'lwp_options'} || {}
$args{'lwp_options'}
Truth table50%1/2
A
A= $args{'lwp_options'}
| |||||||||||||||||||||||||||||
| 29 | 1 | bless \%args, $class; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub enumerate { | ||||||||||||||||||||||||||
|
Subroutine
enumerate: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 0 | my ($self, $callback, $path, $level) = @_; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | if (reftype $callback ne "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $callback = [ $callback ]; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $path //= $self->{basepath}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$path //= $self->{"basepath"}
MC/DC0%0/2
$path //= $self->{"basepath"}
$path
$self->{"basepath"}
Truth table0%0/3
A //= B
A= $pathB= $self->{"basepath"}
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | $level //= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$level //= 0
MC/DC0%0/1
$level //= 0
$level
Truth table0%0/2
A
A= $level
| |||||||||||||||||||||||||||||
| 39 | 0 | my $url = $self->{_finder} . $path; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | $url .= '.' if $url !~ /[*.]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $url .= '*' if $url =~ /\.$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | my $res = $self->{_ua}->get($url); | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | if ($res->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | my $completer_answer = eval { decode_json($res->content) }; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (!$completer_answer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | $self->log_warning("URL <$url>: Couldn't decode JSON string: <" . $res->content . ">: $@"); | |||||||||||||||||||||||||||
| 47 | 0 | return 0; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return 0 if !$completer_answer->{metrics}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | my $count = 0; | |||||||||||||||||||||||||||
| 51 | 0 | for my $metric (@{ $completer_answer->{metrics} }) { | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | next if ($callback->[1] && $callback->[1]($metric->{path}, $level)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$callback->[1] and $callback->[1]->($metric->{"path"}, $level)
MC/DC0%0/2
$callback->[1] and $callback->[1]->($metric->{"path"}, $level)
$callback->[1]
$callback->[1]->($metric->{"path"}, $level)
Truth table0%0/3
A and B
A= $callback->[1]B= $callback->[1]->($metric->{"path"}, $level)
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($metric->{is_leaf}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $callback->[0]($metric->{path}, $level); | |||||||||||||||||||||||||||
| 55 | 0 | ++$count; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 0 | $count += $self->enumerate($callback, $metric->{path}, $level + 1); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | return $count; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||
| 64 | 0 | $self->log_warning("Can't get <$url>: " . $res->status_line); | |||||||||||||||||||||||||||
| 65 | 0 | return 0; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub host { | ||||||||||||||||||||||||||
|
Subroutine
host: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 71 | 1 | return $self->{host}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub ua { | ||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 76 | 0 | return $self->{_ua}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub log_message { | ||||||||||||||||||||||||||
|
Subroutine
log_message: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 0 | my ($self, $message) = @_; | |||||||||||||||||||||||||||
| 81 | 0 | print $message, "\n"; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub log_warning { | ||||||||||||||||||||||||||
|
Subroutine
log_warning: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | my ($self, $message) = @_; | |||||||||||||||||||||||||||
| 86 | 0 | warn $message, "\n"; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | 1; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 NAME | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Graphite::Enumerator - Utility module to recursively enumerate graphite metrics | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | my $gren = Graphite::Enumerator->new( | ||||||||||||||||||||||||||||
| 98 | host => 'https://graphite.example.com', | ||||||||||||||||||||||||||||
| 99 | basepath => 'general.metrics', | ||||||||||||||||||||||||||||
| 100 | lwp_options => { | ||||||||||||||||||||||||||||
| 101 | env_proxy => 1, | ||||||||||||||||||||||||||||
| 102 | keep_alive => 1, | ||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||
| 105 | $gren->enumerate(sub { | ||||||||||||||||||||||||||||
| 106 | my ($path) = @_; | ||||||||||||||||||||||||||||
| 107 | print "Found metric $path !\n"; | ||||||||||||||||||||||||||||
| 108 | }); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 METHODS | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 Graphite::Enumerator->new(%args) | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | The constructor recognizes 3 arguments: | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | host => host name (in that case, the protocol defaults to http) or base URL | ||||||||||||||||||||||||||||
| 117 | basepath => top-level metric namespace to scan | ||||||||||||||||||||||||||||
| 118 | lwp_options => hash of options to initialize LWP::UserAgent internally | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 $g->enumerate($coderef) | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 $g->enumerate([ $coderef, $filter_coderef ]) | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Calls C<$coderef> for each metric under the basepath, with two parameters: | ||||||||||||||||||||||||||||
| 125 | 1. the metric name as a string; 2. the depth level of the metric relative | ||||||||||||||||||||||||||||
| 126 | to the base path (starting at 0). | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | If an array reference of 2 coderefs is provided, the second coderef will be | ||||||||||||||||||||||||||||
| 129 | used as an input filter called with the same parameters as above. This will | ||||||||||||||||||||||||||||
| 130 | allow, for instance, to stop recursion on a given path by providing a regex, or | ||||||||||||||||||||||||||||
| 131 | to stop recursion past a certain level. The code should return false to allow | ||||||||||||||||||||||||||||
| 132 | further processing, and true, indicating a match, to prevent further processing | ||||||||||||||||||||||||||||
| 133 | along that path. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | enumerate() returns the number of metrics found (or 0 on error). | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 $g->host | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Returns the host passed to the constructor (with eventually | ||||||||||||||||||||||||||||
| 140 | C<http://> prepended). | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 $g->ua | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Returns the internal LWP::UserAgent object. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 $g->log_message($message) | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Prints the C<$message> to STDOUT. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 $g->log_warning($message) | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Warns about the C<$message>. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 ACKNOWLEDGMENT | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | This module was originally developed for Booking.com. | ||||||||||||||||||||||||||||
| 157 | With approval from Booking.com, this module was generalized | ||||||||||||||||||||||||||||
| 158 | and put on CPAN, for which the author would like to express | ||||||||||||||||||||||||||||
| 159 | his gratitude. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Rafael Garcia-Suarez, E<lt>rgs@consttype.orgE<gt> | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | This code is available under the same license as Perl version 5.10.1 or higher. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | A git repository for this module is available at L<https://github.com/rgs/Graphite-Enumerator>. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||