Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 21 additions & 47 deletions docs/platforms/php/guides/laravel/configuration/laravel-options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,33 @@ SENTRY_TRACES_SAMPLE_RATE=1.0

### Advanced Sample Rate

If you want more control over which requests are monitored, you can use the [`traces_sampler`](/platforms/php/guides/laravel/configuration/options/#traces-sampler) option:
If you want more control over which requests are monitored, use the [`traces_sampler`](/platforms/php/guides/laravel/configuration/options/#traces-sampler) option as a callable:

```php {filename:config/sentry.php}
'traces_sampler' => function (\Sentry\Tracing\SamplingContext $context): float {
// We always sample if the front-end indicates it was sampled to have full traces front to back
if ($context->getParentSampled()) {
return 1.0;
}
'traces_sampler' => [App\Exceptions\Sentry::class, 'tracesSampler'],
```

if (some_condition()) {
// Drop this transaction, by setting its sample rate to 0
return 0.0;
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function tracesSampler(\Sentry\Tracing\SamplingContext $context): float
{
// Keep front-end and back-end traces connected.
if ($context->getParentSampled()) {
return 1.0;
}

if (some_condition()) {
// Drop this transaction.
return 0.0;
}

// Default sample rate for all other transactions.
return 0.25;
}

// Default sample rate for all other transactions
return 0.25;
},
}
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).

</Alert>

### More Configuration

You can also configure which parts of your application are traced automatically.
Expand Down Expand Up @@ -136,31 +138,3 @@ These settings have no effect if `SENTRY_TRACES_SAMPLE_RATE` is set to `0.0` or
'default_integrations' => env('SENTRY_TRACE_DEFAULT_INTEGRATIONS_ENABLED', true),
],
```

## Closures and Config Caching

Sometimes the SDK requires a closure as an option value. However, this causes problems when using `php artisan config:cache`, resulting in the `Your configuration files are not serializable` error.

We can work around that by providing a callable instead of a closure. In this example we are using the `traces_sampler` option, but this can be used for any other option that accepts a closure:

```php {filename:config/sentry.php}
'traces_sampler' => [App\Exceptions\Sentry::class, 'tracesSampler'],
```

This callable points to the `App\Exceptions\Sentry` class and the `tracesSampler` method:

```php {filename:app/Exceptions/Sentry.php}
<?php

namespace App\Exceptions;

use Sentry\Tracing\SamplingContext;

class Sentry
{
public static function tracesSampler(SamplingContext $context): float
{
// The code you would have placed in the closure...
}
}
```
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
In the Laravel config, a closure can be used to modify the event or return a completely new one. If you return `null`, the event will be discarded.
In Laravel, use a callable to modify the check-in event or return a completely new one. If you return `null`, the event will be discarded.

```php {filename:config/sentry.php}
'before_send_check_in' => function (\Sentry\Event $event): ?\Sentry\Event {
$checkIn = $event->getCheckIn();
$checkInEnvironment = $checkIn->getEnvironment();

if ($checkInEnvironment !== 'production') {
return null;
}

return $event;
},
'before_send_check_in' => [App\Exceptions\Sentry::class, 'beforeSendCheckIn'],
```

<Alert>
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function beforeSendCheckIn(\Sentry\Event $event): ?\Sentry\Event
{
$checkIn = $event->getCheckIn();
$checkInEnvironment = $checkIn->getEnvironment();

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).
if ($checkInEnvironment !== 'production') {
return null;
}

</Alert>
return $event;
}
}
```
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
```php {filename:config/sentry.php}
'before_send' => function (\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event {
if ($hint !== null && $hint->exception !== null && str_contains($hint->exception->getMessage(), 'database unavailable')) {
$event->setFingerprint(['database-unavailable']);
}

return $event;
},
'before_send' => [App\Exceptions\Sentry::class, 'beforeSend'],
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function beforeSend(\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event
{
if ($hint !== null && $hint->exception !== null && str_contains($hint->exception->getMessage(), 'database unavailable')) {
$event->setFingerprint(['database-unavailable']);
}

</Alert>
return $event;
}
}
```
26 changes: 14 additions & 12 deletions platform-includes/configuration/before-send-hint/php.laravel.mdx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
```php {filename:config/sentry.php}
'before_send' => function (\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event {
// Ignore the event if the original exception is an instance of MyException
if ($hint !== null && $hint->exception instanceof MyException) {
return null;
}

return $event;
},
'before_send' => [App\Exceptions\Sentry::class, 'beforeSend'],
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function beforeSend(\Sentry\Event $event, ?\Sentry\EventHint $hint): ?\Sentry\Event
{
// Ignore the event if the original exception is an instance of MyException.
if ($hint !== null && $hint->exception instanceof MyException) {
return null;
}

</Alert>
return $event;
}
}
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
In the Laravel config, a closure can be used to modify the event or return a completely new one. If you return `null`, the event will be discarded.
In Laravel, use a callable to modify the transaction event or return a completely new one. If you return `null`, the event will be discarded.

```php {filename:config/sentry.php}
'before_send_transaction' => function (\Sentry\Event $transaction): ?\Sentry\Event {
return $transaction;
},
'before_send_transaction' => [App\Exceptions\Sentry::class, 'beforeSendTransaction'],
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).

</Alert>
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function beforeSendTransaction(\Sentry\Event $transaction): ?\Sentry\Event
{
return $transaction;
}
}
```
20 changes: 11 additions & 9 deletions platform-includes/configuration/before-send/php.laravel.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
In the Laravel config, a closure can be used to modify the event or return a completely new one. If you return `null`, the event will be discarded.
In Laravel, use a callable to modify the event or return a completely new one. If you return `null`, the event will be discarded.

```php {filename:config/sentry.php}
'before_send' => function (\Sentry\Event $event): ?\Sentry\Event {
return $event;
},
'before_send' => [App\Exceptions\Sentry::class, 'beforeSend'],
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).

</Alert>
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function beforeSend(\Sentry\Event $event): ?\Sentry\Event
{
return $event;
}
}
```
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
```php {filename:config/sentry.php}
'before_breadcrumb' => function (\Sentry\Breadcrumb $breadcrumb): ?\Sentry\Breadcrumb {
return $breadcrumb;
},
'before_breadcrumb' => [App\Exceptions\Sentry::class, 'beforeBreadcrumb'],
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).

</Alert>
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function beforeBreadcrumb(\Sentry\Breadcrumb $breadcrumb): ?\Sentry\Breadcrumb
{
return $breadcrumb;
}
}
```
23 changes: 11 additions & 12 deletions platform-includes/logs/options/php.laravel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
To filter logs, or update them before they are sent to Sentry, you can use the `before_send_log` option.

```php {filename:config/sentry.php}
// ...
'before_send_log' => function (\Sentry\Logs\Log $log): ?\Sentry\Logs\Log {
'before_send_log' => [App\Exceptions\Sentry::class, 'beforeSendLog'],
```

```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function beforeSendLog(\Sentry\Logs\Log $log): ?\Sentry\Logs\Log
{
if ($log->getLevel() === \Sentry\Logs\LogLevel::info()) {
// Filter out all info logs
// Filter out all info logs.
return null;
}

return $log;
},
// ...
]);
}
}
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).

</Alert>

The `before_send_log` function receives a log object, and should return the log object if you want it to be sent to Sentry, or `null` if you want to discard it.
29 changes: 15 additions & 14 deletions platform-includes/metrics/options/php.laravel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

To filter metrics, or update them before they are sent to Sentry, you can use the `before_send_metric` option. If the callback returns `null`, the metric is not emitted. Attributes can also be updated in the callback function.

```php
// ...
'before_send_metric' => static function (\Sentry\Metrics\Types\Metric $metric): ?\Sentry\Metrics\Types\Metric {
if ($metric->getName() === 'removed-metric') {
return null;
}
return $metric;
},
// ...
```php {filename:config/sentry.php}
'before_send_metric' => [App\Exceptions\Sentry::class, 'beforeSendMetric'],
```

The `before_send_metric` function receives a metric object, and should return the metric object if you want it to be sent to Sentry, or `null` if you want to discard it.

<Alert>
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function beforeSendMetric(\Sentry\Metrics\Types\Metric $metric): ?\Sentry\Metrics\Types\Metric
{
if ($metric->getName() === 'removed-metric') {
return null;
}

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).
return $metric;
}
}
```

</Alert>
The `before_send_metric` function receives a metric object, and should return the metric object if you want it to be sent to Sentry, or `null` if you want to discard it.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
```php {filename:config/sentry.php}
'traces_sampler' => function (\Sentry\Tracing\SamplingContext $context): float {
// always inherit
if ($context->getParentSampled()) {
return 1.0;
}

// the rest of sampling logic
},
'traces_sampler' => [App\Exceptions\Sentry::class, 'tracesSampler'],
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function tracesSampler(\Sentry\Tracing\SamplingContext $context): float
{
// Always inherit the parent sampling decision.
if ($context->getParentSampled()) {
return 1.0;
}

</Alert>
// The rest of your sampling logic.
return 0.25;
}
}
```
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
```php {filename:config/sentry.php}
// Specify a fixed sample rate:
'traces_sample_rate' => 0.2,
// Or provide a custom sampler:
'traces_sampler' => function (\Sentry\Tracing\SamplingContext $context): float {
// return a number between 0 and 1
},
// Or provide a custom sampler as a callable:
'traces_sampler' => [App\Exceptions\Sentry::class, 'tracesSampler'],
```

<Alert>

Learn more in [Closures and Config Caching](/platforms/php/guides/laravel/configuration/laravel-options/#closures-and-config-caching).

</Alert>
```php {filename:app/Exceptions/Sentry.php}
class Sentry
{
public static function tracesSampler(\Sentry\Tracing\SamplingContext $context): float
{
// Return a value between 0 and 1.
return $context->getParentSampled() ? 1.0 : 0.2;
}
}
```
Loading
Loading