line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Statsd; |
2
|
|
|
|
|
|
|
$Mojolicious::Plugin::Statsd::VERSION = '0.03'; |
3
|
2
|
|
|
2
|
|
1518
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
16
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
425
|
use Mojo::Loader; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
86
|
|
6
|
2
|
|
|
2
|
|
11
|
use Time::HiRes qw(gettimeofday tv_interval); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
17
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has adapter => undef; |
9
|
|
|
|
|
|
|
has prefix => ''; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
6
|
|
|
6
|
1
|
37203
|
my ($self, $app, $conf) = @_; |
13
|
|
|
|
|
|
|
|
14
|
6
|
|
100
|
|
|
60
|
$self->_load_adapter(($conf->{adapter} // 'Statsd'), $conf); |
15
|
|
|
|
|
|
|
|
16
|
6
|
|
66
|
|
|
118
|
$self->{prefix} = $conf->{prefix} // $app->moniker . q[.]; |
17
|
|
|
|
|
|
|
|
18
|
6
|
|
100
|
6
|
|
255
|
$app->helper(($conf->{helper} // 'stats') => sub {$self}); |
|
6
|
|
|
|
|
7106
|
|
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _load_adapter { |
22
|
6
|
|
|
6
|
|
16
|
my ($self, $adapter, $conf) = @_; |
23
|
|
|
|
|
|
|
|
24
|
6
|
100
|
|
|
|
24
|
return $self->adapter($adapter) if ref $adapter; |
25
|
|
|
|
|
|
|
|
26
|
5
|
|
|
|
|
39
|
my $class = sprintf('%s::Adapter::%s', ref $self, $adapter); |
27
|
5
|
|
|
|
|
17
|
my $err = Mojo::Loader::load_class $class; |
28
|
|
|
|
|
|
|
|
29
|
5
|
50
|
|
|
|
89
|
if (ref $err) { |
30
|
0
|
|
|
|
|
0
|
die "Loading adapter $class failed: $err"; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
5
|
|
|
|
|
47
|
$self->adapter($class->new(%$conf)); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub with_prefix { |
37
|
2
|
|
|
2
|
1
|
1796
|
my ($self, $add_prefix) = @_; |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
14
|
(ref $self)->new(%$self, prefix => $self->prefix . $add_prefix); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub counter { |
43
|
5
|
|
|
5
|
1
|
15
|
my ($self, $name, @args) = @_; |
44
|
|
|
|
|
|
|
|
45
|
5
|
|
|
|
|
17
|
$self->adapter->counter($self->_prepare_names($name), @args); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub increment { |
49
|
2
|
|
|
2
|
1
|
378
|
(shift)->counter(shift, 1, shift); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub decrement { |
53
|
1
|
|
|
1
|
1
|
5
|
(shift)->counter(shift, -1, shift); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub timing { |
57
|
3
|
|
|
3
|
1
|
11
|
my ($self, $name, @args) = @_; |
58
|
|
|
|
|
|
|
|
59
|
3
|
50
|
|
|
|
12
|
my ($time, $sample_rate) = ref $args[1] ? reverse(@args) : @args; |
60
|
|
|
|
|
|
|
|
61
|
3
|
100
|
|
|
|
11
|
if (ref $time eq 'CODE') { |
62
|
1
|
|
|
|
|
7
|
my @start = gettimeofday(); |
63
|
1
|
|
|
|
|
4
|
$time->(); |
64
|
1
|
|
|
|
|
128
|
$time = int(tv_interval(\@start) * 1000); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
3
|
|
|
|
|
33
|
$self->adapter->timing($self->_prepare_names($name), $time, $sample_rate); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub gauge { |
71
|
0
|
|
|
0
|
1
|
0
|
my ($self, $name, $value) = @_; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
$self->adapter->gauge($self->_prepare_names($name), $value); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub set_add { |
77
|
0
|
|
|
0
|
1
|
0
|
my ($self, $name, @values) = @_; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
0
|
$self->adapter->set_add($self->_prepare_names($name), @values); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _prepare_names { |
83
|
8
|
|
|
8
|
|
48
|
my ($self, $names) = @_; |
84
|
|
|
|
|
|
|
|
85
|
8
|
100
|
|
|
|
27
|
return [map { $self->prefix . $_ } ref($names) ? @$names : $names]; |
|
9
|
|
|
|
|
28
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
__END__ |