line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Statsd::Adapter::Statsd; |
2
|
|
|
|
|
|
|
$Mojolicious::Plugin::Statsd::Adapter::Statsd::VERSION = '0.04'; |
3
|
3
|
|
|
3
|
|
215494
|
use Mojo::Base -base; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
20
|
|
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
858
|
use IO::Socket::INET (); |
|
3
|
|
|
|
|
11987
|
|
|
3
|
|
|
|
|
1573
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has socket => sub { |
8
|
|
|
|
|
|
|
my $self = shift; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
IO::Socket::INET->new( |
11
|
|
|
|
|
|
|
Proto => 'udp', |
12
|
|
|
|
|
|
|
PeerAddr => $self->addr, |
13
|
|
|
|
|
|
|
Blocking => 0, |
14
|
|
|
|
|
|
|
) or die "Can't open write socket for stats: $@"; |
15
|
|
|
|
|
|
|
}; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has addr => sub { |
18
|
|
|
|
|
|
|
$ENV{STATSD_ADDR} // '127.0.0.1:8125'; |
19
|
|
|
|
|
|
|
}; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub timing { |
22
|
1
|
|
|
1
|
1
|
212
|
my ($self, $names, $time, $sample_rate) = @_; |
23
|
|
|
|
|
|
|
|
24
|
1
|
50
|
50
|
|
|
9
|
if (($sample_rate //= 1) < 1) { |
25
|
0
|
0
|
|
|
|
0
|
return unless rand() <= $sample_rate; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$self->_send( |
29
|
1
|
|
|
|
|
4
|
map { _sampled($_, $sample_rate) } |
30
|
1
|
|
|
|
|
3
|
map { sprintf '%s:%d|ms', $_, $time } |
|
1
|
|
|
|
|
6
|
|
31
|
|
|
|
|
|
|
@$names |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub counter { |
36
|
4
|
|
|
4
|
1
|
2850
|
my ($self, $counters, $delta, $sample_rate) = @_; |
37
|
|
|
|
|
|
|
|
38
|
4
|
100
|
100
|
|
|
20
|
if (($sample_rate //= 1) < 1) { |
39
|
1
|
50
|
|
|
|
31
|
return unless rand() <= $sample_rate; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$self->_send( |
43
|
5
|
|
|
|
|
11
|
map { _sampled($_, $sample_rate) } |
44
|
4
|
|
|
|
|
10
|
map { sprintf '%s:%d|c', $_, $delta } |
|
5
|
|
|
|
|
23
|
|
45
|
|
|
|
|
|
|
@$counters |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub gauge { |
50
|
2
|
|
|
2
|
1
|
898
|
my ($self, $gauges, $value) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
$self->_send( |
53
|
2
|
|
|
|
|
5
|
map { sprintf '%s:%s|g', $_, $value } |
|
2
|
|
|
|
|
10
|
|
54
|
|
|
|
|
|
|
@$gauges |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub set_add { |
59
|
1
|
|
|
1
|
1
|
405
|
my ($self, $sets, @values) = @_; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$self->_send( |
62
|
|
|
|
|
|
|
map { |
63
|
1
|
|
|
|
|
3
|
my $set = $_; |
|
1
|
|
|
|
|
8
|
|
64
|
1
|
|
|
|
|
3
|
map { sprintf '%s:%s|s', $set, $_ } @values |
|
3
|
|
|
|
|
10
|
|
65
|
|
|
|
|
|
|
} @$sets |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
6
|
100
|
|
6
|
|
27
|
sub _sampled { $_[1] == 1 ? $_[0] : ($_[0] . '|@' . $_[1]) } |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _send { |
72
|
8
|
|
|
8
|
|
15
|
my ($self) = shift; |
73
|
8
|
|
|
|
|
17
|
my $payload = join("\012", @_); |
74
|
|
|
|
|
|
|
|
75
|
8
|
100
|
50
|
|
|
18
|
($self->socket->send($payload) // -1) == length($payload) |
76
|
|
|
|
|
|
|
or warn "stats: UDP packet may have been truncated ($!)"; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
__END__ |