line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Statsd::Adapter::Statsd; |
2
|
|
|
|
|
|
|
$Mojolicious::Plugin::Statsd::Adapter::Statsd::VERSION = '0.03'; |
3
|
3
|
|
|
3
|
|
286851
|
use Mojo::Base -base; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
26
|
|
4
|
|
|
|
|
|
|
|
5
|
3
|
|
|
3
|
|
1117
|
use IO::Socket::INET (); |
|
3
|
|
|
|
|
15400
|
|
|
3
|
|
|
|
|
5076
|
|
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
|
299
|
my ($self, $names, $time, $sample_rate) = @_; |
23
|
|
|
|
|
|
|
|
24
|
1
|
50
|
50
|
|
|
12
|
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
|
|
|
|
|
7
|
|
31
|
|
|
|
|
|
|
@$names |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub counter { |
36
|
4
|
|
|
4
|
1
|
6691
|
my ($self, $counters, $delta, $sample_rate) = @_; |
37
|
|
|
|
|
|
|
|
38
|
4
|
100
|
100
|
|
|
29
|
if (($sample_rate //= 1) < 1) { |
39
|
1
|
50
|
|
|
|
58
|
return unless rand() <= $sample_rate; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
$self->_send( |
43
|
5
|
|
|
|
|
20
|
map { _sampled($_, $sample_rate) } |
44
|
4
|
|
|
|
|
12
|
map { sprintf '%s:%d|c', $_, $delta } |
|
5
|
|
|
|
|
32
|
|
45
|
|
|
|
|
|
|
@$counters |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub gauge { |
50
|
2
|
|
|
2
|
1
|
1300
|
my ($self, $gauges, $value) = @_; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
$self->_send( |
53
|
2
|
|
|
|
|
7
|
map { sprintf '%s:%s|g', $_, $value } |
|
2
|
|
|
|
|
15
|
|
54
|
|
|
|
|
|
|
@$gauges |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub set_add { |
59
|
1
|
|
|
1
|
1
|
657
|
my ($self, $sets, @values) = @_; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$self->_send( |
62
|
|
|
|
|
|
|
map { |
63
|
1
|
|
|
|
|
11
|
my $set = $_; |
|
1
|
|
|
|
|
11
|
|
64
|
1
|
|
|
|
|
3
|
map { sprintf '%s:%s|s', $set, $_ } @values |
|
3
|
|
|
|
|
16
|
|
65
|
|
|
|
|
|
|
} @$sets |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
6
|
100
|
|
6
|
|
36
|
sub _sampled { $_[1] == 1 ? $_[0] : ($_[0] . '|@' . $_[1]) } |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _send { |
72
|
8
|
|
|
8
|
|
42
|
my ($self) = shift; |
73
|
8
|
|
|
|
|
22
|
my $payload = join("\012", @_); |
74
|
|
|
|
|
|
|
|
75
|
8
|
100
|
50
|
|
|
26
|
($self->socket->send($payload) // -1) == length($payload) |
76
|
|
|
|
|
|
|
or warn "stats: UDP packet may have been truncated ($!)"; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
__END__ |