| 1 | package Sentry::Hub; | ||
| 2 | use Mojo::Base -base, -signatures; | ||
| 3 | |||
| 4 | use Mojo::Util 'dumper'; | ||
| 5 | use Sentry::Hub::Scope; | ||
| 6 | use Sentry::Logger; | ||
| 7 | use Sentry::Severity; | ||
| 8 | use Sentry::Tracing::Transaction; | ||
| 9 | use Sentry::Util qw(uuid4); | ||
| 10 | use Time::HiRes qw(time); | ||
| 11 | use Try::Tiny; | ||
| 12 | |||
| 13 | my $Instance; | ||
| 14 | |||
| 15 | has _last_event_id => undef; | ||
| 16 | has _stack => sub { [{}] }; | ||
| 17 | has client => undef; | ||
| 18 | has logger => sub { Sentry::Logger->new }; | ||
| 19 | has scopes => sub { [Sentry::Hub::Scope->new] }; | ||
| 20 | |||
| 21 | # has _root_scope => | ||
| 22 | |||
| 23 | sub init ($package, $options) { | ||
| 24 | $Instance = Sentry::Hub->new($options); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub bind_client ($self, $client) { | ||
| 28 | $self->client($client); | ||
| 29 | $client->setup_integrations(); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _get_top_scope ($self) { | ||
| 33 | return @{ $self->scopes }[0]; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub get_current_scope ($package) { | ||
| 37 | return @{ $package->get_current_hub()->scopes }[-1]; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub get_current_hub { | ||
| 41 | $Instance //= Sentry::Hub->new(); | ||
| 42 | return $Instance; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub configure_scope ($self, $cb) { | ||
| 46 | $cb->($self->get_current_scope); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub push_scope ($self) { | ||
| 50 | my $scope = $self->get_current_scope->clone; | ||
| 51 | push $self->scopes->@*, $scope; | ||
| 52 | return $scope; | ||
| 53 | } | ||
| 54 | sub pop_scope ($self) { pop @{ $self->scopes } } | ||
| 55 | |||
| 56 | sub with_scope ($self, $cb) { | ||
| 57 | my $scope = $self->push_scope; | ||
| 58 | |||
| 59 | try { | ||
| 60 | $cb->($scope); | ||
| 61 | } finally { | ||
| 62 | $self->pop_scope; | ||
| 63 | }; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub get_scope ($self) { | ||
| 67 | return $self->get_current_scope; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _invoke_client ($self, $method, @args) { | ||
| 71 | my $client = $self->client; | ||
| 72 | my $scope = $self->get_current_scope; | ||
| 73 | |||
| 74 | if ($client->can($method)) { | ||
| 75 | $client->$method(@args, $scope); | ||
| 76 | } else { | ||
| 77 | warn "Unknown method: $method"; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _new_event_id ($self) { | ||
| 82 | $self->_last_event_id(uuid4()); | ||
| 83 | return $self->_last_event_id; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub capture_message ($self, $message, $level = Sentry::Severity->Info, | ||
| 87 | $hint = undef) { | ||
| 88 | my $event_id = $self->_new_event_id(); | ||
| 89 | |||
| 90 | $self->_invoke_client('capture_message', $message, $level, | ||
| 91 | { event_id => $event_id }); | ||
| 92 | |||
| 93 | return $event_id; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub capture_exception ($self, $exception, $hint = undef) { | ||
| 97 | my $event_id = $self->_new_event_id(); | ||
| 98 | |||
| 99 | $self->_invoke_client('capture_exception', $exception, | ||
| 100 | { event_id => $event_id }); | ||
| 101 | |||
| 102 | return $event_id; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub capture_event ($self, $event, $hint = {}) { | ||
| 106 | my $event_id = $self->_new_event_id(); | ||
| 107 | |||
| 108 | $self->_invoke_client('capture_event', $event, | ||
| 109 | { $hint->%*, event_id => $event_id }); | ||
| 110 | |||
| 111 | return $event_id; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub add_breadcrumb ($self, $crumb, $hint = undef) { | ||
| 115 | $self->get_current_scope->add_breadcrumb($crumb); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub run ($self, $cb) { | ||
| 119 | $cb->($self); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub start_transaction ($self, $context, $custom_sampling_context = undef) { | ||
| 123 | return Sentry::Tracing::Transaction->new( | ||
| 124 | { $context->%*, _hub => $self, start_timestamp => time }); | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; |