Performance

Sample Specific Transactions

traces_sampler
If you want to sample specific Transaction, use the traces_sampler option:

config/sentry.php
Copied
'traces_sampler' => function (\Sentry\Tracing\SamplingContext $context): float {
    if ($context->getParentSampled()) {
        // If the parent transaction (for example a JavaScript front-end)
        // is sampled, also sample the current transaction
        return 1.0;
    }

    if (some_condition()) {
        // Drop this transaction, by setting its sample rate to 0
        return 0;
    }

    // Default sample rate for all other transactions (replaces `traces_sample_rate`)
    return 0.25;
},

You can also define traces_sampler as a callable that receives a \Sentry\Tracing\SamplingContext $context which should return a float (for the sample rate):

Copied
'traces_sampler' => array('App\Helpers\General\TracesSampler', 'sample')

Instrument HTTP Calls

To instrument HTTP calls using GuzzleHttp, you'll need to add a handler to your Guzzle Client, which we provide within our SDK. Here's an example of it use:

Copied
$stack = \GuzzleHttp\HandlerStack::create();

// This is the important line
$stack->push(\Sentry\Tracing\GuzzleTracingMiddleware::trace());
// ----

$client = new \GuzzleHttp\Client([
    'base_uri' => 'http://httpbin.org',
    'timeout'  => 2.0,
    'handler'  => $stack
]);
$client->request('GET', '/get');

After adding this handler, you will receive spans for every request from this client.

Custom Instrumentation

To instrument certain regions of your code, you can create a transaction to capture them.

Copied
$transactionContext = new \Sentry\Tracing\TransactionContext();
$transactionContext->setName('External Call');
$transactionContext->setOp('http.caller');
$transaction = \Sentry\startTransaction($transactionContext);

// Setting the transaction to the current Hub allows you to retrieve it later
\Sentry\SentrySdk::getCurrentHub()->setSpan($transaction);

$spanContext = new \Sentry\Tracing\SpanContext();
$spanContext->setOp('functionX');
$span1 = $transaction->startChild($spanContext);

// Calling functionX
$this->functionX();
$span1->finish();

$transaction->finish();

Retrieving a Transaction

If you want to attach Spans to an already ongoing Transaction, you can use \Sentry\SentrySdk::getCurrentHub()->getTransaction(). This function will return a \Sentry\Tracing\Transaction if there is a running Transaction on the scope, otherwise it returns null.

Copied
$transaction = \Sentry\SentrySdk::getCurrentHub()->getTransaction();

if ($transaction instanceof Transaction) {
    $transaction->setName($routeName);
    $transaction->setData([
        'action' => $route->getActionName(),
        'name' => $route->getName()
    ]);
}

You can also use this to filter for specific conditions when you don't want to send a \Sentry\Tracing\Transaction. This example illustrates how:

Copied
$transaction = \Sentry\SentrySdk::getCurrentHub()->getTransaction();

if ($transaction instanceof \Sentry\Tracing\Transaction) {
    // $transaction->setSampled(false); // __DONT__ SEND TRANSACTION
    // $transaction->setSampled(true); // __DO__ SEND TRANSACTION
}

Improve Response Time

Using the performance capabilities of PHP has some impact on your response time (depending on the traces_sample_rate you configured). Because of the nature of PHP, requests in most cases are sent in the same process as you serve your users' response. In sum, this process affects the time it takes to send a request to Sentry, as it is added on top of your servers' response time. To mitigate this and reduce the addition close to zero, you can use a Relay running locally on the same machine or local network that acts as a proxy/agent. As a result, instead of your PHP process sending things to Sentry, the PHP process sends it to your local Relay, which then forwards it to Sentry.

To begin using Relay, review our content for Getting Started.

Currently, we recommend using Relay in managed mode (which is the default). Then follow the instructions in the Relay docs to send a Test Event via Relay to Sentry.

Don't forget to update your DSN to point to your running Relay instance. After you set up Relay, you should see a dramatic improvement to the impact on your server.

Connecting Services

If you are also using Performance Monitoring for JavaScript you can use a helper function to continue the trace started from your backend in order to connect services. Add the following line to your blade template rendering the <head/> of your page.

app.blade.php
Copied
<head>
...
{!! \Sentry\Laravel\Integration::sentryTracingMeta() !!}
...
</head>

This helper function will render a meta tag similar to this <meta name="sentry-trace" content="49879feb76c84743ba5034bd2d3f1ca3-7cb5535c930d4666-1"/> which our JS SDK will pick up and continue the trace. Therefore your frontend and your backend are connected via the same trace.

Otherwise other backend services with Performance Monitoring will connect automatically.