blib/lib/Sentry.pm

filter: Statementstmt 79.6% 51 / 64 Branchbran 28.5% 4 / 14 Conditioncond 37.0% 10 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 85.7% 12 / 14 Podpod 50.0% 1 / 2 totaltotal 57.3% 78 / 136 CC 21 SCAR 26.1
CC
21
Cov
62%
_send
30.0
_build_message
22.6
new
5.6
CRAP 13.6
↑ Summary
1 # ABSTRACT: Yet another lightweight Sentry client
2  
3 package Sentry;
4 1 $Sentry::VERSION = '0.01';
5  
6  
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Base64 'encode_base64';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Class::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 my @LEVELS;
17  
18 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
19 1 @LEVELS = qw( fatal error warning warn info debug );
20 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 for my $level (@LEVELS) {
22 6 *{ __PACKAGE__ . "::$level" } = subname $level =>
23 0 ▶ sub { shift->_send( message => shift, level => $level, @_ ) };
Subroutine fatal: not called CC=1 SCAR=1.2
Pod undocumented
24 }
25 }
26  
27 1 my @INTERFACES = (
28 'exception', 'stacktrace', 'template', 'breadcrumbs',
29 'contexts', 'request', 'threads', 'user',
30 'debug_meta', 'repos', 'sdk'
31 );
32  
33  
34 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.3
Pod documented
35 1 my ( $class, $dsn, %params ) = @_;
36  
37 1 ▶ die 'API key is not defined' unless $dsn;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn 0 1 1
38  
39 my $self = {
40 ua => LWP::UserAgent->new( timeout => 10 ),
41 1 ▶ sentry_version => $params{sentry_version} || 7,
Condition50%1/2
$params{'sentry_version'} || 7
l!l
01
MC/DC0%0/1
$params{'sentry_version'} || 7
$params{'sentry_version'}
Truth table50%1/2
A
A= $params{'sentry_version'}
Aresult
11
00
42 %params,
43 };
44  
45 (
46 my $protocol, $self->{public_key}, $self->{secret_key},
47 1 my $host_path,
48 my $project_id
49 )
50 = $dsn =~ m{^ ( https? ) :// ( \w+ ) : ( \w+ ) @ ( .+ ) / ( \d+ ) $}ixaa;
51  
52 die 'Wrong dsn format'
53 5 ▶ if grep { !defined $_ || !length $_ } (
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
005
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
54 1 ▶ $protocol, $self->{public_key}, $self->{secret_key}, $host_path,
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ && length $_;} $protocol, $self->{"public_key"}, $self->{"secret_key"}, $host_path, $project_id 0 1 1
55 $project_id
56 );
57  
58 1 $self->{uri} = "$protocol://$host_path/api/$project_id/store/";
59  
60 1 bless $self, $class;
61 }
62  
63 # Send a message to Sentry server.
64 # Returns the id of inserted message or dies.
65  
66 0 ▶sub _send {
Subroutine _send: not called CC=5 SCAR=34.0
67 0 my ( $self, %params ) = @_;
68  
69 my $auth = sprintf
70 'Sentry sentry_version=%s, sentry_timestamp=%s, sentry_key=%s, sentry_client=%s, sentry_secret=%s',
71 $self->{sentry_version},
72 time(),
73 $self->{public_key},
74 __PACKAGE__,
75 $self->{secret_key},
76 0 ;
77  
78 0 my $message = $self->_build_message(%params);
79 0 $message = encode_json $message;
80 my $response = $self->{ua}->post(
81 $self->{uri},
82 0 'X-Sentry-Auth' => $auth,
83 'Content-Type' => 'application/json',
84 Content => encode_base64($message),
85 );
86  
87 0 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
88 0 ▶ if ( int( $response->code / 100 ) == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $response->code / 100 == 4) 0 0 0
89 0 die $response->status_line . ': ' . $response->decoded_content;
90 }
91  
92 0 die $response->status_line;
93 }
94  
95 0 my $answer_ref = decode_json $response->decoded_content;
96  
97 0 ▶ die 'Wrong answer format' unless $answer_ref && $answer_ref->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $answer_ref and $answer_ref->{"id"} 0 0 0
Condition0%0/3
$answer_ref and $answer_ref->{"id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$answer_ref and $answer_ref->{"id"}
$answer_ref $answer_ref->{"id"}
Truth table0%0/3
A and B
A= $answer_refB= $answer_ref->{"id"}
ABresult
0X0
100
111
98  
99 0 return $answer_ref->{id};
100 }
101  
102 2 ▶sub _build_message {
Subroutine _build_message: called CC=12 SCAR=31.2
103 2 my ( $self, %params ) = @_;
104  
105 die 'No message given'
106 2 ▶ unless defined $params{message} && length $params{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"message"} and length $params{"message"} 0 2 2
Condition33%1/3
defined $params{"message"} and length $params{"message"}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $params{"message"} and length $params{"message"}
defined $params{"message"} length $params{"message"}
Truth table33%1/3
A and B
A= defined $params{"message"}B= length $params{"message"}
ABresult
0X0
100
111
107  
108 my $data_ref = {
109 message => $params{message},
110 timestamp => strftime( '%FT%X.000000Z', gmtime time ),
111 level => $params{level} || $self->{level} || 'info',
112 logger => $params{logger},
113 platform => $params{platform} || 'perl',
114 culprit => $params{culprit} || '',
115 2 ▶ tags => { %{ $self->{tags} }, %{ $params{tags} } } || {},
Condition40%2/5
$params{'server_name'} || hostname()
l!l&&r!l&&!r
020
$params{'extra'} || {}
l!l
02
MC/DC0%0/2
$params{'server_name'} || hostname()
$params{'server_name'} hostname()
MC/DC0%0/1
$params{'extra'} || {}
$params{'extra'}
Truth table33%1/3
A || B
A= $params{'server_name'}B= hostname()
ABresult
1X1
011
000
Truth table50%1/2
A
A= $params{'extra'}
Aresult
11
00
116 server_name => $params{server_name} || hostname(),
117 modules => $params{modules},
118 extra => $params{extra} || {},
119 2 ▶ };
Condition45%5/11
$params{'level'} || $self->{'level'} || 'info'
l!l
02
$params{"level"} or $self->{"level"}
l!l&&r!l&&!r
002
$params{'platform'} || 'perl'
l!l
02
$params{'culprit'} || ''
l!l
02
{%{$self->{'tags'};}, %{$params{'tags'};}} || {}
l!l
20
MC/DC0%0/2
$params{'level'} || $self->{'level'} || 'info'
$params{"level"} $self->{"level"}
MC/DC0%0/1
$params{'platform'} || 'perl'
$params{'platform'}
MC/DC0%0/1
$params{'culprit'} || ''
$params{'culprit'}
MC/DC0%0/1
{%{$self->{'tags'};}, %{$params{'tags'};}} || {}
{%{$self->{'tags'};}, %{$params{'tags'};}}
Truth table33%1/3
A or B
A= $params{"level"}B= $self->{"level"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $params{'platform'}
Aresult
11
00
Truth table50%1/2
A
A= $params{'culprit'}
Aresult
11
00
Truth table50%1/2
A
A= {%{$self->{'tags'};}, %{$params{'tags'};}}
Aresult
11
00
120  
121 2 for (@INTERFACES) {
122 22 ▶ $data_ref->{$_} = $params{$_} if $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $params{$_} 0 22 22
123 }
124  
125 2 return $data_ref;
126 }
127  
128 1 1;
129  
130 __END__
↑ Summary