blib/lib/Net/Sentry/Client.pm

filter: Statementstmt 68.1% 45 / 66 Branchbran 50.0% 2 / 4 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 2 totaltotal 52.5% 61 / 116 CC 11 SCAR 34.0
CC
11
Cov
54%
_getMessage
90.0
new
3.4
send
2.0
CRAP 30.0
↑ Summary
1 package Net::Sentry::Client;
2  
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::UUID::MT ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use MIME::Base64 'encode_base64';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Time::HiRes (qw(gettimeofday));
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Digest::HMAC_SHA1 qw( hmac_sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Constructor
21 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod undocumented
22 1 my ( $class, %options ) = @_;
23
24 1 ▶ if (! exists $options{sentry_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"sentry_key"}) 0 1 1
25 0 die "Mandatory paramter 'sentry_key' not defined";
26 }
27
28 1 ▶ if (! exists $options{remote_url}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"remote_url"}) 0 1 1
29 0 die "Mandatory paramter 'remote_url' not defined";
30 }
31  
32 1 my $self = {
33 sentry_version => 0,
34 ua => LWP::UserAgent->new(),
35 %options,
36 };
37  
38 1 bless $self, $class;
39 }
40  
41 # Send our message to the sentry server/servers
42 0 ▶sub send {
Subroutine send: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my ( $self, $params ) = @_;
44
45 0 my $ua = $self->{ua};
46 0 my $uri = $self->{remote_url};
47 0 my $message = $self->_getMessage( $params );
48 0 my $stamp = gettimeofday();
49 0 $stamp = sprintf ( "%.12g", $stamp );
50 0 my %header = $self->_getAuthHeader( $stamp, $message );
51 0 my $request = $ua->post( $uri, %header, Content => $message );
52 0 return $request;
53 }
54  
55 # From outside message, we make a json message for server.
56 0 ▶sub _getMessage {
Subroutine _getMessage: not called CC=9 SCAR=45.0
57 0 my ( $self, $params ) = @_;
58
59 my $data = {
60 message => $params->{message},
61 timestamp => time(),
62 level => $params->{level}||20,
63 message_id => Data::UUID::MT->new->create_hex(),
64 logger => $params->{logger}||'root',
65 view => $params->{view}||undef,
66 server_name => $params->{server_name}||hostname,
67 url => $params->{url}||undef,
68 site => $params->{site}||undef,
69 data => $params->{data}||undef,
70 traceback => $params->{traceback}||undef,
71 0 ▶ };
Condition0%0/17
$params->{'level'} || 20
l!l
00
$params->{'logger'} || 'root'
l!l
00
$params->{'view'} || undef
l!l
00
$params->{'server_name'} || hostname()
l!l&&r!l&&!r
000
$params->{'url'} || undef
l!l
00
$params->{'site'} || undef
l!l
00
$params->{'data'} || undef
l!l
00
$params->{'traceback'} || undef
l!l
00
MC/DC0%0/1
$params->{'level'} || 20
$params->{'level'}
MC/DC0%0/1
$params->{'logger'} || 'root'
$params->{'logger'}
MC/DC0%0/1
$params->{'view'} || undef
$params->{'view'}
MC/DC0%0/2
$params->{'server_name'} || hostname()
$params->{'server_name'} hostname()
MC/DC0%0/1
$params->{'url'} || undef
$params->{'url'}
MC/DC0%0/1
$params->{'site'} || undef
$params->{'site'}
MC/DC0%0/1
$params->{'data'} || undef
$params->{'data'}
MC/DC0%0/1
$params->{'traceback'} || undef
$params->{'traceback'}
Truth table0%0/2
A
A= $params->{'level'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'logger'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'view'}
Aresult
11
00
Truth table0%0/3
A || B
A= $params->{'server_name'}B= hostname()
ABresult
1X1
011
000
Truth table0%0/2
A
A= $params->{'url'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'site'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'data'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'traceback'}
Aresult
11
00
72 0 my $json = JSON->new->utf8(1)->pretty(1)->allow_nonref(1)->encode( $data );
73
74 0 return encode_base64(compress( $json ));
75 }
76  
77 # Make signature which is required for the sentry server
78 0 ▶sub _getSignature {
Subroutine _getSignature: not called CC=1 SCAR=6.9
79 0 my ( $self, $stamp, $message ) = @_;
80 0 return hmac_sha1_hex( "$stamp $message", $self->{sentry_key} );
81 }
82  
83 # Make the specific header
84 0 ▶sub _getAuthHeader {
Subroutine _getAuthHeader: not called CC=1 SCAR=6.9
85 0 my ( $self, $stamp, $message ) = @_;
86 my $header_format = sprintf (
87 "Sentry sentry_signature=%s ,sentry_timestamp=%s ,sentry_client=%s",
88 $self->_getSignature( $stamp, $message ),
89 $stamp,
90 $self->{sentry_version}
91 0 );
92 0 my %header = ( 'Authorization' => $header_format, 'Content-Type' => 'application/octet-stream' );
93
94 0 return %header;
95 }
96  
97 2 1;
98  
99 __END__
↑ Summary