| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$params{'sentry_version'} || 7
$params{'sentry_version'}
Truth table50%1/2
A
A= $params{'sentry_version'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 $_
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | $protocol, $self->{public_key}, $self->{secret_key}, $host_path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ( int( $response->code / 100 ) == 4 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$answer_ref and $answer_ref->{"id"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $params{"message"} and length $params{"message"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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()
$params{'extra'} || {}
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()
Truth table50%1/2
A
A= $params{'extra'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'
$params{"level"} or $self->{"level"}
$params{'platform'} || 'perl'
$params{'culprit'} || ''
{%{$self->{'tags'};}, %{$params{'tags'};}} || {}
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"}
Truth table50%1/2
A
A= $params{'platform'}
Truth table50%1/2
A
A= $params{'culprit'}
Truth table50%1/2
A
A= {%{$self->{'tags'};}, %{$params{'tags'};}}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | for (@INTERFACES) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 22 | ▶ | $data_ref->{$_} = $params{$_} if $params{$_}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | return $data_ref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||