summaryrefslogtreecommitdiffstats
path: root/sentry.pl
blob: 3dbc6f489441fa54cb1c80265c794d285ed5c40f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl -w

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

#
# report errors to sentry
# expects a filename with a Data::Dumper serialised parameters
# called by Bugzilla::Sentry
#

use strict;
use warnings;

BEGIN {
    delete $ENV{SERVER_SOFTWARE};
}

use FindBin qw($Bin);
use lib $Bin;
use lib "$Bin/lib";

use Bugzilla;
use Bugzilla::Constants;
use Fcntl qw(:flock);
use File::Slurp qw(read_file);
use HTTP::Request::Common;
use LWP::UserAgent;
use POSIX qw(nice);
use URI;

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);
nice(19);

exit(1) unless Bugzilla->params->{sentry_uri};
my $uri = URI->new(Bugzilla->params->{sentry_uri});
my $header = build_header($uri);
exit(1) unless $header;

flock(DATA, LOCK_EX);
foreach my $file (glob(bz_locations()->{error_reports} . '/*.dump')) {
    eval {
        send_file($uri, $header, $file);
    };
}

sub build_header {
    my ($uri) = @_;

    # split the sentry uri
    return undef unless $uri->userinfo && $uri->path;
    my ($public_key, $secret_key) = split(/:/, $uri->userinfo);
    $uri->userinfo(undef);
    my $project_id = $uri->path;
    $project_id =~ s/^\///;
    $uri->path("/api/$project_id/store/");

    # build the header
    return {
        'X-Sentry-Auth' => sprintf(
            "Sentry sentry_version=%s, sentry_timestamp=%s, sentry_key=%s, sentry_client=%s, sentry_secret=%s",
            '2.0',
            (time),
            $public_key,
            'bmo/' . BUGZILLA_VERSION,
            $secret_key,
        ),
        'Content-Type' => 'application/json'
    };
}

sub send_file {
    my ($uri, $header, $filename) = @_;
    # read data dump
    my $message = read_file($filename);
    unlink($filename);

    # and post to sentry
    my $request = POST $uri->canonical, %$header, Content => $message;
    my $response = LWP::UserAgent->new(timeout => 10)->request($request);
}

__DATA__
this exists so the flock() code works.
do not remove this data section.