| 1 | package AnyEvent::Graphite; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use AnyEvent::Socket; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use AnyEvent::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 0 | my($class, %args) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # the rest of the args will get passed to the object | ||||||||||||||||||||||||||||
| 16 | # so remove host and port as we'll possibly set them manually here | ||||||||||||||||||||||||||||
| 17 | 0 | ▶ | my $host = delete $args{host} || '127.0.0.1'; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'host'} || '127.0.0.1'
MC/DC0%0/1
delete $args{'host'} || '127.0.0.1'
delete $args{'host'}
Truth table0%0/2
A
A= delete $args{'host'}
| |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | my $port = delete $args{port} || 2003; | ||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'port'} || 2003
MC/DC0%0/1
delete $args{'port'} || 2003
delete $args{'port'}
Truth table0%0/2
A
A= delete $args{'port'}
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | bless { | |||||||||||||||||||||||||||
| 21 | host => $host, | ||||||||||||||||||||||||||||
| 22 | port => $port, | ||||||||||||||||||||||||||||
| 23 | %args, | ||||||||||||||||||||||||||||
| 24 | }, $class; | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | 0 | my($self, $id, $value, $ts) = @_; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | return unless (defined($id) && defined($value)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $id and defined $value
MC/DC0%0/2
defined $id and defined $value
defined $id
defined $value
Truth table0%0/3
A and B
A= defined $idB= defined $value
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $ts ||= AE::now; # cached time() from the framework | ||||||||||||||||||||||||||
|
Condition0%0/3
$ts ||= &AE::now()
MC/DC0%0/2
$ts ||= &AE::now()
$ts
&AE::now()
Truth table0%0/3
A ||= B
A= $tsB= &AE::now()
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if($self->{conn}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $self->{conn}->push_write(join(" ", $id, $value, $ts) . "\n"); | |||||||||||||||||||||||||||
| 33 | } else { | ||||||||||||||||||||||||||||
| 34 | 0 | my $handle; $handle = new AnyEvent::Handle | |||||||||||||||||||||||||||
| 35 | connect => [$self->{host} => $self->{port}], | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | on_error => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | # we have no reason to exist w/out a connection. | ||||||||||||||||||||||||||||
| 38 | 0 | die "Unable to connect to Graphite server at $self->{host}:$self->{port}: $!\n"; | |||||||||||||||||||||||||||
| 39 | 0 | }; | |||||||||||||||||||||||||||
| 40 | 0 | $self->{conn} = $handle; | |||||||||||||||||||||||||||
| 41 | 0 | $self->send($id, $value, $ts); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub finish { | ||||||||||||||||||||||||||
|
Subroutine
finish: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | # you need to let the client actually process the data, so the event loop needs to run | ||||||||||||||||||||||||||||
| 47 | # this lets you let your data get out, but still exit when it's done | ||||||||||||||||||||||||||||
| 48 | 0 | my($self) = @_; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | $self->{conn}->on_drain(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 0 | print "All data transmitted\n"; | |||||||||||||||||||||||||||
| 51 | 0 | exit; | |||||||||||||||||||||||||||
| 52 | 0 | }); | |||||||||||||||||||||||||||
| 53 | 0 | AnyEvent->condvar->recv; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 NAME | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | AnyEvent::Graphite - A non-blocking Graphite (http://graphite.wikidot.com/) client | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head1 VERSION | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Version 0.08 | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | my $graphite = AnyEvent::Graphite->new( | ||||||||||||||||||||||||||||
| 67 | host => '127.0.0.1', | ||||||||||||||||||||||||||||
| 68 | port => '2003', | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | $graphite->send("a.b.c.d", $value, $timestamp); #timestamp is optional | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Joshua Barratt, C<< <josh at mediatemple.net> >> | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 79 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | 1; # End of AnyEvent::Graphite | |||||||||||||||||||||||||||