| 1 | package ForgeOps::Tracker::EventBuilder; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use POSIX qw(strftime); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use ForgeOps::Tracker::PiiScrubber qw(scrub scrub_string); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use ForgeOps::Tracker::SqlStatement (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use ForgeOps::Tracker::StrictPrivacy (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | my $MAX_FRAMES = 500; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # How many lines of source to grab on either side of the culprit line (see | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # _attach_source_context), and the longest a single captured line is allowed to be before getting | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # truncated: guards against a single pathological minified/generated line ballooning the | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # payload. ForgeOps itself re-truncates on arrival too, the same "don't just trust the SDK" | ||||||||||||||||||||||||||||||||||||||||||
| 16 | # posture $MAX_FRAMES already gets on the server side. | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | my $CONTEXT_LINES = 5; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | my $MAX_CONTEXT_LINE_LENGTH = 500; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # Identifies this client to the server's auto language-detection on the project the event lands | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # in (see Project#note_sdk_platform server-side); matches this repo's own sdks/perl directory | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # name, the same convention every other language's client follows. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | my $SDK_NAME = 'perl'; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 32 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 32 | my ($class, $configuration) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 32 | return bless { configuration => $configuration }, $class; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # build($error, \%context): $error can be: | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # - a blessed exception object exposing ->message (or overloaded stringification) and, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # optionally, ->trace returning a Devel::StackTrace-compatible object (frames() -> | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # filename/line/subroutine): e.g. Throwable::Error, Moo::Exception-based classes. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # - a plain scalar, typically $@ after `die "..."` or `Carp::confess "..."`: Perl appends | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # " at FILE line N." to any die message that doesn't already end in "\n", and Carp::confess | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # appends a full "\tPACKAGE::sub(...) called at FILE line N" chain on top of that; both are | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # parsed below into real backtrace frames rather than left as one opaque string, verified | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # directly against real confess()/die output before relying on the format, not assumed from | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # documentation alone. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # \%request describes the request the error happened in: transaction_name (the same | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # "GET /users/:id" name the request's root span and performance sample use), endpoint (the HTTP | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # method plus the route pattern), and trace_id (the request's 32-character lowercase hex W3C trace | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # id, which is also what links this error to errors other services reported for the same trace). | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # Each is left out of the payload when undef, and all three are undef outside a request. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # With the configuration's strict_privacy on, the finished payload goes through | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # StrictPrivacy::event: the message is replaced by its message_fingerprint (computed from the | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # message as it would otherwise have been sent, so after scrub_pii, which is also what the server | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # fingerprints from a client without strict privacy, and keeps a message holding an email grouping | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # together), and breadcrumbs, endpoint, the SQL statement, source context, the user's | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # email/username and every context/tag key not in privacy_allowed_fields are left out. The user's | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # id is sent only as a keyed hash, and only when privacy_user_key is set. | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 33 | ▶ | sub build { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build: called CC=11 SCAR=24.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 33 | my ($self, $error, $context, $user, $breadcrumbs, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 33 | ▶ | $context ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$context ||= {}
MC/DC100%1/1
$context ||= {}
$context
Truth table100%2/2
A
A= $context
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 33 | my $config = $self->{configuration}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 33 | my ($exception_class, $message, $backtrace) = $self->_analyze($error); | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | my %payload = ( | ||||||||||||||||||||||||||||||||||||||||||
| 62 | exception_class => $exception_class, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | message => $message, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | backtrace => $backtrace, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | occurred_at => strftime('%Y-%m-%dT%H:%M:%SZ', gmtime), | ||||||||||||||||||||||||||||||||||||||||||
| 66 | environment => $config->{environment}, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | release => $config->{release}, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | server_name => $config->{server_name}, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 33 | context => { %$context }, | |||||||||||||||||||||||||||||||||||||||||
| 70 | tags => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | sdk_name => $SDK_NAME, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 33 | ▶ | $payload{user} = { %$user } if $user && %$user; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$user and %$user
MC/DC50%1/2
$user and %$user
$user
%$user
Truth table66%2/3
A and B
A= $userB= %$user
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | # Omitted entirely (never sent as an empty array) when there's nothing to report. | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 33 | ▶ | $payload{breadcrumbs} = [ map { { %$_ } } @$breadcrumbs ] if $breadcrumbs && @$breadcrumbs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$breadcrumbs and @$breadcrumbs
MC/DC100%2/2
$breadcrumbs and @$breadcrumbs
$breadcrumbs
@$breadcrumbs
Truth table100%3/3
A and B
A= $breadcrumbsB= @$breadcrumbs
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 33 | for my $key (qw(transaction_name endpoint trace_id)) { | |||||||||||||||||||||||||||||||||||||||||
| 77 | 99 | ▶ | $payload{$key} = $request->{$key} if $request && defined $request->{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$request and defined $request->{$key}
MC/DC0%0/2
$request and defined $request->{$key}
$request
defined $request->{$key}
Truth table33%1/3
A and B
A= $requestB= defined $request->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 33 | $self->_attach_sql(\%payload, $error); | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 33 | ▶ | my $payload = $config->{scrub_pii} ? $self->_scrub_payload(\%payload) : \%payload; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 33 | ▶ | return $payload unless $config->{strict_privacy}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return ForgeOps::Tracker::StrictPrivacy::event($payload, $config->{privacy_allowed_fields}, $config->{privacy_user_key}); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # See SqlStatement for what's read off the error and how it's masked. The statement itself only | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # goes out when capture_sql_statement is on; the extracted names go out on their own | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # (capture_sql_objects) so an issue can still name the procedure or view involved. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 33 | ▶ | sub _attach_sql { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attach_sql: called CC=8 SCAR=21.3
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 33 | my ($self, $payload, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 33 | my $config = $self->{configuration}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 33 | ▶ | return unless $config->{capture_sql_objects} || $config->{capture_sql_statement}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$config->{"capture_sql_objects"} or $config->{"capture_sql_statement"}
MC/DC50%1/2
$config->{"capture_sql_objects"} or $config->{"capture_sql_statement"}
$config->{"capture_sql_objects"}
$config->{"capture_sql_statement"}
Truth table66%2/3
A or B
A= $config->{"capture_sql_objects"}B= $config->{"capture_sql_statement"}
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 32 | my $masked = ForgeOps::Tracker::SqlStatement::mask(ForgeOps::Tracker::SqlStatement::find_in($error)); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 32 | ▶ | return unless defined $masked; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | my $objects = ForgeOps::Tracker::SqlStatement::objects($masked); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | $payload->{sql_objects} = $objects if $objects && $config->{capture_sql_objects}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$objects and $config->{"capture_sql_objects"}
MC/DC0%0/2
$objects and $config->{"capture_sql_objects"}
$objects
$config->{"capture_sql_objects"}
Truth table33%1/3
A and B
A= $objectsB= $config->{"capture_sql_objects"}
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | $payload->{sql_statement} = $masked if $config->{capture_sql_statement} && !$config->{strict_privacy}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$config->{"capture_sql_statement"} and not $config->{"strict_privacy"}
MC/DC50%1/2
$config->{"capture_sql_statement"} and not $config->{"strict_privacy"}
$config->{"capture_sql_statement"}
not $config->{"strict_privacy"}
Truth table66%2/3
A and not(B)
A= $config->{"capture_sql_statement"}B= not $config->{"strict_privacy"}
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 33 | ▶ | sub _analyze { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_analyze: called CC=4 SCAR=20.3
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 33 | my ($self, $error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if (ref $error && eval { $error->can('message') }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $error and eval {
do {
$error->can("message")
}
}
MC/DC0%0/2
ref $error and eval {
do {
$error->can("message")
}
}
ref $error
eval {
do {
$error->can("message")
}
}
Truth table33%1/3
A and B
A= ref $errorB= eval {
do {
$error->can("message")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $exception_class = ref $error; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $message = $error->message; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | my $backtrace = eval { $error->can('trace') } ? $self->_frames_from_stack_trace($error->trace) : []; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return ($exception_class, $message, $backtrace); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # Plain scalar (the common case: $@). "RuntimeError" mirrors the other SDKs' own fallback | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # exception_class for an error with no more specific type available. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 33 | my $text = "$error"; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 33 | my ($message, $backtrace) = $self->_parse_die_text($text); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 33 | return ('RuntimeError', $message, $backtrace); | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub _frames_from_stack_trace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_frames_from_stack_trace: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my ($self, $trace) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my @frames; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | for my $frame ($trace->frames) { | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | last if @frames >= $MAX_FRAMES; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | push @frames, $self->_frame($frame->filename, $frame->line, $frame->subroutine); | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | return \@frames; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 33 | ▶ | sub _parse_die_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_die_text: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 33 | my ($self, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 33 | my @lines = split /\n/, $text; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 33 | ▶ | return ('', []) unless @lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 33 | my @frames; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 33 | my $message = shift @lines; | |||||||||||||||||||||||||||||||||||||||||
| 137 | # "MESSAGE at FILE line N.": what Perl itself appends to any die string not already ending | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # in "\n", and the first line Carp::confess/croak produce too. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 33 | ▶ | if ($message =~ s/\s+at\s+(\S+)\s+line\s+(\d+)\.\s*$//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 33 | push @frames, $self->_frame($1, $2, undef); | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | # Remaining lines, present only from Carp::confess: "\tPACKAGE::sub(...) called at FILE line N" | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 33 | for my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||
| 145 | 6 | ▶ | last if @frames >= $MAX_FRAMES; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 6 | ▶ | if ($line =~ /^\s*(\S+?)\(.*?\)\s+called\s+at\s+(\S+)\s+line\s+(\d+)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | push @frames, $self->_frame($2, $3, $1); | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 33 | return ($message, \@frames); | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 37 | ▶ | sub _frame { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_frame: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 37 | my ($self, $file, $line, $method) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 37 | ▶ | my $frame = { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||
| 158 | line => defined($line) ? $line + 0 : undef, | ||||||||||||||||||||||||||||||||||||||||||
| 159 | method => $method, | ||||||||||||||||||||||||||||||||||||||||||
| 160 | in_app => $self->_in_app($file), | ||||||||||||||||||||||||||||||||||||||||||
| 161 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 37 | return $self->_attach_source_context($frame); | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 37 | ▶ | sub _in_app { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_in_app: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 37 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 37 | my $root = $self->{configuration}{app_root}; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 37 | ▶ | return 0 unless $file && $root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$file and $root
MC/DC0%0/2
$file and $root
$file
$root
Truth table33%1/3
A and B
A= $fileB= $root
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 37 | ▶ | return 0 unless index($file, $root) == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | # A vendored/system library installed alongside the app's own lib/ directory is never in_app, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # the same exclusion every other SDK applies for its own package-manager directory. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 8 | return $file !~ m{/(?:local|vendor)/lib/perl5/}; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | # Reads a few lines of source straight off disk around the culprit line, at die/confess-time, in | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # the same running process the error came from. Gated on two things: the frame has to be in_app | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # (never a vendored/system library: there'd be nothing meaningful to show, and it's not the host | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # app's own code to begin with), and configuration's capture_source_context has to be true (see | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # Configuration for why it defaults to true and why ForgeOps' own per-project setting, not this | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # flag, is the durable, protected way to turn it off). Best-effort: any file that can't be opened | ||||||||||||||||||||||||||||||||||||||||||
| 181 | # (deleted, permission denied, a path that only ever existed inside a build step and isn't present | ||||||||||||||||||||||||||||||||||||||||||
| 182 | # in this deployment) just means this one frame gets no source context, never a die of its own. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # Never read at all under strict privacy, which never sends it. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 37 | ▶ | sub _attach_source_context { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attach_source_context: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 37 | my ($self, $frame) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 37 | my $config = $self->{configuration}; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 37 | ▶ | return $frame unless $config->{capture_source_context} && $frame->{in_app} && !$config->{strict_privacy}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$config->{"capture_source_context"} and $frame->{"in_app"} and not $config->{"strict_privacy"}
$config->{"capture_source_context"} and $frame->{"in_app"}
MC/DC66%2/3
$config->{"capture_source_context"} and $frame->{"in_app"} and not $config->{"strict_privacy"}
$config->{"capture_source_context"}
$frame->{"in_app"}
not $config->{"strict_privacy"}
Truth table75%3/4
A and B and not(C)
A= $config->{"capture_source_context"}B= $frame->{"in_app"}C= not $config->{"strict_privacy"}
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | 7 | my $lines = $self->_read_source_lines($frame->{file}); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 7 | ▶ | return $frame unless $lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 6 | my $index = $frame->{line} - 1; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 6 | ▶ | return $frame unless $index >= 0 && $index <= $#$lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$index >= 0 and $index <= $lines->$#*
MC/DC0%0/2
$index >= 0 and $index <= $lines->$#*
$index >= 0
$index <= $lines->$#*
Truth table33%1/3
A and B
A= $index >= 0B= $index <= $lines->$#*
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 6 | my $from = $index - $CONTEXT_LINES; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 6 | ▶ | $from = 0 if $from < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 6 | my $to = $index + $CONTEXT_LINES; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 6 | ▶ | $to = $#$lines if $to > $#$lines; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 6 | $frame->{context_line} = _truncate_line($lines->[$index]); | |||||||||||||||||||||||||||||||||||||||||
| 201 | 6 | $frame->{pre_context} = [ map { _truncate_line($_) } @{$lines}[$from .. $index - 1] ]; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 6 | $frame->{post_context} = [ map { _truncate_line($_) } @{$lines}[$index + 1 .. $to] ]; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 6 | return $frame; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | # Isolated into its own method (rather than inlined into _attach_source_context) so tests can | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # monkeypatch it to prove no read is ever attempted when capture_source_context is off. | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 7 | ▶ | sub _read_source_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_source_lines: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 7 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 7 | ▶ | return undef unless defined $file && length $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $file and length $file
MC/DC0%0/2
defined $file and length $file
defined $file
length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 7 | ▶ | open(my $fh, '<', $file) or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 6 | my @lines = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 6 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 6 | chomp @lines; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 6 | return \@lines; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 32 | ▶ | sub _truncate_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_truncate_line: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 32 | my ($line) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 32 | ▶ | return $line if length($line) <= $MAX_CONTEXT_LINE_LENGTH; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 1 | return substr($line, 0, $MAX_CONTEXT_LINE_LENGTH) . '...'; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | # exception_class/occurred_at/environment/release/server_name/sdk_name/user/transaction_name/ | ||||||||||||||||||||||||||||||||||||||||||
| 226 | # endpoint/trace_id are left alone: structured fields this client or the host app sets | ||||||||||||||||||||||||||||||||||||||||||
| 227 | # deliberately, not free text an exception or its context could accidentally spill sensitive data | ||||||||||||||||||||||||||||||||||||||||||
| 228 | # into (endpoint in particular is a declared route pattern, never the literal path). user specifically is a deliberate | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # exemption, not an oversight: scrub_string's own email pattern would otherwise redact the exact | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # thing this field exists to carry (the %scrubbed copy below never touches it either way). | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 32 | ▶ | sub _scrub_payload { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_scrub_payload: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 32 | my ($self, $payload) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 32 | my %scrubbed = %$payload; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 32 | $scrubbed{message} = scrub_string($payload->{message}); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 32 | ▶ | $scrubbed{sql_statement} = scrub_string($payload->{sql_statement}) if defined $payload->{sql_statement}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | $scrubbed{backtrace} = [ | ||||||||||||||||||||||||||||||||||||||||||
| 237 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 36 | my %frame = %$_; | |||||||||||||||||||||||||||||||||||||||||
| 239 | 36 | ▶ | $frame{file} = scrub_string($frame{file}) if defined $frame{file}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 36 | ▶ | $frame{method} = scrub_string($frame{method}) if defined $frame{method}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 36 | ▶ | if (exists $frame{context_line}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 6 | $frame{context_line} = scrub_string($frame{context_line}); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 6 | $frame{pre_context} = [ map { scrub_string($_) } @{ $frame{pre_context} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 6 | $frame{post_context} = [ map { scrub_string($_) } @{ $frame{post_context} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 36 | \%frame; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 32 | } @{ $payload->{backtrace} } | |||||||||||||||||||||||||||||||||||||||||
| 248 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # Breadcrumb message/data are free text the host app or an integration wrote; category, level, | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # and timestamp are structured values set deliberately, the same split as the top-level | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # fields above, so they are left alone. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 32 | ▶ | if ($payload->{breadcrumbs}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | $scrubbed{breadcrumbs} = [ | ||||||||||||||||||||||||||||||||||||||||||
| 254 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | my %crumb = %$_; | |||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | ▶ | $crumb{message} = scrub_string($crumb{message}) if defined $crumb{message}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | $crumb{data} = scrub($crumb{data}); | |||||||||||||||||||||||||||||||||||||||||
| 258 | 2 | \%crumb; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 2 | } @{ $payload->{breadcrumbs} } | |||||||||||||||||||||||||||||||||||||||||
| 260 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 32 | $scrubbed{context} = scrub($payload->{context}); | |||||||||||||||||||||||||||||||||||||||||
| 263 | 32 | $scrubbed{tags} = scrub($payload->{tags}); | |||||||||||||||||||||||||||||||||||||||||
| 264 | 32 | return \%scrubbed; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||