Skip to content

Support sentry/sentry 2.3 and fix deprecations #298

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

Merged
merged 18 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 11 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
- ...
- Drop support for `sentry/sentry` < 2.3 (#298)
- Add support to `in_app_include` client option (#298)
- Remap `excluded_exception` option to use the new `IgnoreErrorIntegration` (#298)

## 3.3.0 (2020-01-14)
- Add support for Symfony 5.0 (#266, thanks to @Big-Shark)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php": "^7.1",
"jean85/pretty-package-versions": "^1.0",
"ocramius/package-versions": "^1.3.0",
"sentry/sdk": "^2.0",
"sentry/sdk": "^2.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This locks against sentry/sentry:^2.3. Because of this, this should be released as 3.4, to leave space for 3.3.x patches.

"symfony/config": "^3.4||^4.0||^5.0",
"symfony/console": "^3.4||^4.0||^5.0",
"symfony/dependency-injection": "^3.4||^4.0||^5.0",
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ parameters:
- test/
ignoreErrors:
- "/Call to function method_exists.. with 'Symfony.+' and 'getRootNode' will always evaluate to false./"
- "/Call to function method_exists.. with 'Sentry..Options' and 'getClassSerializers' will always evaluate to false./"
- "/Call to function method_exists.. with 'Sentry..Options' and 'getMaxRequestBodySi...' will always evaluate to false./"
- '/Class PHPUnit_Framework_TestCase not found/'
- '/Symfony\\Component\\HttpKernel\\Event\\(GetResponse|FilterController)Event not found.$/'
-
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
backupGlobals="false"
bootstrap="vendor/autoload.php"
cacheResult="false"
processIsolation="true"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used an annotation directly on the only test that needs it, the End2EndTest, so now the test suite is faster.

>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
Expand Down
4 changes: 2 additions & 2 deletions src/Command/SentryTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sentry\SentryBundle\Command;

use Sentry\SentryBundle\SentryBundle;
use Sentry\SentrySdk;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -16,7 +16,7 @@ public function __construct()

protected function execute(InputInterface $input, OutputInterface $output): int
{
$currentHub = SentryBundle::getCurrentHub();
$currentHub = SentrySdk::getCurrentHub();
$client = $currentHub->getClient();

if (! $client) {
Expand Down
21 changes: 0 additions & 21 deletions src/DependencyInjection/ClientBuilderConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
namespace Sentry\SentryBundle\DependencyInjection;

use Sentry\ClientBuilderInterface;
use Sentry\Integration\ErrorListenerIntegration;
use Sentry\Integration\ExceptionListenerIntegration;
use Sentry\Integration\IntegrationInterface;
use Sentry\SentryBundle\SentryBundle;

class ClientBuilderConfigurator
Expand All @@ -14,23 +11,5 @@ public static function configure(ClientBuilderInterface $clientBuilder): void
{
$clientBuilder->setSdkIdentifier(SentryBundle::SDK_IDENTIFIER);
$clientBuilder->setSdkVersion(SentryBundle::getSdkVersion());

$options = $clientBuilder->getOptions();
if (! $options->hasDefaultIntegrations()) {
return;
}

$integrations = $options->getIntegrations();
$options->setIntegrations(array_filter($integrations, static function (IntegrationInterface $integration): bool {
if ($integration instanceof ErrorListenerIntegration) {
return false;
}

if ($integration instanceof ExceptionListenerIntegration) {
return false;
}

return true;
}));
}
}
48 changes: 18 additions & 30 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Sentry\SentryBundle\DependencyInjection;

use Jean85\PrettyVersions;
use PackageVersions\Versions;
use Sentry\Options;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
Expand Down Expand Up @@ -58,14 +57,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->validate()
->ifTrue($this->isNotAValidCallback())
->thenInvalid('Expecting callable or service reference, got %s');
if (PrettyVersions::getVersion('sentry/sentry')->getPrettyVersion() !== '2.0.0') {
$optionsChildNodes->booleanNode('capture_silenced_errors');
}
if ($this->classSerializersAreSupported()) {
$optionsChildNodes->arrayNode('class_serializers')
->defaultValue([])
->prototype('scalar');
}
Comment on lines -61 to -68
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here (and below) I've removed all the conditions that were needed for backward compatibility with sentry/sentry 2.0, it's no longer needed.

$optionsChildNodes->booleanNode('capture_silenced_errors');
$optionsChildNodes->arrayNode('class_serializers')
->defaultValue([])
->prototype('scalar');
$optionsChildNodes->integerNode('context_lines')
->min(0)
->max(99);
Expand All @@ -75,14 +70,19 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue('%kernel.environment%')
->cannotBeEmpty();
$optionsChildNodes->scalarNode('error_types');
$optionsChildNodes->arrayNode('in_app_include')
->defaultValue([
'%kernel.project_dir%',
])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that this behavior doesn't work well, and doesn't reproduce the previous behavior, due to getsentry/sentry-php#953 😞

->prototype('scalar');
$optionsChildNodes->arrayNode('in_app_exclude')
->defaultValue([
'%kernel.cache_dir%',
'%kernel.project_dir%/vendor',
])
->prototype('scalar');
$optionsChildNodes->arrayNode('excluded_exceptions')
->defaultValue($defaultValues->getExcludedExceptions())
->defaultValue([])
->prototype('scalar');
$optionsChildNodes->scalarNode('http_proxy');
$optionsChildNodes->arrayNode('integrations')
Expand All @@ -97,15 +97,13 @@ public function getConfigTreeBuilder(): TreeBuilder
})
->thenInvalid('Expecting service reference, got "%s"');
$optionsChildNodes->scalarNode('logger');
if ($this->maxRequestBodySizeIsSupported()) {
$optionsChildNodes->enumNode('max_request_body_size')
->values([
'none',
'small',
'medium',
'always',
]);
}
$optionsChildNodes->enumNode('max_request_body_size')
->values([
'none',
'small',
'medium',
'always',
]);
$optionsChildNodes->integerNode('max_breadcrumbs')
->min(1);
$optionsChildNodes->integerNode('max_value_length')
Expand All @@ -114,7 +112,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue($defaultValues->getPrefixes())
->prototype('scalar');
$optionsChildNodes->scalarNode('project_root')
->defaultValue('%kernel.project_dir%');
->setDeprecated();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a double deprecation, it will pop up during cache warmup, so the user can be notified before reaching production.

$optionsChildNodes->scalarNode('release')
->defaultValue(Versions::getVersion(Versions::ROOT_PACKAGE_NAME))
->info('Release version to be reported to sentry, see https://docs.sentry.io/workflow/releases/?platform=php')
Expand Down Expand Up @@ -193,14 +191,4 @@ private function isNotAValidCallback(): \Closure
return true;
};
}

private function classSerializersAreSupported(): bool
{
return method_exists(Options::class, 'getClassSerializers');
}

private function maxRequestBodySizeIsSupported(): bool
{
return method_exists(Options::class, 'getMaxRequestBodySize');
}
}
32 changes: 32 additions & 0 deletions src/DependencyInjection/IntegrationFilterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Sentry\SentryBundle\DependencyInjection;

use Sentry\Integration\ErrorListenerIntegration;
use Sentry\Integration\ExceptionListenerIntegration;
use Sentry\Integration\IntegrationInterface;

class IntegrationFilterFactory
{
public static function create(array $integrationsFromConfiguration): callable
{
return function (array $integrations) use ($integrationsFromConfiguration) {
$allIntegrations = array_merge($integrations, $integrationsFromConfiguration);

return array_filter(
$allIntegrations,
static function (IntegrationInterface $integration): bool {
if ($integration instanceof ErrorListenerIntegration) {
return false;
}

if ($integration instanceof ExceptionListenerIntegration) {
return false;
}

return true;
}
);
};
}
}
22 changes: 19 additions & 3 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Monolog\Logger as MonologLogger;
use Sentry\ClientBuilderInterface;
use Sentry\Integration\IgnoreErrorsIntegration;
use Sentry\Monolog\Handler;
use Sentry\Options;
use Sentry\SentryBundle\ErrorTypesParser;
use Sentry\SentryBundle\EventListener\ErrorListener;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -63,7 +65,6 @@ private function passConfigurationToOptions(ContainerBuilder $container, array $
'default_integrations',
'enable_compression',
'environment',
'excluded_exceptions',
'http_proxy',
'logger',
'max_request_body_size',
Expand All @@ -90,6 +91,10 @@ private function passConfigurationToOptions(ContainerBuilder $container, array $
$options->addMethodCall('setInAppExcludedPaths', [$processedOptions['in_app_exclude']]);
}

if (\array_key_exists('in_app_include', $processedOptions)) {
$options->addMethodCall('setInAppIncludedPaths', [$processedOptions['in_app_include']]);
}

if (\array_key_exists('error_types', $processedOptions)) {
$parsedValue = (new ErrorTypesParser($processedOptions['error_types']))->parse();
$options->addMethodCall('setErrorTypes', [$parsedValue]);
Expand All @@ -114,14 +119,25 @@ private function passConfigurationToOptions(ContainerBuilder $container, array $
$options->addMethodCall('setClassSerializers', [$classSerializers]);
}

$integrations = [];
if (\array_key_exists('integrations', $processedOptions)) {
$integrations = [];
foreach ($processedOptions['integrations'] as $integrationName) {
$integrations[] = new Reference(substr($integrationName, 1));
}
}

$options->addMethodCall('setIntegrations', [$integrations]);
if (\array_key_exists('excluded_exceptions', $processedOptions) && $processedOptions['excluded_exceptions']) {
$ignoreOptions = [
'ignore_exceptions' => $processedOptions['excluded_exceptions'],
];

$integrations[] = new Definition(IgnoreErrorsIntegration::class, [$ignoreOptions]);
}

$integrationsCallable = new Definition('callable', [$integrations]);
$integrationsCallable->setFactory([IntegrationFilterFactory::class, 'create']);

$options->addMethodCall('setIntegrations', [$integrationsCallable]);
}

private function valueToCallable($value)
Expand Down
4 changes: 2 additions & 2 deletions src/EventListener/ConsoleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sentry\SentryBundle\EventListener;

use Sentry\SentryBundle\SentryBundle;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
Expand Down Expand Up @@ -38,7 +38,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
$commandName = $command->getName();
}

SentryBundle::getCurrentHub()
SentrySdk::getCurrentHub()
->configureScope(static function (Scope $scope) use ($commandName): void {
$scope->setTag('command', $commandName ?? 'N/A');
});
Expand Down
10 changes: 5 additions & 5 deletions src/EventListener/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sentry\SentryBundle\EventListener;

use Sentry\SentryBundle\SentryBundle;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function onKernelRequest(RequestEvent $event): void
return;
}

$currentClient = SentryBundle::getCurrentHub()->getClient();
$currentClient = SentrySdk::getCurrentHub()->getClient();
if (null === $currentClient || ! $currentClient->getOptions()->shouldSendDefaultPii()) {
return;
}
Expand All @@ -79,9 +79,9 @@ public function onKernelRequest(RequestEvent $event): void

$userData['ip_address'] = $event->getRequest()->getClientIp();

SentryBundle::getCurrentHub()
SentrySdk::getCurrentHub()
->configureScope(function (Scope $scope) use ($userData): void {
$scope->setUser($userData);
$scope->setUser($userData, true);
});
}

Expand All @@ -97,7 +97,7 @@ public function onKernelController(ControllerEvent $event): void

$matchedRoute = (string) $event->getRequest()->attributes->get('_route');

SentryBundle::getCurrentHub()
SentrySdk::getCurrentHub()
->configureScope(function (Scope $scope) use ($matchedRoute): void {
$scope->setTag('route', $matchedRoute);
});
Expand Down
6 changes: 3 additions & 3 deletions src/EventListener/SubRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sentry\SentryBundle\EventListener;

use Sentry\SentryBundle\SentryBundle;
use Sentry\SentrySdk;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand All @@ -24,7 +24,7 @@ public function onKernelRequest(RequestEvent $event): void
return;
}

SentryBundle::getCurrentHub()->pushScope();
SentrySdk::getCurrentHub()->pushScope();
}

/**
Expand All @@ -38,6 +38,6 @@ public function onKernelFinishRequest(FinishRequestEvent $event): void
return;
}

SentryBundle::getCurrentHub()->popScope();
SentrySdk::getCurrentHub()->popScope();
}
}
15 changes: 2 additions & 13 deletions src/SentryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Jean85\PrettyVersions;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -23,24 +22,14 @@ public static function getSdkVersion(): string
*/
public static function getCurrentHub(): HubInterface
{
if (class_exists(SentrySdk::class)) {
return SentrySdk::getCurrentHub();
}

return Hub::getCurrent();
return SentrySdk::getCurrentHub();
}

/**
* This method avoids deprecations with sentry/sentry:^2.2
*/
public static function setCurrentHub(HubInterface $hub): void
{
if (class_exists(SentrySdk::class)) {
SentrySdk::setCurrentHub($hub);

return;
}

Hub::setCurrent($hub);
SentrySdk::setCurrentHub($hub);
}
Comment on lines 23 to 34
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those two methods are left here for BC.

}
Loading