Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handler integrations option #1439

Closed
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- feat: Add `error_handler_integrations` option (#1439)

## 3.12.0 (2022-11-22)

- feat: Add `before_send_transaction` option (#1424)
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ parameters:
count: 1
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:hasErrorHandlerIntegrations\\(\\) should return bool but returns mixed\\.$#"
count: 1
path: src/Options.php

-
message: "#^Method Sentry\\\\Options\\:\\:isCompressionEnabled\\(\\) should return bool but returns mixed\\.$#"
count: 1
Expand Down
22 changes: 11 additions & 11 deletions src/Integration/IntegrationRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,19 @@ private function getIntegrationsToSetup(Options $options): array
*/
private function getDefaultIntegrations(Options $options): array
{
if (!$options->hasDefaultIntegrations()) {
return [];
}
$integrations = [];

$integrations = [
new RequestIntegration(),
new TransactionIntegration(),
new FrameContextifierIntegration(),
new EnvironmentIntegration(),
new ModulesIntegration(),
];
if ($options->hasDefaultIntegrations()) {
$integrations = [
new RequestIntegration(),
new TransactionIntegration(),
new FrameContextifierIntegration(),
new EnvironmentIntegration(),
new ModulesIntegration(),
];
}

if (null !== $options->getDsn()) {
if ($options->hasErrorHandlerIntegrations() && null !== $options->getDsn()) {
array_unshift($integrations, new ExceptionListenerIntegration(), new ErrorListenerIntegration(), new FatalErrorListenerIntegration());
}

Expand Down
24 changes: 24 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,28 @@ public function setDefaultIntegrations(bool $enable): void
$this->options = $this->resolver->resolve($options);
}

/**
* Returns whether the error handler integrations are enabled or fallbacks
* to whether the default integrations are enabled when null.
*/
public function hasErrorHandlerIntegrations(): bool
{
return $this->options['error_handler_integrations'] ?? $this->hasDefaultIntegrations();
}

/**
* Sets whether the error handler integrations are enabled.
*
* @param ?bool $enable Flag indicating whether the error handler integrations should be enabled, fallbacks
* to "default_integrations" when null
*/
public function setErrorHandlerIntegrations(?bool $enable): void
{
$options = array_merge($this->options, ['error_handler_integrations' => $enable]);

$this->options = $this->resolver->resolve($options);
}

/**
* Gets the max length for values in the event payload.
*/
Expand Down Expand Up @@ -811,6 +833,7 @@ private function configureOptions(OptionsResolver $resolver): void
$resolver->setDefaults([
'integrations' => [],
'default_integrations' => true,
'error_handler_integrations' => null,
'send_attempts' => 0,
'prefixes' => array_filter(explode(\PATH_SEPARATOR, get_include_path() ?: '')),
'sample_rate' => 1,
Expand Down Expand Up @@ -874,6 +897,7 @@ private function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('integrations', ['Sentry\\Integration\\IntegrationInterface[]', 'callable']);
$resolver->setAllowedTypes('send_default_pii', 'bool');
$resolver->setAllowedTypes('default_integrations', 'bool');
$resolver->setAllowedTypes('error_handler_integrations', ['bool', 'null']);
$resolver->setAllowedTypes('max_value_length', 'int');
$resolver->setAllowedTypes('http_proxy', ['null', 'string']);
$resolver->setAllowedTypes('http_connect_timeout', ['int', 'float']);
Expand Down
40 changes: 40 additions & 0 deletions tests/Integration/IntegrationRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,46 @@ public function setupOnce(): void
ModulesIntegration::class => new ModulesIntegration(),
],
];

yield 'Default integrations and no error handler integrations' => [
new Options([
'dsn' => 'http://public@example.com/sentry/1',
'default_integrations' => true,
'error_handler_integrations' => false,
]),
[
'The "Sentry\\Integration\\RequestIntegration" integration has been installed.',
'The "Sentry\\Integration\\TransactionIntegration" integration has been installed.',
'The "Sentry\\Integration\\FrameContextifierIntegration" integration has been installed.',
'The "Sentry\\Integration\\EnvironmentIntegration" integration has been installed.',
'The "Sentry\\Integration\\ModulesIntegration" integration has been installed.',
],
[
RequestIntegration::class => new RequestIntegration(),
TransactionIntegration::class => new TransactionIntegration(),
FrameContextifierIntegration::class => new FrameContextifierIntegration(),
EnvironmentIntegration::class => new EnvironmentIntegration(),
ModulesIntegration::class => new ModulesIntegration(),
],
];

yield 'No default integrations and error handler integrations' => [
new Options([
'dsn' => 'http://public@example.com/sentry/1',
'default_integrations' => false,
'error_handler_integrations' => true,
]),
[
'The "Sentry\\Integration\\ExceptionListenerIntegration" integration has been installed.',
'The "Sentry\\Integration\\ErrorListenerIntegration" integration has been installed.',
'The "Sentry\\Integration\\FatalErrorListenerIntegration" integration has been installed.',
],
[
ExceptionListenerIntegration::class => new ExceptionListenerIntegration(),
ErrorListenerIntegration::class => new ErrorListenerIntegration(),
FatalErrorListenerIntegration::class => new FatalErrorListenerIntegration(),
],
];
}

/**
Expand Down
44 changes: 44 additions & 0 deletions tests/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,50 @@ static function (): void {},
null,
null,
];

yield [
'default_integrations',
false,
'hasDefaultIntegrations',
'setDefaultIntegrations',
null,
null,
];

yield [
'error_handler_integrations',
true,
'hasErrorHandlerIntegrations',
'setErrorHandlerIntegrations',
null,
null,
];
}

/**
* @dataProvider errorHandlerIntegrationsOptionDataProvider
*/
public function testErrorHandlerIntegrationsFallback(
bool $defaultIntegrationsValue,
?bool $errorHandlerIntegrationsValue,
bool $expectedValue
): void {
$options = new Options([
'default_integrations' => $defaultIntegrationsValue,
'error_handler_integrations' => $errorHandlerIntegrationsValue,
]);

$this->assertEquals($expectedValue, $options->hasErrorHandlerIntegrations());
}

public function errorHandlerIntegrationsOptionDataProvider(): iterable
{
yield [true, null, true];
yield [true, false, false];
yield [true, true, true];
yield [false, null, false];
yield [false, false, false];
yield [false, true, true];
}

/**
Expand Down