| 1 | package Net::Graphite; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Errno qw(EINTR); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp qw/confess/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Scalar::Util qw/reftype/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | $Net::Graphite::VERSION = '0.18'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | our $TEST = 0; # if true, don't send anything to graphite | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||
| 14 | 6 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | my %args = @_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 6 | return bless { | |||||||||||||||||||||||||||
| 18 | host => '127.0.0.1', | ||||||||||||||||||||||||||||
| 19 | port => 2003, | ||||||||||||||||||||||||||||
| 20 | fire_and_forget => 0, | ||||||||||||||||||||||||||||
| 21 | return_connect_error => 0, | ||||||||||||||||||||||||||||
| 22 | proto => 'tcp', | ||||||||||||||||||||||||||||
| 23 | timeout => 1, | ||||||||||||||||||||||||||||
| 24 | # flush_limit | ||||||||||||||||||||||||||||
| 25 | # path | ||||||||||||||||||||||||||||
| 26 | # transformer | ||||||||||||||||||||||||||||
| 27 | %args, | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # private | ||||||||||||||||||||||||||||
| 30 | _flush_buffer => [], | ||||||||||||||||||||||||||||
| 31 | # _socket | ||||||||||||||||||||||||||||
| 32 | }, $class; | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub send { | ||||||||||||||||||||||||||||
| 36 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=12 SCAR=25.4
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 6 | my $value; | |||||||||||||||||||||||||||
| 38 | 6 | ▶ | $value = shift if @_ % 2; # single value passed in | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 6 | my %args = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 6 | ▶ | if ($args{data}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | my $xform = $args{transformer} || $self->transformer; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'transformer'} || $self->transformer
MC/DC0%0/2
$args{'transformer'} || $self->transformer
$args{'transformer'}
$self->transformer
Truth table33%1/3
A || B
A= $args{'transformer'}B= $self->transformer
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | if ($xform) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | # FIXME | ||||||||||||||||||||||||||||
| 45 | 0 | $self->{_flush_buffer} = [ $xform->($args{data}) ]; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | 3 | ▶ | if (ref $args{data}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 2 | my $reftype = reftype $args{data}; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # default transformers | ||||||||||||||||||||||||||||
| 52 | 2 | ▶ | if ($reftype eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | # hash structure from Yves | ||||||||||||||||||||||||||||
| 54 | 2 | ▶ | my $start_path = $args{path} ? $args{path} : $self->path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 2 | foreach my $epoch (sort {$a <=> $b} keys %{ $args{data} }) { | |||||||||||||||||||||||||||
| 56 | 4 | $self->_fill_lines_for_epoch($epoch, $args{data}{$epoch}, $start_path); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | # TODO - not sure what structure is most useful; | ||||||||||||||||||||||||||||
| 60 | # an aref of [$path, $value, $epoch] seems a bit trivial? | ||||||||||||||||||||||||||||
| 61 | # elsif ($reftype eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 62 | # | ||||||||||||||||||||||||||||
| 63 | # } | ||||||||||||||||||||||||||||
| 64 | # TODO | ||||||||||||||||||||||||||||
| 65 | # elsif ($reftype eq 'CODE') { | ||||||||||||||||||||||||||||
| 66 | # my $iter = $args{data}; | ||||||||||||||||||||||||||||
| 67 | # while (my $text = $iter->()) { | ||||||||||||||||||||||||||||
| 68 | # $plaintext .= $text; | ||||||||||||||||||||||||||||
| 69 | # } | ||||||||||||||||||||||||||||
| 70 | # } | ||||||||||||||||||||||||||||
| 71 | # how about sth of DBI? XML? maybe not | ||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||
| 73 | 0 | confess "Arg 'data' passed to send method is a ref but has no transformer"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | else { | ||||||||||||||||||||||||||||
| 77 | # passed plaintext without a transformer | ||||||||||||||||||||||||||||
| 78 | # FIXME | ||||||||||||||||||||||||||||
| 79 | 1 | $self->{_flush_buffer} = [ $args{data} ]; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||
| 84 | 3 | ▶ | $value = $args{value} unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | my $path = $args{path} || $self->path; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args{'path'} || $self->path
MC/DC0%0/2
$args{'path'} || $self->path
$args{'path'}
$self->path
Truth table66%2/3
A || B
A= $args{'path'}B= $self->path
| |||||||||||||||||||||||||||||
| 86 | 3 | ▶ | my $time = $args{time} || time; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args{'time'} || time
MC/DC0%0/2
$args{'time'} || time
$args{'time'}
time
Truth table66%2/3
A || B
A= $args{'time'}B= time
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | $self->{_flush_buffer} = [ "$path $value $time\n" ]; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 6 | $self->flush(); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 6 | return join('', @{ $self->{_flush_buffer} }); # FIXME | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub flush { | ||||||||||||||||||||||||||||
| 97 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
flush: called CC=12 SCAR=42.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | return unless | ||||||||||||||||||||||||||||
| 99 | 6 | ▶ | my $flush_buffer = $self->{_flush_buffer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 6 | ▶ | return unless @$flush_buffer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 6 | ▶ | $self->trace($flush_buffer) if $self->{trace}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 6 | ▶ | unless ($Net::Graphite::TEST) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | # FIXME: need to deal with incompletely-sent metrics | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($self->connect()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | foreach my $buf (@$flush_buffer) { | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | while (length($buf)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | my $res = $self->{_socket}->send($buf); | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (not defined $res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | next if $! == EINTR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | last; # not sure what to do here | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | substr($buf, 0, $res, ''); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if (length($buf) && not $self->{fire_and_forget}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $buf and not $self->{"fire_and_forget"}
MC/DC0%0/2
length $buf and not $self->{"fire_and_forget"}
length $buf
not $self->{"fire_and_forget"}
Truth table0%0/3
A and not(B)
A= length $bufB= not $self->{"fire_and_forget"}
| |||||||||||||||||||||||||||||
| 118 | 0 | confess "Error sending data"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | # I didn't close the socket! | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub _fill_lines_for_epoch { | ||||||||||||||||||||||||||||
| 127 | 28 | ▶ | my ($self, $epoch, $hash, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_fill_lines_for_epoch: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # still in the "branches" | ||||||||||||||||||||||||||||
| 130 | 28 | ▶ | if (ref $hash) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 12 | foreach my $key (sort keys %$hash) { | |||||||||||||||||||||||||||
| 132 | 24 | my $value = $hash->{$key}; | |||||||||||||||||||||||||||
| 133 | 24 | $self->_fill_lines_for_epoch($epoch, $value, "$path.$key"); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | # reached the "leaf" value | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 16 | push @{ $self->{_flush_buffer} }, "$path $hash $epoch\n"; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | sub connect { | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
connect: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | return $self->{_socket} | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if $self->{_socket} && $self->{_socket}->connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"_socket"} and $self->{"_socket"}->connected
MC/DC0%0/2
$self->{"_socket"} and $self->{"_socket"}->connected
$self->{"_socket"}
$self->{"_socket"}->connected
Truth table0%0/3
A and B
A= $self->{"_socket"}B= $self->{"_socket"}->connected
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | $self->{_socket} = IO::Socket::INET->new( | ||||||||||||||||||||||||||||
| 148 | PeerHost => $self->{host}, | ||||||||||||||||||||||||||||
| 149 | PeerPort => $self->{port}, | ||||||||||||||||||||||||||||
| 150 | Proto => $self->{proto}, | ||||||||||||||||||||||||||||
| 151 | Timeout => $self->{timeout}, | ||||||||||||||||||||||||||||
| 152 | 0 | ); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | unless ($self->{_socket}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($self->{return_connect_error}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 156 | # This is probably only used if you call $graphite->connect before ->send | ||||||||||||||||||||||||||||
| 157 | # in order to check if there is a connection; | ||||||||||||||||||||||||||||
| 158 | # otherwise, it'll just "forget" (without even "firing"). | ||||||||||||||||||||||||||||
| 159 | 0 | return; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | elsif (not $self->{fire_and_forget}) { | ||||||||||||||||||||||||||||
| 162 | 0 | confess "Error creating socket: $!"; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 0 | return $self->{_socket}; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # if you need to close/flush for some reason | ||||||||||||||||||||||||||||
| 169 | sub close { | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return unless my $socket = delete $self->{_socket}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | $socket->close(); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | sub trace { | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | my (undef, $val_line) = @_; | ||||||||||||||||||||||||||
|
Subroutine
trace: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 0 | print STDERR $val_line; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | ### mutators | ||||||||||||||||||||||||||||
| 181 | sub flush_limit { | ||||||||||||||||||||||||||||
| 182 | 0 | ▶ | my ($self, $limit) = @_; | ||||||||||||||||||||||||||
|
Subroutine
flush_limit: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | 0 | ▶ | $self->{flush_limit} = $limit if defined $limit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 0 | return $self->{flush_limit}; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | sub path { | ||||||||||||||||||||||||||||
| 187 | 3 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
path: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 188 | 3 | ▶ | $self->{path} = $path if defined $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 3 | return $self->{path}; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | sub transformer { | ||||||||||||||||||||||||||||
| 192 | 3 | ▶ | my ($self, $xform) = @_; | ||||||||||||||||||||||||||
|
Subroutine
transformer: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 3 | ▶ | $self->{transformer} = $xform if defined $xform; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 3 | return $self->{transformer}; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 4 | 1; | |||||||||||||||||||||||||||
| 198 | __END__ | ||||||||||||||||||||||||||||