Performance

Sample Specific Transactions

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

Copied
\Sentry\init([
    'dsn' => 'https://examplePublicKey@o0.ingest.sentry.io/0',
    '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, depending on where your request originates, you can connect traces:

  1. For requests that start in your backend, by adding a meta tag in your HTML template that contains tracing information.
  2. For requests that start in JavaScript, by the SDK setting a header on requests to your backend.

Otherwise, backend services with Performance Monitoring connect automatically.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").