| 1 | # | ||
| 2 | # Test statsd backend to write Graphite-like | ||
| 3 | # flushed statistics to a given file. | ||
| 4 | # | ||
| 5 | # To enable this backend, include 'test' in the backends | ||
| 6 | # configuration array: | ||
| 7 | # | ||
| 8 | # "backends": ["test"], | ||
| 9 | # | ||
| 10 | # This backend supports the following config options: | ||
| 11 | # | ||
| 12 | # outputFile: file where to flush statistics (in append mode) | ||
| 13 | # | ||
| 14 | |||
| 15 | package Net::Statsd::Server::Backend::Test; | ||
| 16 | { | ||
| 17 | $Net::Statsd::Server::Backend::Test::VERSION = '0.19'; | ||
| 18 | } | ||
| 19 | |||
| 20 | use 5.008; | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use base qw(Net::Statsd::Server::Backend::Graphite); | ||
| 24 | |||
| 25 | sub post_stats { | ||
| 26 | my ($self, $stat_list) = @_; | ||
| 27 | |||
| 28 | my $dump_file = $self->{config}->{outputFile}; | ||
| 29 | if (! $dump_file or (-e $dump_file && ! -w $dump_file)) { | ||
| 30 | die "Can't write to dump file '$dump_file': $!"; | ||
| 31 | } | ||
| 32 | |||
| 33 | my $stat_string = $self->stats_to_string($stat_list); | ||
| 34 | |||
| 35 | eval { | ||
| 36 | open my $dump_fh, '>>', $dump_file; | ||
| 37 | say $dump_fh join(" ", "#", time, "stats flush"); | ||
| 38 | say $dump_fh $stat_string; | ||
| 39 | close $dump_fh; | ||
| 40 | $self->{lastFlush} = [Time::HiRes::gettimeofday]; | ||
| 41 | } | ||
| 42 | or do { | ||
| 43 | if ($self->{debug}) { | ||
| 44 | # TODO use logger! | ||
| 45 | warn("Exception while posting stats to file $dump_file: $@"); | ||
| 46 | } | ||
| 47 | $self->{lastException} = [Time::HiRes::gettimeofday]; | ||
| 48 | }; | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | 1; |