| 1 | package ForgeOps::Tracker::Integrations::Dancer2Performance; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Dancer2::Plugin; | ||
| 6 | use Dancer2::Core::Hook; | ||
| 7 | use Time::HiRes (); | ||
| 8 | use ForgeOps::Tracker; | ||
| 9 | |||
| 10 | # Dancer2 plugin that times every request end to end and reports it, bucketed by transaction | ||
| 11 | # name, for a dashboard widget on a project's Performance page. A separate, independent plugin | ||
| 12 | # from Integrations::Dancer2 (error reporting), not layered onto it, for the same reason PSGI's | ||
| 13 | # own two middlewares (Integrations::PSGI and Integrations::PSGIPerformance) are kept apart: | ||
| 14 | # performance tracking runs regardless of whether error reporting is even configured. | ||
| 15 | # | ||
| 16 | # use Dancer2; | ||
| 17 | # use ForgeOps::Tracker::Integrations::Dancer2Performance; # that's it, no further wiring | ||
| 18 | # | ||
| 19 | # Registers a before_request/after_request hook pair (Dancer2's own documented hooks), storing | ||
| 20 | # the start time on the current request between them via Dancer2's own per-request var storage, | ||
| 21 | # rather than a lexical closed over by both hooks: the hook code closes over the app object, not | ||
| 22 | # the request, and the same app object dispatches every request it ever handles, so a variable | ||
| 23 | # scoped to the request itself is what actually keeps two overlapping requests' own timers from | ||
| 24 | # clobbering each other. | ||
| 25 | # | ||
| 26 | # Also starts a trace per request in before_request and finishes it in after_request (root span | ||
| 27 | # named like the transaction), so a slow request's own breakdown reaches /spans; there is no | ||
| 28 | # automatic database or HTTP span, add those by hand with ForgeOps::Tracker::span (and | ||
| 29 | # ForgeOps::Tracker::http_span for an outgoing HTTP call, which also hands it the traceparent | ||
| 30 | # header to send). | ||
| 31 | # | ||
| 32 | # The trace continues the caller's when the request arrived with a usable W3C `traceparent` header | ||
| 33 | # (see ForgeOps::Tracker::TraceParent), and its trace id exists even with track_tracing off, since | ||
| 34 | # every error reported during the request carries it. Dancer2 has already matched the route when | ||
| 35 | # before_request fires, so the request is named there (transaction name and endpoint, both | ||
| 36 | # "GET /users/:id"), before the route runs: an error reported from inside the route carries both. | ||
| 37 | # | ||
| 38 | # A route that dies never reaches after_request (see below), so an on_route_exception hook | ||
| 39 | # finishes its trace instead, marked errored so it's sent however fast it was. It remembers the | ||
| 40 | # request's context for that error first (ForgeOps::Tracker::snapshot_onto): Dancer2 runs the two | ||
| 41 | # plugins' on_route_exception hooks in load order, and Integrations::Dancer2's report has to find | ||
| 42 | # the trace id either way. | ||
| 43 | # | ||
| 44 | # The transaction name is the matched route's own spec_route (e.g. "/users/:id", the pattern as | ||
| 45 | # originally declared): Dancer2 sets this on the request during route matching, before the route | ||
| 46 | # handler runs, so it is already populated by the time after_request fires. A distinct user id | ||
| 47 | # must not explode into its own separate transaction the way the raw path would. | ||
| 48 | # | ||
| 49 | # after_request only fires for a request that actually matched a route and produced a normal | ||
| 50 | # response (confirmed directly against Dancer2::Core::App's own dispatch code: a 404's | ||
| 51 | # response_not_found path returns without ever calling execute_hook for after_request at all), so | ||
| 52 | # a request that never matched any route isn't recorded by this integration, an accepted gap | ||
| 53 | # rather than a fallback: Dancer2's own hook API simply doesn't expose that case as an event. | ||
| 54 | my $STARTED_AT_KEY = 'forge_ops_tracker_performance_started_at'; | ||
| 55 | |||
| 56 | sub BUILD { | ||
| 57 | my ($plugin) = @_; | ||
| 58 | my $app = $plugin->app; | ||
| 59 | |||
| 60 | $app->add_hook(Dancer2::Core::Hook->new( | ||
| 61 | name => 'before_request', | ||
| 62 | code => sub { | ||
| 63 | ForgeOps::Tracker::clear_breadcrumbs(); | ||
| 64 | my $request = $app->request; | ||
| 65 | ForgeOps::Tracker::start_trace($request ? scalar $request->header('traceparent') : undef); | ||
| 66 | return unless $request; | ||
| 67 | |||
| 68 | $request->var($STARTED_AT_KEY, Time::HiRes::time); | ||
| 69 | my $route = $request->route; | ||
| 70 | if ($route) { | ||
| 71 | my $name = $request->method . ' ' . $route->spec_route; | ||
| 72 | ForgeOps::Tracker::set_request_route($name, $name); | ||
| 73 | } | ||
| 74 | }, | ||
| 75 | )); | ||
| 76 | |||
| 77 | $app->add_hook(Dancer2::Core::Hook->new( | ||
| 78 | name => 'on_route_exception', | ||
| 79 | code => sub { | ||
| 80 | my (undef, $error) = @_; | ||
| 81 | my $request = $app->request; | ||
| 82 | ForgeOps::Tracker::snapshot_onto($error); | ||
| 83 | my $started_at = $request ? $request->var($STARTED_AT_KEY) : undef; | ||
| 84 | return unless defined $started_at; | ||
| 85 | |||
| 86 | ForgeOps::Tracker::finish_trace( | ||
| 87 | _transaction_name($request), $started_at, (Time::HiRes::time - $started_at) * 1000, | ||
| 88 | ); | ||
| 89 | }, | ||
| 90 | )); | ||
| 91 | |||
| 92 | $app->add_hook(Dancer2::Core::Hook->new( | ||
| 93 | name => 'after_request', | ||
| 94 | code => sub { | ||
| 95 | my $request = $app->request; | ||
| 96 | return unless $request; | ||
| 97 | |||
| 98 | my $started_at = $request->var($STARTED_AT_KEY); | ||
| 99 | return unless defined $started_at; | ||
| 100 | |||
| 101 | my $duration_ms = (Time::HiRes::time - $started_at) * 1000; | ||
| 102 | my $transaction_name = _transaction_name($request); | ||
| 103 | |||
| 104 | ForgeOps::Tracker::record_performance($transaction_name, $duration_ms); | ||
| 105 | ForgeOps::Tracker::finish_trace($transaction_name, $started_at, $duration_ms); | ||
| 106 | ForgeOps::Tracker::add_breadcrumb( | ||
| 107 | $transaction_name, | ||
| 108 | category => 'controller', | ||
| 109 | data => { status => $app->response ? $app->response->status : undef, path => $request->path }, | ||
| 110 | ); | ||
| 111 | }, | ||
| 112 | )); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _transaction_name { | ||
| 116 | my ($request) = @_; | ||
| 117 | my $route = $request->route; | ||
| 118 | my $pattern = $route ? $route->spec_route : undef; | ||
| 119 | return $request->method . ' ' . (defined $pattern ? "$pattern" : $request->path); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; |