NAME
Sentry::Scope - Data attached to events, and the scopes in effect
SYNOPSIS
Sentry::SDK->configure_scope(sub ($scope) {
$scope->set_tag(tenant => 'acme');
});
Sentry::SDK->with_scope(sub ($scope) {
$scope->set_context(order => { id => 42 });
Sentry::SDK->capture_message('order failed');
});
DESCRIPTION
A scope holds the data that is attached to events. The global, the isolation and the current scope are in effect at any time, see "SCOPES" in Sentry::SDK. Applications usually get scopes from Sentry::SDK; the class methods below are the same functions for code that must not depend on Sentry::SDK being unmocked.
CLASS METHODS
get_global_scope, get_isolation_scope, get_current_scope
my $scope = Sentry::Scope->get_current_scope;
The scopes in effect.
get_client
The client of the current, else the isolation, else the global scope; Sentry::Client::NoOp if none has one.
get_active_span
The span of the current, else the isolation, else the global scope.
with_scope, with_isolation_scope
See "with_scope" in Sentry::SDK and "with_isolation_scope" in Sentry::SDK.
use_scopes
my ($isolation, $current)
= (Sentry::Scope->get_isolation_scope, Sentry::Scope->get_current_scope);
$loop->on(event => sub {
Sentry::Scope->use_scopes($isolation, $current, sub {
Sentry::SDK->capture_message('done');
});
});
Runs the callback with the given scopes in effect, e.g. a callback of non-blocking code in the scopes of the code that registered it. Returns the callback's result and rethrows its exceptions.
set_scopes
Internal, for the integrations of servers whose requests span several callbacks (mod_perl, FastCGI). Use "use_scopes".
trace_propagation
my ($sentry_trace, $sampling_context)
= Sentry::Scope->trace_propagation($span);
Internal, for HTTP client integrations (see "propagate_trace" in Sentry::Integration::Base): the sentry-trace value and the dynamic sampling context of $span or the active span while tracing is enabled, else of the trace of the isolation scope.
trace_id
The id of the trace of the code running now: of the active span while tracing is enabled, else of the isolation scope.
bind_scopes
Mojo::IOLoop->singleton->reactor->io(
$handle => Sentry::Scope->bind_scopes(sub ($reactor, $writable) { ... }));
Returns a callback that calls the given one with the scopes in effect now, whenever and wherever it is called, and passes its arguments and result. Sentry::Integration::MojoIOLoop does this for promises, timers and user agent callbacks.
ATTRIBUTES
Plain data accessors: attachments, breadcrumbs, client, contexts, error_event_processors, event_processors, extra, feature_flags (a Sentry::FeatureFlags), fingerprint, last_event_id, level, propagation_context, request, session (of "RELEASE HEALTH" in Sentry::SDK), span, tags, transaction_name and user. They hold the data of this scope only; events get the data of all scopes in effect.
METHODS
set_tag, set_tags, set_extra, set_extras, set_context, set_user, set_level, set_fingerprint, set_span
$scope->set_tag(tenant => 'acme')->set_user({ id => 42 });
Set data of the scope and return it. set_context with undef removes the context.
set_client
Sentry::SDK->with_scope(sub ($scope) {
$scope->set_client(Sentry::Client::NoOp->new);
...; # captures nothing
});
Sets the client of the scope, which takes precedence over those of the scopes below it (see "get_client" in Sentry::SDK), and returns the scope. Clients are created by "init" in Sentry::SDK only (Sentry::Client->new is internal): this is for putting an existing client, or a Sentry::Client::NoOp to capture nothing, on a scope.
set_transaction_name
$scope->set_transaction_name($name, $source);
Names the events captured in the scope (errors, messages) and renames the transaction of its span, with $source (default custom). The isolation scope in effect renames the transaction of the current scope: the request's, while serving one. Events of a transaction get its name, other events the name of the scope; transactions keep their own name otherwise. The framework integrations keep a custom name. A name set before the request began (e.g. in setup of a CGI::Application served as plain CGI) does not apply: each request starts unnamed. With undef, removes the name of the scope.
get_span
The span of this scope.
add_breadcrumb
$scope->add_breadcrumb(\%breadcrumb, \%hint);
Like "add_breadcrumb" in Sentry::SDK, but adds to this scope.
add_feature_flag
$scope->add_feature_flag('new-checkout' => !!1);
Records the result of a feature flag in this scope (see Sentry::FeatureFlags) and returns the scope. Unlike "add_feature_flag" in Sentry::SDK, it leaves the active span alone.
clear_breadcrumbs
Removes the breadcrumbs of this scope and hides those the global and isolation scope hold at that moment from the events of this scope and its clones. Breadcrumbs added later show up.
clear
Resets the data of the scope (including clear_breadcrumbs), keeping the event processors.
add_event_processor, add_error_processor
$scope->add_event_processor(sub ($event, $hint) { ...; return $event });
Event processors get every event captured in the scope and return it, changed or not, or undef to drop it. Error processors only get events with an exception and run first.
propagation_context
The trace of the scope's unit of work, for the SDK's integrations. The isolation scope of the process, outside of requests and other units of work, has a trace per process: a forked child (e.g. a prefork worker) starts its own, unless the parent continued one (e.g. from SENTRY_TRACE).
set_new_propagation_context
Starts a new trace for errors outside of transactions. Integrations call it on the isolation scope where a unit of work begins.
continue_trace
$isolation_scope->continue_trace(\%headers);
Continues the trace of the sentry-trace and baggage headers, or starts a new one if there is no valid one; see "continue_trace" in Sentry::SDK.
clone
A shallow copy of the scope: its lists and hashes (tags, contexts, breadcrumbs, feature flags etc.) are copied one level deep, their values (e.g. a single context or breadcrumb), the span and the client are shared.
capture_message, capture_exception, capture_event, capture_feedback
$scope->capture_exception($exception, \%hint);
Captures with this scope as current scope. "capture_exception" in Sentry::SDK etc. call them on the current scope.
capture_log, capture_metric
$scope->capture_log({ level => 'info', body => 'Order placed' });
$scope->capture_metric({ type => 'counter', name => 'orders', value => 1 });
Capture a log or metric with the trace, span and user of the scopes in effect, this one as current scope; see "capture_log" in Sentry::Client for the fields. "logger" in Sentry::SDK and "metrics" in Sentry::SDK call them on the current scope.
add_attachment
$scope->add_attachment({ path => '/var/log/myapp/last-job.log' });
$scope->add_attachment(Sentry::Attachment->new(
bytes => $dump, filename => 'state.json'));
Adds an attachment (a Sentry::Attachment or the arguments of one), which is sent with the error, message and feedback events captured while the scope is in effect. Dies on wrong arguments. Returns the scope.
clear_attachments
Removes the attachments of the scope. Returns the scope.
trace_ids
my ($trace_id, $span_id) = $scope->trace_ids;
Internal: the ids logs and metrics are linked by, the trace and the active span (undef without tracing or span).
start_transaction
See "start_transaction" in Sentry::SDK.
apply_to_event
my $event = $scope->apply_to_event(\%event, \%hint);
Applies the data of this scope to an event and runs the event processors. Used by the client.
update
$scope->update({ tags => { a => 1 }, level => 'warning' });
Applies a capture context (see "capture_exception" in Sentry::SDK).
FUNCTIONS
add_global_event_processor, get_global_event_processors
Event processors for events of all scopes.