NAME
Sentry::Integration::Base - Base class for integrations
SYNOPSIS
package My::Integration::Foo;
use parent 'Sentry::Integration::Base';
use feature 'signatures';
sub install ($self) {
$self->patch(
'Foo', 'Foo', 'fetch',
sub ($integration, $orig, $foo, $key) {
return $integration->in_span(
{ op => 'cache.get', description => $key },
sub ($span) { $orig->($foo, $key) },
sub ($span, $error, @result) {
$integration->add_breadcrumb({ category => 'cache', message => $key })
if $integration->breadcrumbs;
},
);
}
);
}
# Enable or configure it
Sentry::SDK->init({ integrations => [My::Integration::Foo->new] });
DESCRIPTION
Integrations instrument third-party code by wrapping its methods. The wrappers are installed once per process and integration class, as soon as the instrumented module is loaded. Every call then asks the current client for its instance of the integration: without one (no client, or the integration is not enabled) the original method runs untouched, otherwise the wrapper runs with that instance, so the options of the latest "init" in Sentry::SDK apply.
Code of the SDK must never change what the application does: errors in a wrapper are logged, exceptions of the original method pass through unchanged, and the calling context (list, scalar, void) is kept, as are $! and $?. Integrations leave the SDK's own requests to Sentry alone.
ATTRIBUTES
breadcrumbs
Record breadcrumbs. Default: 1.
tracing
Record spans. Default: 1.
METHODS
install
sub install ($self) { ... }
Override to install the wrappers, usually with "patch". Called once per process and integration class.
setup_once
Called by the SDK once per process and integration class. Integrations that do not use "patch" may override it instead of "install"; they are then responsible for checking whether they are enabled.
new
my $integration = My::Integration::Foo->new(breadcrumbs => 0);
Dies on arguments that are not among "arguments", so that a misspelt one does not silently do nothing.
arguments
sub arguments ($class) { ($class->SUPER::arguments, qw(ttl prefix)) }
The names of the arguments "new" takes: by default the attributes of the SDK's integrations ("breadcrumbs", "tracing" and those of the integration class). An integration of its own with arguments of its own overrides it to add them.
setup
sub setup ($self, $client) { ... }
Override for what belongs to a client rather than to the process: called for every client using the integration, at each "init" in Sentry::SDK, after "setup_once".
missing_dsn_hint
sub missing_dsn_hint ($self) {
return under_my_server() ? 'pass SENTRY_DSN with ...' : undef;
}
Override if the integration knows why the SDK may have no DSN, e.g. a server that hides the environment variables: "init" in Sentry::SDK without DSN then warns with the returned text (once per process).
origin
sub origin ($self) { 'auto.db.foo' }
The origin of the integration's spans (see trace origin): auto.<category>.<name>. "in_span" sets it. Default: auto.
name
The name of the integration, its class name.
patch
$self->patch($module, $package, $method, sub ($integration, $orig, @args) { ... });
Wraps $package::$method once $module is loaded, whether that happens before or after init. The module is never loaded by the SDK. If the method is missing or "incompatibility" reports a problem, the integration is disabled instead. The arguments after $orig are aliased to the caller's variables, as in @_; a wrapper of a function that assigns to its arguments passes them on as @_[2 .. $#_].
$self->patch($module, $package, $method, $wrapper, relocate_messages => 0);
Messages of C code (XS methods) that name the wrapper's line are changed to name the caller's, as without the SDK, at some cost per call. Hot XS methods whose messages do not matter turn that off with relocate_messages => 0.
$self->patch($module, $package, $function, $wrapper, imported => 1);
For functions the module exports: copies packages imported before the wrapper was installed (e.g. before init) are wrapped, too.
incompatibility
sub incompatibility ($self, $module) {
return missing_api(['Foo::Internal', qw(_fetch)]);
}
Override to check what the integration relies on when $module has been loaded, beyond the patched method, which is always checked: return why it cannot work, or undef. If it cannot, the integration is disabled with one warning and the application runs as without it.
disable
$integration->disable($reason);
Disables the integration for the process: its wrappers call the original code only. Warns once.
is_disabled
Whether the integration has been disabled.
activate
$integration->activate;
For integrations that instrument code without "patch": call it once the integration instruments something in the process (e.g. a module it hooks into was loaded). "patch" does it itself.
is_active
Whether the integration instruments something in the process and is not disabled. Only active integrations are listed in the sdk.integrations of events.
missing_api
my $problem = Sentry::Integration::Base::missing_api(
['Foo', qw(fetch store)], ['Foo::Item', qw(key)]);
Describes the methods missing of the given classes, or returns undef.
in_span
my @result = $integration->in_span(\%span_context, $code, $on_finish);
Calls $code->($span) in the caller's context, inside a child span of the current span if "tracing" is enabled and a span is active ($span is undef otherwise). The span is finished in any case, with status internal_error if $code dies; the exception is rethrown. $on_finish->($span, $error, @result) records breadcrumbs and response data; failures in it are logged.
in_active_span
my @result = $integration->in_active_span(\%span_context, $code,
$on_finish);
Like "in_span", but the span is the active one while $code runs, in a scope of its own: spans and errors of the code it calls belong to it.
in_child_span
my @result = $integration->in_child_span($parent, \%span_context, $code,
$on_finish);
Like "in_span", but the span is a child of $parent instead of the current span; without $parent there is no span. For nested spans of one call, e.g. a cache.get span around the db.redis span of the command.
add_query_source
$integration->in_span(\%span_context, $code, sub ($span, @) {
$integration->add_query_source($span);
});
For database integrations, like sentry-python's: if the query took longer than "enable_db_query_source, db_query_source_threshold_ms" in Sentry::SDK, adds the innermost frame of the application's code (see "in_app_include, in_app_exclude" in Sentry::SDK) to the span, as code.filepath, code.lineno, code.function and code.namespace. Call it from the code finishing the span, while the application's call is still on the stack.
propagate_trace
$integration->propagate_trace($span, $url, sub { $request->header(@_) });
Adds the sentry-trace and baggage headers to an outgoing request if $url matches "trace_propagation_targets" in Sentry::SDK. The callback returns the value of the header named by its first argument, or sets it to the second argument, like the header methods of HTTP::Headers and Mojo::Headers. Entries of other vendors in an existing baggage header are kept. $span is the span of the request, if any; without one, the headers continue the active span or the trace of the isolation scope (see "get_traceparent, get_baggage" in Sentry::SDK).
http_request_data
my $data = $integration->http_request_data($method, $url);
my $span_context = {
op => 'http.client',
description => "$method $data->{url}",
data => $data,
};
For HTTP client integrations: the data of an outgoing request for its span and breadcrumb, i.e. http.request.method, url without query, user info and fragment, and the filtered query as http.query, with the data of "with_http_request_details".
http_client_span_context
my $span_context = $integration->http_client_span_context($method, $url);
my $data = $span_context->{data}; # also for the breadcrumb
For HTTP client integrations: the context of the span (op http.client) of an outgoing request, with the "http_request_data" as its data. The description is the method and the URL, followed by the operation of "with_http_request_details" in parentheses, as the JavaScript SDK's GraphQL client integration names spans: POST https://api.example.com/graphql (query GetUser).
with_http_request_details
my $result = $integration->with_http_request_details(
{
operation => 'query GetUser',
data => { 'graphql.operation.name' => 'GetUser' },
sensitive_query_parameters => ['variables'],
},
sub { $client->send_over_http(...) }
);
For integrations of libraries that send their requests through an HTTP client (e.g. Sentry::Integration::GraphQLClient): calls the code in the caller's context, and the HTTP client integrations add the details to the spans and breadcrumbs of the requests it starts, see "http_client_span_context". The values of the sensitive_query_parameters in their URLs are filtered. The details apply to the requests started while the code runs: also to those that callbacks of an event loop it runs (e.g. a blocking Mojo::UserAgent request) start, not to those started by callbacks after it returned.
set_response_length
$integration->set_response_length($span, $content_length);
For HTTP client integrations: sets http.response_content_length of the span to the Content-Length of the response, if it is a number of bytes. Does nothing without span.
add_breadcrumb
$integration->add_breadcrumb(\%breadcrumb, \%hint);
Adds a breadcrumb to the isolation scope, like "add_breadcrumb" in Sentry::SDK.
guarded
Sentry::Integration::Base::guarded(sub { ... });
Runs SDK code on behalf of the application: an error is logged, not thrown.