File Coverage

lib/Measure/Everything/Adapter/InfluxDB/Direct.pm
Criterion Covered Total %
statement 24 41 58.5
branch 0 8 0.0
condition 0 5 0.0
subroutine 8 12 66.6
pod 0 2 0.0
total 32 68 47.0


line stmt bran cond sub pod time code
1             package Measure::Everything::Adapter::InfluxDB::Direct;
2 1     1   430 use strict;
  1         2  
  1         24  
3 1     1   3 use warnings;
  1         0  
  1         39  
4              
5             our $VERSION = '1.000';
6              
7             # ABSTRACT: Send stats directly to InfluxDB via http
8              
9 1     1   10 use base qw(Measure::Everything::Adapter::Base);
  1         1  
  1         470  
10 1     1   566 use InfluxDB::LineProtocol qw(data2line);
  1         3188  
  1         6  
11 1     1   529 use Hijk;
  1         10467  
  1         28  
12 1     1   412 use MIME::Base64 qw/encode_base64/;
  1         492  
  1         52  
13 1     1   389 use Log::Any qw($log);
  1         7677  
  1         3  
14 1     1   3338 use Try::Tiny;
  1         942  
  1         238  
15              
16             sub init {
17 0     0 0   my $self = shift;
18              
19 0 0         die __PACKAGE__.' required param "host" missing!' unless $self->{host};
20 0 0         die __PACKAGE__.' required param "db" missing!' unless $self->{db};
21              
22             my %args = (
23             method => "POST",
24             host => $self->{host},
25             port => $self->{port} || 8086,
26             path => "/write",
27             query_string => "db=" . $self->{db},
28 0   0       );
29              
30 0 0 0       if ($self->{username} && $self->{password}) {
31 0           my $base64 = encode_base64( join( ":", $self->{username}, $self->{password} ) );
32 0           chomp($base64);
33 0           $args{Authorization} = "Basic $base64";
34             }
35              
36 0           $self->{_fixed_args} = \%args;
37             }
38              
39             sub write {
40 0     0 0   my $self = shift;
41 0           my $line = data2line(@_);
42              
43             try {
44             my $res = Hijk::request({
45 0     0     %{ $self->{_fixed_args} },
  0            
46             body => $line,
47             });
48 0 0         if ( $res->{status} != 204 ) {
49 0           $log->warnf("Could not send line %s to influx: %s",$line, $res->{body});
50             }
51             }
52             catch {
53 0     0     $log->errorf("Could not reach influx for line %s : %s",$line, $_);
54             }
55 0           }
56              
57             1;
58              
59             __END__