DEV Community

parsa raad
parsa raad

Posted on Edited on AI-assisted

Understanding setContext() in Sentry PHP: Making Errors More Debuggable

When an error occurs in production, the error message alone is often not enough to understand what happened.

You might know that a request failed, but still have questions:

  • Which user triggered the event?
  • Which operation was being performed?
  • What additional information could help reproduce the issue?

This is where Sentry's setContext() becomes useful.

What is setContext()?

setContext() allows you to attach structured, custom data to a Sentry event.

Instead of sending unrelated values individually, you can group related information under a meaningful name.

For example:

$scope->setContext('payment', [
    'user_id' => 123,
    'amount' => 250.50,
    'currency' => 'USD',
    'gateway' => 'stripe',
]);
Enter fullscreen mode Exit fullscreen mode

This adds a payment context to the event, making it easier to investigate what happened.

Understanding withScope()

You will often see setContext() used together with withScope().

Why?

Because a Scope defines the context associated with captured events.

Using withScope() creates a temporary scope. Once the callback finishes, that scope is discarded.

This is useful when you want to attach information to one specific event without unintentionally affecting other events.

\Sentry\withScope(function (\Sentry\State\Scope $scope) {
    $scope->setContext('payment', [
        'user_id' => 123,
        'gateway' => 'stripe',
    ]);

    \Sentry\captureMessage('Payment processing failed.');
});
Enter fullscreen mode Exit fullscreen mode

The context is attached to the event captured inside that scope.

A Real-World Laravel Example

Imagine you are processing an order payment in a Laravel application.

If the payment fails, you may want to capture the event in Sentry along with relevant order information.

A simple message tells you what happened, but additional context makes debugging much easier.

\Sentry\withScope(function (\Sentry\State\Scope $scope) use ($order) {
    $scope->setContext('order', [
        'order_id' => $order->id,
        'status' => $order->status,
        'total' => $order->total,
        'payment_method' => $order->payment_method,
    ]);

    \Sentry\captureMessage(
        'Order payment failed.',
        \Sentry\Severity::warning()
    );
});
Enter fullscreen mode Exit fullscreen mode

Now you have more information available when investigating the event in Sentry.

Instead of seeing only the error message, you can also inspect the associated order context, which contains useful information about the failed payment.

Important: Avoid sending passwords, authentication tokens, private keys, or other sensitive information to monitoring services. User identifiers and other potentially sensitive data should also be handled according to your privacy requirements.

Context vs Tags vs Extra

Sentry provides several ways to attach additional information to events.

Method Purpose
setContext() Group related structured information
setTag() Add metadata that can be used for filtering and searching
setExtra() Attach additional debugging information
setUser() Associate an event with a user

For example, use a tag when you want to filter events by an operation:

$scope->setTag('operation', 'payment');
Enter fullscreen mode Exit fullscreen mode

Use context when you want to attach multiple related values:

$scope->setContext('payment', [
    'amount' => 250,
    'currency' => 'USD',
]);
Enter fullscreen mode Exit fullscreen mode

These methods serve different purposes and can be used together.

Final Thoughts

Good error monitoring is not just about knowing that something failed. It's about having enough information to understand why it failed.

setContext() is a simple but useful way to make Sentry events more informative, especially in complex Laravel applications.

A little extra context can save a lot of debugging time.


References:

php #laravel #sentry #debugging #webdev

Top comments (0)