blib/lib/Sentry/Log/Raven.pm

filter: Statementstmt 53.0% 35 / 66 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 78.5% 11 / 14 Podpod 66.6% 2 / 3 totaltotal 39.3% 48 / 122 CC 16 SCAR 37.0
CC
16
Cov
37%
new
72.0
buildMessage
72.0
message
6.0
CRAP 40.5
↑ Summary
1 package Sentry::Log::Raven;
2  
3 =head1 NAME
4  
5 Sentry::Log::Raven - sending exception log messages to Sentry.
6  
7 =cut
8  
9 1 our $VERSION = '1.03';
10  
11  
12 =head1 SYNOPSIS
13  
14  
15 my $raven = Sentry::Log::Raven->new(
16 sentry_public_key => "public",
17 sentry_secret_key => "secret",
18 domain_url => "http(s)://sentry domain",
19 project_id => "sentry project id",
20 sentry_version => 4 # can be omitted
21 ssl_verify => 0 # can be omitted
22  
23 );
24  
25  
26 $raven->message({ message => "Alert!" });
27  
28 =head1 EXPORT
29  
30  
31 =cut
32  
33 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use MIME::Base64 'encode_base64';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Time::HiRes (qw(gettimeofday));
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Mozilla::CA;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 =head4 new
47  
48 Constructor. Use like:
49  
50 my $raven = Sentry::Log::Raven->new(
51 sentry_public_key => "public",
52 sentry_secret_key => "secret",
53 domain_url => "http(s)://sentry domain",
54 project_id => "sentry project id",
55 sentry_version => 4 # can be omitted
56 ssl_verify => 0 # can be omitted
57 );
58  
59 =cut
60 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
61 0 my ( $class, %options ) = @_;
62  
63 0 foreach (qw(sentry_public_key sentry_secret_key domain_url project_id)) {
64 0 ▶ if (!exists $options{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{$_}) 0 0 0
65 0 die "Mandatory paramter '$_' not defined";
66 }
67 }
68  
69 0 my $self = {
70 ua => LWP::UserAgent->new(),
71 %options,
72 };
73  
74 0 ▶ $self->{'ssl_verify'} ||= 0;
Condition0%0/2
$self->{"ssl_verify"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"ssl_verify"} ||= 0
$self->{"ssl_verify"}
Truth table0%0/2
A
A= $self->{"ssl_verify"}
Aresult
11
00
75  
76 0 ▶ if ($self->{domain_url} !~ m/^http/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"domain_url"} =~ /^http/) 0 0 0
77 0 die "Domain url not defined correctly";
78 }
79  
80 0 ▶ if ($self->{domain_url} =~ m/^https/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"domain_url"} =~ /^https/) 0 0 0
81 0 ▶ if ($self->{'ssl_verify'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ssl_verify'} == 1) { } 0 0 0
82 0 $self->{ua}->ssl_opts( SSL_ca_file => Mozilla::CA::SSL_ca_file() );
83 } else {
84 0 $self->{ua}->ssl_opts( verify_hostname => 0 );
85 }
86 }
87  
88 0 ▶ $self->{'sentry_version'} ||= 4;
Condition0%0/2
$self->{"sentry_version"} ||= 4
l!l
00
MC/DC0%0/1
$self->{"sentry_version"} ||= 4
$self->{"sentry_version"}
Truth table0%0/2
A
A= $self->{"sentry_version"}
Aresult
11
00
89  
90 0 bless $self, $class;
91 }
92  
93 =head4 message
94  
95 Send message to Sentry server.
96  
97 $raven->message( {
98 'message' => "Message",
99 'logger' => "Name of the logger", # defult "root"
100 'level' => "Error level", # default 'error'
101 'platform' => "Platform name", # default 'perl',
102 'culprit' => "Module or/and function raised error", # default ""
103 'tags' => "Hashref of tags", # default {}
104 'server_name' => "Server name where error occured", # current host name is default
105 'modules' => "list of relevant modules",
106 'extra' => "extra params described below"
107 } );
108  
109 The structure of 'modules' list is:
110  
111 [
112 {
113 "my.module.name": "1.0"
114 }
115 ]
116  
117 The structure of 'extra' field is:
118  
119 {
120 "my_key" => 1,
121 "some_other_value" => "foo bar"
122 }
123  
124  
125 =cut
126 0 ▶sub message {
Subroutine message: not called CC=2 SCAR=17.9
Pod documented
127 0 my ( $self, $params ) = @_;
128
129 0 my $message = $self->buildMessage( $params );
130 0 my $stamp = gettimeofday();
131 0 $stamp = sprintf ( "%.12g", $stamp );
132  
133 my $header_format = sprintf (
134 "Sentry sentry_version=%s, sentry_timestamp=%s, sentry_key=%s, sentry_client=%s, sentry_secret=%s",
135 $self->{sentry_version},
136 time(),
137 $self->{'sentry_public_key'},
138 "perl_client/0.01",
139 0 $self->{'sentry_secret_key'},
140 );
141 0 my %header = ( 'X-Sentry-Auth' => $header_format );
142  
143 0 my $sentry_url;
144
145 0 ▶ if ($self->{'sentry_version'} > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sentry_version'} > 3) { } 0 0 0
146 0 $sentry_url = $self->{domain_url} . '/api/' . $self->{project_id} . '/store/';
147 } else {
148 0 $sentry_url = $self->{domain_url};
149 }
150  
151 0 my $request = POST($sentry_url, %header, Content => $message);
152 0 my $response = $self->{'ua'}->request( $request );
153
154 0 return $response;
155 }
156  
157  
158 0 ▶sub buildMessage {
Subroutine buildMessage: not called CC=8 SCAR=42.8
Pod undocumented
159 0 my ( $self, $params ) = @_;
160
161 my $data = {
162 'event_id' => sprintf("%x%x%x", time(), time() + int(rand()), time() + int(rand())),
163 'message' => $params->{'message'},
164 'timestamp' => time(),
165 'level' => $params->{'level'} || 'error',
166 'logger' => $params->{'logger'} || 'root',
167 'platform' => $params->{'platform'} || 'perl',
168 'culprit' => $params->{'culprit'} || "",
169 'tags' => $params->{'tags'} || {},
170 'server_name' => $params->{server_name} || hostname,
171 'modules' => $params->{'modules'},
172 0 ▶ 'extra' => $params->{'extra'} || {}
Condition0%0/15
$params->{'level'} || 'error'
l!l
00
$params->{'logger'} || 'root'
l!l
00
$params->{'platform'} || 'perl'
l!l
00
$params->{'culprit'} || ''
l!l
00
$params->{'tags'} || {}
l!l
00
$params->{'server_name'} || hostname()
l!l&&r!l&&!r
000
$params->{'extra'} || {}
l!l
00
MC/DC0%0/1
$params->{'level'} || 'error'
$params->{'level'}
MC/DC0%0/1
$params->{'logger'} || 'root'
$params->{'logger'}
MC/DC0%0/1
$params->{'platform'} || 'perl'
$params->{'platform'}
MC/DC0%0/1
$params->{'culprit'} || ''
$params->{'culprit'}
MC/DC0%0/1
$params->{'tags'} || {}
$params->{'tags'}
MC/DC0%0/2
$params->{'server_name'} || hostname()
$params->{'server_name'} hostname()
MC/DC0%0/1
$params->{'extra'} || {}
$params->{'extra'}
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->{'platform'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'culprit'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'tags'}
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->{'extra'}
Aresult
11
00
173 };
174  
175 0 my $json = JSON->new->utf8(1)->pretty(1)->allow_nonref(1);
176 0 return $json->encode( $data );
177 }
178  
179 1 1;
180  
181 =head1 LICENSE AND COPYRIGHT
182  
183 Copyright (C) 2014 by Enginuity Search Media
184  
185 daniel@theenginuity.com
186  
187 This program is free software; you can redistribute it and/or modify it
188 under the terms of the the Artistic License (2.0). You may obtain a
189 copy of the full license at:
190  
191 L<http://www.perlfoundation.org/artistic_license_2_0>
↑ Summary