| 1 | package Linux::GetPidstat::Writer::Mackerel; | ||||||||||||||||||||||||||||
| 2 | 15 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use WebService::Mackerel; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use JSON::XS qw/decode_json encode_json/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new { | ||||||||||||||||||||||||||||
| 12 | 14 | ▶ | my ( $class, %opt ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | my $mackerel = WebService::Mackerel->new( | ||||||||||||||||||||||||||||
| 15 | api_key => $opt{mackerel_api_key}, | ||||||||||||||||||||||||||||
| 16 | service_name => $opt{mackerel_service_name}, | ||||||||||||||||||||||||||||
| 17 | 14 | ); | |||||||||||||||||||||||||||
| 18 | 14 | $opt{mackerel} = $mackerel; | |||||||||||||||||||||||||||
| 19 | 14 | ▶ | $opt{mackerel_metric_key_prefix} = "" unless defined $opt{mackerel_metric_key_prefix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 14 | bless \%opt, $class; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub output { | ||||||||||||||||||||||||||||
| 25 | 7 | ▶ | my ($self, $program_name, $metric_name, $metric) = @_; | ||||||||||||||||||||||||||
|
Subroutine
output: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 7 | my $graph_name = sprintf("custom.%s%s.%s", $self->{mackerel_metric_key_prefix}, $metric_name, $program_name); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 7 | my $metric_type = $self->{mackerel_metric_type}; | |||||||||||||||||||||||||||
| 29 | 7 | ▶ | if ($self->{dry_run}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | printf "(dry_run) mackerel post: type=%s, name=%s, time=%s, metric=%s\n", | ||||||||||||||||||||||||||||
| 31 | 1 | $metric_type, $graph_name, $self->{now}->epoch, $metric; | |||||||||||||||||||||||||||
| 32 | 1 | return; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 6 | my $res = do { | |||||||||||||||||||||||||||
| 36 | 6 | ▶ | if ($metric_type eq "service") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 37 | $self->{mackerel}->post_service_metrics([{ | ||||||||||||||||||||||||||||
| 38 | "name" => $graph_name, | ||||||||||||||||||||||||||||
| 39 | "time" => $self->{now}->epoch, | ||||||||||||||||||||||||||||
| 40 | 4 | "value" => $metric, | |||||||||||||||||||||||||||
| 41 | }]); | ||||||||||||||||||||||||||||
| 42 | } elsif ($metric_type eq "host") { | ||||||||||||||||||||||||||||
| 43 | $self->{mackerel}->post_host_metrics([{ | ||||||||||||||||||||||||||||
| 44 | "hostId" => $self->{mackerel_host_id}, | ||||||||||||||||||||||||||||
| 45 | "name" => $graph_name, | ||||||||||||||||||||||||||||
| 46 | "time" => $self->{now}->epoch, | ||||||||||||||||||||||||||||
| 47 | 1 | "value" => $metric, | |||||||||||||||||||||||||||
| 48 | }]); | ||||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||
| 50 | 1 | croak "Invalid metric type of mackerel: type=$metric_type"; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | }; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | my $content = eval { decode_json $res; }; | |||||||||||||||||||||||||||
| 55 | 5 | ▶ | if (chomp $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | carp "Failed mackerel post $metric_type metrics: err=$@, res=$res"; | |||||||||||||||||||||||||||
| 57 | 1 | return; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | my $is_success = $content->{success} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$content->{'success'} || 0
MC/DC100%1/1
$content->{'success'} || 0
$content->{'success'}
Truth table100%2/2
A
A= $content->{'success'}
| |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | if ($is_success != JSON::true or $content->{error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$is_success != JSON::true() or $content->{"error"}
MC/DC50%1/2
$is_success != JSON::true() or $content->{"error"}
$is_success != JSON::true()
$content->{"error"}
Truth table66%2/3
A or B
A= $is_success != JSON::true()B= $content->{"error"}
| |||||||||||||||||||||||||||||
| 62 | 15 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 1 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 64 | 1 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||
| 65 | 1 | carp "Failed mackerel post $metric_type metrics: res=" . Data::Dumper::Dumper($content); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub bulk_output { | ||||||||||||||||||||||||||||
| 70 | 6 | ▶ | my ($self, $ret_pidstats) = @_; | ||||||||||||||||||||||||||
|
Subroutine
bulk_output: called CC=11 SCAR=27.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 6 | my $metric_type = $self->{mackerel_metric_type}; | |||||||||||||||||||||||||||
| 73 | 6 | my @request_stuffs; | |||||||||||||||||||||||||||
| 74 | # ex. backup_mysql => { cpu => 21.0 } | ||||||||||||||||||||||||||||
| 75 | 6 | ▶ | while (my ($program_name, $s) = each %$ret_pidstats) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 12 | ▶ | while (my ($metric_name, $metric) = each %$s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 108 | my $graph_name = sprintf("custom.%s%s.%s", $self->{mackerel_metric_key_prefix}, $metric_name, $program_name); | |||||||||||||||||||||||||||
| 78 | my %stuff = ( | ||||||||||||||||||||||||||||
| 79 | "name" => $graph_name, | ||||||||||||||||||||||||||||
| 80 | "time" => $self->{now}->epoch, | ||||||||||||||||||||||||||||
| 81 | "value" => $metric, | ||||||||||||||||||||||||||||
| 82 | 108 | ▶ | ($metric_type eq "host" ? (hostId => $self->{mackerel_host_id}) : ()), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | 108 | push @request_stuffs, \%stuff; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 6 | ▶ | if ($self->{dry_run}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | printf "(dry_run) mackerel post: type=%s, time=%s, metric=%s\n", | ||||||||||||||||||||||||||||
| 90 | 2 | $metric_type, $self->{now}->epoch, encode_json(\@request_stuffs); | |||||||||||||||||||||||||||
| 91 | 2 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4 | my $res = do { | |||||||||||||||||||||||||||
| 95 | 4 | ▶ | if ($metric_type eq "service") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 96 | 4 | $self->{mackerel}->post_service_metrics(\@request_stuffs) | |||||||||||||||||||||||||||
| 97 | } elsif ($metric_type eq "host") { | ||||||||||||||||||||||||||||
| 98 | 0 | $self->{mackerel}->post_host_metrics(\@request_stuffs) | |||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 0 | croak "Invalid metric type of mackerel: type=$metric_type"; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | }; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | my $content = eval { decode_json $res; }; | |||||||||||||||||||||||||||
| 105 | 4 | ▶ | if (chomp $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | carp "Failed mackerel post $metric_type metrics: err=$@, res=$res"; | |||||||||||||||||||||||||||
| 107 | 0 | return; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | ▶ | my $is_success = $content->{success} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$content->{'success'} || 0
MC/DC0%0/1
$content->{'success'} || 0
$content->{'success'}
Truth table50%1/2
A
A= $content->{'success'}
| |||||||||||||||||||||||||||||
| 111 | 4 | ▶ | if ($is_success != JSON::true or $content->{error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$is_success != JSON::true() or $content->{"error"}
MC/DC0%0/2
$is_success != JSON::true() or $content->{"error"}
$is_success != JSON::true()
$content->{"error"}
Truth table33%1/3
A or B
A= $is_success != JSON::true()B= $content->{"error"}
| |||||||||||||||||||||||||||||
| 112 | 15 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 113 | 0 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||
| 114 | 0 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||||||||
| 115 | 0 | carp "Failed mackerel post $metric_type metrics: res=" . Data::Dumper::Dumper($content); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 15 | 1; | |||||||||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||||||||||