blib/lib/Sentry/Transport/Http.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 90 CC 5 SCAR 24.8
CC
5
Cov
0%
send
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 12.0
1 package Sentry::Transport::Http;
2 use Mojo::Base -base, -signatures;
3  
4 use HTTP::Status qw(:constants);
5 use Mojo::JSON 'encode_json';
6 use Mojo::UserAgent;
7 use Mojo::Util 'dumper';
8 use Readonly;
9 use Sentry::Envelope;
10 use Sentry::Hub;
11 use Sentry::Logger 'logger';
12  
13 Readonly my $SENTRY_API_VERSION => '7';
14  
15 has _http => sub {
16 Mojo::UserAgent->new(request_timeout => 5, connect_timeout => 1);
17 };
18 has _sentry_client => 'perl-sentry/1.0';
19 has _headers => sub ($self) {
20 my @header = (
21 "Sentry sentry_version=$SENTRY_API_VERSION",
22 "sentry_client=" . $self->_sentry_client,
23 'sentry_key=' . $self->dsn->user,
24 );
25  
26 my $pass = $self->dsn->pass;
27 push @header, "sentry_secret=$pass" if $pass;
28  
29 return {
30 'Content-Type' => 'application/json',
31 'X-Sentry-Auth' => join(', ', @header),
32 };
33 };
34 has _sentry_url => sub ($self) {
35 my $dsn = $self->dsn;
36 die 'DSN missing' unless $dsn;
37  
38 return sprintf('%s://%s:%d/api/%d',
39 $dsn->protocol, $dsn->host, $dsn->port, $dsn->project_id);
40 };
41 has dsn => undef;
42  
43 sub send ($self, $payload) {
44 my $is_transaction = ($payload->{type} // '') eq 'transaction';
45 my $endpoint = $is_transaction ? 'envelope' : 'store';
46 my $tx;
47 my $url = $self->_sentry_url . "/$endpoint/";
48  
49 if ($is_transaction) {
50 my $envelope = Sentry::Envelope->new(
51 event_id => $payload->{event_id},
52 body => $payload,
53 );
54 $payload = $envelope->serialize;
55 $tx = $self->_http->post($url => $self->_headers, $payload);
56 } else {
57 $tx = $self->_http->post($url => $self->_headers, json => $payload);
58 }
59  
60 logger->log(
61 sprintf(
62 qq{Sentry request done. Payload: \n<<<<<<<<<<<<<<\n%s\n<<<<<<<<<<<<<<\nCode: %s},
63 $tx->req->body, $tx->res->code // 'ERROR'
64 ),
65 __PACKAGE__
66 );
67  
68 if (!defined $tx->res->code || $tx->res->is_error) {
69 logger->warn('Error: ' . ($tx->res->error // {})->{message});
70 return;
71 }
72  
73 if ($tx->res->code == HTTP_BAD_REQUEST) {
74 logger->error($tx->res->body);
75 }
76  
77 return $tx->res->json;
78 }
79  
80 1;