| 1 | package ForgeOps::Tracker::Integrations::Dancer2; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Dancer2::Plugin; | ||
| 6 | use Dancer2::Core::Hook; | ||
| 7 | use ForgeOps::Tracker; | ||
| 8 | |||
| 9 | # Dancer2 plugin. Named ForgeOps::Tracker::Integrations::Dancer2 rather than the ecosystem's usual | ||
| 10 | # Dancer2::Plugin::* convention, to stay consistent with this repo's own | ||
| 11 | # ForgeOps::Tracker::Integrations::* namespace (see Integrations/PSGI.pm): Dancer2 instantiates | ||
| 12 | # any package that `use Dancer2::Plugin;` and is itself `use`d from an app, regardless of | ||
| 13 | # namespace, so this works exactly the same as a conventionally-named one: | ||
| 14 | # | ||
| 15 | # use Dancer2; | ||
| 16 | # use ForgeOps::Tracker::Integrations::Dancer2; # that's it, no further wiring | ||
| 17 | # | ||
| 18 | # Registers an on_route_exception hook: Dancer2's own documented hook, fired whenever a route | ||
| 19 | # throws an exception that reaches Dancer2's own top-level handling, *before* Dancer2 renders its | ||
| 20 | # error page. Reporting from a hook, rather than wrapping every route by hand, is what makes this | ||
| 21 | # automatic with no per-route changes, the same "no further wiring" story every other framework | ||
| 22 | # integration in this repo provides for its own framework. The hook only observes; it doesn't | ||
| 23 | # change what Dancer2 does with the exception afterward, so Dancer2's own error page (or a | ||
| 24 | # registered error handler) still renders exactly as if this plugin weren't installed. | ||
| 25 | sub BUILD { | ||
| 26 | my ($plugin) = @_; | ||
| 27 | |||
| 28 | # A fresh breadcrumb trail per request: a prefork worker serves many requests in a row. | ||
| 29 | $plugin->app->add_hook(Dancer2::Core::Hook->new( | ||
| 30 | name => 'before_request', | ||
| 31 | code => sub { ForgeOps::Tracker::clear_breadcrumbs() }, | ||
| 32 | )); | ||
| 33 | |||
| 34 | $plugin->app->add_hook(Dancer2::Core::Hook->new( | ||
| 35 | name => 'on_route_exception', | ||
| 36 | code => sub { | ||
| 37 | my ($app, $error) = @_; | ||
| 38 | my $request = $app->request; | ||
| 39 | # Dancer2's after_request hook never fires for a request that raised (confirmed | ||
| 40 | # directly), so the performance plugin's own controller breadcrumb can't cover this | ||
| 41 | # case: record the failing request's own here, so the report below carries it. | ||
| 42 | if ($request) { | ||
| 43 | my $route = $request->route; | ||
| 44 | my $pattern = $route ? $route->spec_route : undef; | ||
| 45 | ForgeOps::Tracker::add_breadcrumb( | ||
| 46 | $request->method . ' ' . (defined $pattern ? "$pattern" : $request->path), | ||
| 47 | category => 'controller', | ||
| 48 | level => 'error', | ||
| 49 | data => { path => $request->path }, | ||
| 50 | ); | ||
| 51 | } | ||
| 52 | ForgeOps::Tracker::report($error, { | ||
| 53 | path => $request ? $request->path : undef, | ||
| 54 | method => $request ? $request->method : undef, | ||
| 55 | }); | ||
| 56 | }, | ||
| 57 | )); | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; |