blib/lib/ForgeOps/Tracker/Integrations/PSGIPerformance.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 47 CC 5 SCAR 27.7
CC
5
Cov
0%
call
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 16.0
1 package ForgeOps::Tracker::Integrations::PSGIPerformance;
2  
3 use strict;
4 use warnings;
5 use parent 'Plack::Middleware';
6 use Time::HiRes ();
7 use ForgeOps::Tracker;
8  
9 # PSGI/Plack middleware that times every request end to end and reports it, bucketed by
10 # transaction name, for a dashboard widget on a project's Performance page (so it can show which
11 # parts of your app are actually slow, not just which ones raise). A separate, independent
12 # middleware from Integrations::PSGI (error reporting), not layered onto it, since performance
13 # tracking runs regardless of whether error reporting is even configured: the same
14 # several-genuinely-independent-mechanisms split every other client's own integrations already
15 # use.
16 #
17 # use Plack::Builder;
18 # builder {
19 # enable '+ForgeOps::Tracker::Integrations::PSGI'; # error reporting
20 # enable '+ForgeOps::Tracker::Integrations::PSGIPerformance'; # performance monitoring
21 # $app;
22 # };
23 #
24 # Wraps the downstream app call in an eval, records the duration either way, then re-raises the
25 # original exception unchanged, the same shape Integrations::PSGI's own call already uses to keep
26 # Plack's own error handling exactly as it would be without this middleware installed.
27 #
28 # Also starts a trace for the request and finishes it here (root span named like the
29 # transaction), so a slow request's own breakdown reaches /spans; see ForgeOps::Tracker::span.
30 # There is no automatic database or HTTP span (no DBI or HTTP::Tiny hook here), so add those by
31 # hand (ForgeOps::Tracker::http_span for an outgoing HTTP call, which also hands it the
32 # traceparent header to send).
33 #
34 # The trace continues the caller's when the request arrived with a usable W3C `traceparent` header
35 # (see ForgeOps::Tracker::TraceParent), and its trace id exists even with track_tracing off, since
36 # every error reported during the request carries it. An error that escapes the app is remembered
37 # with the request's context (ForgeOps::Tracker::snapshot_onto) before the trace is finished, so
38 # Integrations::PSGI, enabled outside this middleware, still reports it with the trace id.
39 #
40 # The transaction name is "<HTTP method> <path>", the raw PATH_INFO, not a matched route pattern:
41 # plain PSGI has no route-matching concept of its own (a bare $env hash) to read one from
42 # generically, the same reasoning Go's own net/http integration documents for itself. A Dancer2
43 # app should use Integrations::Dancer2Performance instead, which reads the matched route's own
44 # pattern and so avoids a distinct id exploding into its own separate transaction. For the same
45 # reason an error reported during a plain PSGI request carries the trace id but no transaction
46 # name or endpoint (a raw path is neither), unless the app names the request itself with
47 # ForgeOps::Tracker::set_request_route.
48 sub call {
49 my ($self, $env) = @_;
50  
51 ForgeOps::Tracker::clear_breadcrumbs();
52 ForgeOps::Tracker::start_trace($env->{HTTP_TRACEPARENT});
53  
54 my $started_at = Time::HiRes::time;
55 my @response = eval { @{ $self->app->($env) } };
56 my $error = $@;
57 my $duration_ms = (Time::HiRes::time - $started_at) * 1000;
58  
59 my $transaction_name = "$env->{REQUEST_METHOD} $env->{PATH_INFO}";
60 ForgeOps::Tracker::record_performance($transaction_name, $duration_ms);
61  
62 # Also an automatic "controller" breadcrumb, recorded on the way out (before an exception is
63 # re-raised below), so it is already in the trail by the time Integrations::PSGI, enabled
64 # outside this middleware as in the synopsis above, reports that exception. Recorded whether or
65 # not the request raised, since eval above catches both.
66 ForgeOps::Tracker::add_breadcrumb(
67 $transaction_name,
68 category => 'controller',
69 level => $error ? 'error' : 'info',
70 data => { $error ? () : (status => $response[0]), path => $env->{PATH_INFO} },
71 );
72  
73 ForgeOps::Tracker::snapshot_onto($error) if $error;
74 ForgeOps::Tracker::finish_trace($transaction_name, $started_at, $duration_ms);
75  
76 die $error if $error;
77 return \@response;
78 }
79  
80 1;