line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Statsd; |
2
|
|
|
|
|
|
|
$Mojolicious::Plugin::Statsd::VERSION = '0.04'; |
3
|
2
|
|
|
2
|
|
1205
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
12
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
357
|
use Mojo::Loader; |
|
2
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
70
|
|
6
|
2
|
|
|
2
|
|
12
|
use Time::HiRes qw(gettimeofday tv_interval); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
12
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has adapter => undef; |
9
|
|
|
|
|
|
|
has prefix => ''; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub register { |
12
|
6
|
|
|
6
|
1
|
28843
|
my ($self, $app, $conf) = @_; |
13
|
|
|
|
|
|
|
|
14
|
6
|
|
100
|
|
|
43
|
$self->_load_adapter(($conf->{adapter} // 'Statsd'), $conf); |
15
|
|
|
|
|
|
|
|
16
|
6
|
|
66
|
|
|
94
|
$self->{prefix} = $conf->{prefix} // $app->moniker . q[.]; |
17
|
|
|
|
|
|
|
|
18
|
6
|
|
100
|
6
|
|
130
|
$app->helper(($conf->{helper} // 'stats') => sub {$self}); |
|
6
|
|
|
|
|
11378
|
|
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _load_adapter { |
22
|
6
|
|
|
6
|
|
13
|
my ($self, $adapter, $conf) = @_; |
23
|
|
|
|
|
|
|
|
24
|
6
|
100
|
|
|
|
18
|
return $self->adapter($adapter) if ref $adapter; |
25
|
|
|
|
|
|
|
|
26
|
5
|
|
|
|
|
25
|
my $class = sprintf('%s::Adapter::%s', ref $self, $adapter); |
27
|
5
|
|
|
|
|
15
|
my $err = Mojo::Loader::load_class $class; |
28
|
|
|
|
|
|
|
|
29
|
5
|
50
|
|
|
|
66
|
if (ref $err) { |
30
|
0
|
|
|
|
|
0
|
die "Loading adapter $class failed: $err"; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
5
|
|
|
|
|
39
|
$self->adapter($class->new(%$conf)); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub with_prefix { |
37
|
2
|
|
|
2
|
1
|
1501
|
my ($self, $add_prefix) = @_; |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
12
|
(ref $self)->new(%$self, prefix => $self->prefix . $add_prefix); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub counter { |
43
|
5
|
|
|
5
|
1
|
13
|
my ($self, $name, @args) = @_; |
44
|
|
|
|
|
|
|
|
45
|
5
|
|
|
|
|
16
|
$self->adapter->counter($self->_prepare_names($name), @args); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub increment { |
49
|
2
|
|
|
2
|
1
|
333
|
(shift)->counter(shift, 1, shift); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub decrement { |
53
|
1
|
|
|
1
|
1
|
3
|
(shift)->counter(shift, -1, shift); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub timing { |
57
|
3
|
|
|
3
|
1
|
9
|
my ($self, $name, @args) = @_; |
58
|
|
|
|
|
|
|
|
59
|
3
|
50
|
|
|
|
9
|
my ($time, $sample_rate) = ref $args[1] ? reverse(@args) : @args; |
60
|
|
|
|
|
|
|
|
61
|
3
|
100
|
|
|
|
8
|
if (ref $time eq 'CODE') { |
62
|
1
|
|
|
|
|
5
|
my @start = gettimeofday(); |
63
|
1
|
|
|
|
|
3
|
$time->(); |
64
|
1
|
|
|
|
|
106
|
$time = int(tv_interval(\@start) * 1000); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
3
|
|
|
|
|
29
|
$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
|
|
40
|
my ($self, $names) = @_; |
84
|
|
|
|
|
|
|
|
85
|
8
|
100
|
|
|
|
21
|
return [map { $self->prefix . $_ } ref($names) ? @$names : $names]; |
|
9
|
|
|
|
|
23
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
__END__ |