lib/InfluxDB/Writer/FileTailer.pm

filter: Statementstmt 32.5% 53 / 163 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 46.4% 13 / 28 Podpod 0.0% 0 / 8 totaltotal 26.6% 66 / 248 CC 25 SCAR 30.9
CC
25
Cov
25%
is_running
56.0
send
56.0
setup_file_watcher
30.0
CRAP 22.0
« lib/InfluxDB/Writer/RememberingFileTailer.pm ↑ Summary lib/InfluxDB/Writer/AuthHeaderRole.pm »
1 package InfluxDB::Writer::FileTailer;
2  
3 # ABSTRACT: Tail files and send lines to InfluxDB
4 2 our $VERSION = '1.003'; # VERSION
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Async::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IO::Async::FileStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Hijk ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use InfluxDB::LineProtocol qw(line2data data2line);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 with qw(InfluxDB::Writer::AuthHeaderRole);
22  
23 2 has 'dir' => ( is => 'ro', isa => 'Str', required => 1 );
24 2 has 'influx_host' => ( is => 'ro', isa => 'Str', required => 1 );
25 2 has 'influx_port' =>
26 ( is => 'ro', isa => 'Int', default => 8086, required => 1 );
27 2 has 'influx_db' => ( is => 'ro', isa => 'Str', required => 1 );
28  
29 2 has 'flush_size' =>
30 ( is => 'ro', isa => 'Int', required => 1, default => 1000 );
31 2 has 'flush_interval' =>
32 ( is => 'ro', isa => 'Int', required => 1, default => 30 );
33 2 has 'tags' => ( is => 'ro', isa => 'HashRef', predicate => 'has_tags' );
34 0 ▶has '_files' => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 2 has '_loop' => ( is => 'ro', isa => 'IO::Async::Loop', lazy_build => 1 );
36 0 ▶has 'buffer' => ( is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, traits => ['Array'],
Subroutine __ANON__: not called CC=1 SCAR=1.2
37 handles => {
38 buffer_push => 'push',
39 buffer_all => 'elements',
40 buffer_size => 'count',
41 buffer_splice => 'splice',
42 buffer_is_empty => 'is_empty',
43 },
44  
45 );
46  
47 0 ▶sub _build__loop {
Subroutine _build__loop: not called CC=1 SCAR=6.9
48 0 return IO::Async::Loop->new;
49 }
50  
51 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
52 0 my $self = shift;
53  
54 0 ▶ unless ( -d $self->dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->dir) 0 0 0
55 0 croak "Not a directory: " . $self->dir;
56 }
57  
58 0 $log->infof( "Starting %s in directory %s", __PACKAGE__, $self->dir );
59  
60 0 $self->watch_dir;
61  
62 my $dir = IO::Async::File->new(
63 filename => $self->dir,
64 0 ▶ on_mtime_changed => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 $self->watch_dir;
66 },
67 0 );
68  
69 0 $self->_loop->add($dir);
70  
71 my $timer = IO::Async::Timer::Periodic->new( # could be Countdown
72 interval => $self->flush_interval,
73 0 ▶ on_tick => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 $self->send;
75 },
76 0 );
77 0 $timer->start;
78 0 $self->_loop->add($timer);
79  
80 0 $self->_loop->run;
81 }
82  
83  
84 0 ▶sub cleanup_hook {}
Subroutine cleanup_hook: not called CC=1 SCAR=0.0
Pod undocumented
85 0 ▶sub archive_hook {}
Subroutine archive_hook: not called CC=1 SCAR=0.0
Pod undocumented
86  
87 0 ▶sub watch_dir {
Subroutine watch_dir: not called CC=4 SCAR=30.0
Pod undocumented
88 0 my ($self) = @_;
89  
90 0 $log->infof( "Checking for new files to watch in %s", $self->dir );
91 0 opendir( my $dh, $self->dir );
92 0 ▶ while ( my $f = readdir($dh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = readdir $dh)) 0 0 0
93 0 ▶ next unless $f =~ /\.stats$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /\.stats$/ 0 0 0
94 0 ▶ if ( my $watcher =
Branch: 1 branch
BranchTrueFalseTotal
if (my $watcher = $self->setup_file_watcher(catfile($self->dir, $f))) 0 0 0
95 $self->setup_file_watcher( catfile( $self->dir, $f ) ) ) {
96 0 $self->_loop->add($watcher);
97 }
98 }
99 0 closedir($dh);
100 }
101  
102 0 ▶sub is_running {
Subroutine is_running: not called CC=7 SCAR=40.3
Pod undocumented
103 0 my ($self, $pid, $file) = @_;
104  
105 0 my $running = kill('ZERO', $pid);
106 0 ▶ return unless $running;
Branch: 1 branch
BranchTrueFalseTotal
unless $running 0 0 0
107  
108 # This might only work on linux with $proc access
109 0 my $fd_dir = catdir("/", "proc", $pid, "fd");
110 0 ▶ if (-d $fd_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $fd_dir) 0 0 0
111 0 my $abs_file = File::Spec->rel2abs( $file );
112  
113 0 my $found = 0;
114 0 opendir(my $dh, $fd_dir);
115 0 ▶ while ( my $f = readdir($dh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = readdir $dh)) 0 0 0
116 0 $f = catfile($fd_dir, $f);
117  
118 0 ▶ if ( -f $f && -l $f ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $f and -l $f) 0 0 0
Condition0%0/3
-f $f and -l $f
!ll&&!rl&&r
000
MC/DC0%0/2
-f $f and -l $f
-f $f -l $f
Truth table0%0/3
A and B
A= -f $fB= -l $f
ABresult
0X0
100
111
119  
120 0 my $filename = Cwd::abs_path( $f );
121 0 ▶ if ($abs_file eq $filename) { #need to close dir
Branch: 1 branch
BranchTrueFalseTotal
if ($abs_file eq $filename) 0 0 0
122 0 $found = 1;
123 0 last;
124 }
125 }
126 }
127 0 closedir($dh);
128
129 0 return $found;
130 }
131  
132 0 return $running;
133  
134 }
135  
136 0 ▶sub setup_file_watcher {
Subroutine setup_file_watcher: not called CC=5 SCAR=34.0
Pod undocumented
137 0 my ( $self, $file ) = @_;
138  
139 0 $file =~ /(\d+)\.stats$/;
140 0 my $pid = int($1);
141  
142 0 my $is_running = $self->is_running( $pid, $file );
143  
144 0 ▶ if (!$is_running) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_running) 0 0 0
145  
146 0 ▶ if ( my $w = $self->_files->{$file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = $self->_files->{$file}) { } 0 0 0
147 0 $self->_loop->remove($w);
148 0 undef $w;
149 0 delete $self->_files->{$file};
150 0 $log->infof( "Removed watcher for %s because pid %i is not more",
151 $file, $pid );
152  
153 0 $self->archive_hook($file);
154  
155 }
156 else {
157 0 $log->debugf(
158 "Skipping file %s because pid %i seems to be not running.",
159 $file, $pid );
160  
161 0 $self->cleanup_hook($file);
162 }
163 0 return;
164 }
165  
166 0 ▶ if ( $self->_files->{$file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_files->{$file}) 0 0 0
167 0 $log->debugf( "Already watching file %s", $file );
168 0 return;
169 }
170  
171 0 ▶ if ( open( my $fh, "<", $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $file) { } 0 0 0
172 my $filestream = IO::Async::FileStream->new(
173 read_handle => $fh,
174 0 ▶ on_initial => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 0 my ($stream) = @_;
176 0 $stream->seek_to_last("\n"); # TODO remember last position?
177 },
178  
179 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
180 0 my ( $stream, $buffref ) = @_;
181  
182 0 ▶ while ( $$buffref =~ s/^(.*\n)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffref =~ s/^(.*\n)//) 0 0 0
183 0 my $line = $1;
184 0 ▶ if ( $self->has_tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_tags) 0 0 0
185 0 $line = $self->add_tags_to_line($line);
186 }
187 0 $self->buffer_push($line);
188 }
189  
190 0 ▶ if ( $self->buffer_size > $self->flush_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->buffer_size > $self->flush_size) 0 0 0
191 0 $self->send;
192 }
193  
194 0 return 0;
195 },
196 0 );
197 0 $log->infof( "Tailing file %s", $file );
198 0 $self->_files->{$file} = $filestream;
199 0 return $filestream;
200 }
201 else {
202 0 $log->errorf( "Could not open file %s: %s", $file, $! );
203 0 return;
204 }
205 }
206  
207 0 ▶sub send {
Subroutine send: not called CC=7 SCAR=40.3
Pod undocumented
208 0 my ($self) = @_;
209  
210 0 my $current_size = $self->buffer_size;
211 0 ▶ return unless $current_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_size 0 0 0
212  
213 0 my @to_send = $self->buffer_splice(0, $current_size);
214  
215 0 my %args;
216 0 ▶ if ( $self->_with_auth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_with_auth) 0 0 0
217 0 $args{head} = [ "Authorization" => $self->_auth_header ];
218 }
219  
220 0 $log->debugf( "Sending %i lines to influx", scalar @to_send);
221  
222 0 ( my $body = join("\n", @to_send) ) =~ s/\n{2,}/\n/gs;
223  
224 0 my $request_data = {
225 method => "POST",
226 host => $self->influx_host,
227 port => $self->influx_port,
228 path => "/write",
229 query_string => "db=" . $self->influx_db,
230 body => $body,
231 %args,
232 };
233 0 $log->debugf("The Hijk::Request: %s", $request_data);
234 0 my $res = Hijk::request($request_data);
235  
236 0 ▶ if (my $current_error = $res->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current_error = $res->{"error"}) 0 0 0
237  
238 0 my @errs = (qw/
239 CONNECT_TIMEOUT
240 READ_TIMEOUT
241 TIMEOUT
242 CANNOT_RESOLVE
243 REQUEST_SELECT_ERROR
244 REQUEST_WRITE_ERROR
245 REQUEST_ERROR
246 RESPONSE_READ_ERROR
247 RESPONSE_BAD_READ_VALUE
248 RESPONSE_ERROR
249 /);
250  
251 0 my @matches;
252 0 foreach my $err (@errs) {
253 0 my $const = eval "Hijk::Error::" . $err;
254 0 ▶ if ( $current_error & $const ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_error & $const) 0 0 0
255 0 push(@matches, $err);
256 }
257 }
258  
259 0 $log->errorf("Hijk Request Error(s) cannot send %s", join(", ", @matches));
260  
261 0 return;
262
263 }
264 0 ▶ if ( $res->{status} != 204 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} != 204) 0 0 0
265 $log->errorf(
266 "Could not send %i lines to influx: %s, req: %s",
267 scalar @to_send,
268 $res->{body},
269 0 $request_data
270 );
271  
272 0 return;
273 }
274  
275 0 return scalar(@to_send);
276 }
277  
278 0 ▶sub add_tags_to_line {
Subroutine add_tags_to_line: not called CC=2 SCAR=17.9
Pod undocumented
279 0 my ( $self, $line ) = @_;
280  
281 0 my ( $measurement, $values, $tags, $timestamp ) = line2data($line);
282 0 my $combined_tags;
283 0 ▶ if ($tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags) { } 0 0 0
284 0 $combined_tags = { %$tags, %{ $self->tags } };
285 }
286 else {
287 0 $combined_tags = $tags;
288 }
289 0 return data2line( $measurement, $values, $combined_tags, $timestamp );
290 }
291  
292 2 __PACKAGE__->meta->make_immutable;
293 2 1;
294  
295 __END__
« lib/InfluxDB/Writer/RememberingFileTailer.pm ↑ Summary lib/InfluxDB/Writer/AuthHeaderRole.pm »