File Coverage

blib/lib/AnyEvent/Graphite.pm
Criterion Covered Total %
statement 15 34 44.1
branch 0 4 0.0
condition 0 10 0.0
subroutine 5 10 50.0
pod 0 3 0.0
total 20 61 32.7


line stmt bran cond sub pod time code
1             package AnyEvent::Graphite;
2              
3 1     1   35676 use warnings;
  1         3  
  1         35  
4 1     1   8 use strict;
  1         3  
  1         52  
5              
6             our $VERSION = '0.08';
7              
8 1     1   2654 use AnyEvent;
  1         8210  
  1         37  
9 1     1   1306 use AnyEvent::Socket;
  1         132389  
  1         167  
10 1     1   6943 use AnyEvent::Handle;
  1         10510  
  1         364  
11              
12             sub new {
13 0     0 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   0       my $host = delete $args{host} || '127.0.0.1';
18 0   0       my $port = delete $args{port} || 2003;
19              
20 0           bless {
21             host => $host,
22             port => $port,
23             %args,
24             }, $class;
25             }
26              
27             sub send {
28 0     0 0   my($self, $id, $value, $ts) = @_;
29 0 0 0       return unless (defined($id) && defined($value));
30 0   0       $ts ||= AE::now; # cached time() from the framework
31 0 0         if($self->{conn}) {
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             on_error => sub {
37             # we have no reason to exist w/out a connection.
38 0     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             sub finish {
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     0 0   my($self) = @_;
49             $self->{conn}->on_drain(sub {
50 0     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; # End of AnyEvent::Graphite