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

Updates for sentry/sentry 2.2 #273

Merged
merged 2 commits into from
Sep 23, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ package.xml
.idea
.php_cs.cache
docs/_build
.phpunit.result.cache
34 changes: 19 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,34 @@ php:

env:
matrix:
- LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.*
- LARAVEL=5.1.* TESTBENCH=3.1.* PHPUNIT=5.7.*
- LARAVEL=5.2.* TESTBENCH=3.2.* PHPUNIT=5.7.*
- LARAVEL=5.3.* TESTBENCH=3.3.* PHPUNIT=5.7.*
- LARAVEL=5.4.* TESTBENCH=3.4.* PHPUNIT=5.7.*
- LARAVEL=5.5.* TESTBENCH=3.5.* PHPUNIT=6.5.*
- LARAVEL=5.6.* TESTBENCH=3.6.* PHPUNIT=7.5.*
- LARAVEL=5.7.* TESTBENCH=3.7.* PHPUNIT=7.5.*
- LARAVEL=5.8.* TESTBENCH=3.8.* PHPUNIT=7.5.*
- LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.*
- LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.* SENTRY=^2.0
- LARAVEL=5.1.* TESTBENCH=3.1.* PHPUNIT=5.7.* SENTRY=^2.0
- LARAVEL=5.2.* TESTBENCH=3.2.* PHPUNIT=5.7.* SENTRY=^2.0
- LARAVEL=5.3.* TESTBENCH=3.3.* PHPUNIT=5.7.* SENTRY=^2.0
- LARAVEL=5.4.* TESTBENCH=3.4.* PHPUNIT=5.7.* SENTRY=^2.0
- LARAVEL=5.5.* TESTBENCH=3.5.* PHPUNIT=6.5.* SENTRY=^2.0
- LARAVEL=5.6.* TESTBENCH=3.6.* PHPUNIT=7.5.* SENTRY=^2.0
- LARAVEL=5.7.* TESTBENCH=3.7.* PHPUNIT=7.5.* SENTRY=^2.0
- LARAVEL=5.8.* TESTBENCH=3.8.* PHPUNIT=7.5.* SENTRY=^2.0
- LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.* SENTRY=^2.0
# We add one more test using the bleading edge of the sentry/sentry package
- LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.* SENTRY=dev-develop@dev

matrix:
fast_finish: true
allow_failures:
- php: 7.4snapshot
exclude:
- php: 7.1
env: LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.*
env: LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.* SENTRY=^2.0
- php: 7.1
env: LARAVEL=6.0.* TESTBENCH=3.9.* PHPUNIT=8.0.* SENTRY=dev-develop@dev
- php: 7.2
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.*
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.* SENTRY=^2.0
- php: 7.3
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.*
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.* SENTRY=^2.0
- php: 7.4snapshot
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.*
env: LARAVEL=5.0.* TESTBENCH=3.0.* PHPUNIT=4.8.* SENTRY=^2.0

cache:
directories:
Expand All @@ -51,7 +55,7 @@ jobs:

before_install:
- if [ "$USE_COMPOSER_JSON" != "1" ]; then composer remove friendsofphp/php-cs-fixer --dev --no-update; fi;
- if [ "$USE_COMPOSER_JSON" != "1" ]; then composer require laravel/framework:$LARAVEL illuminate/support:$LARAVEL orchestra/testbench:$TESTBENCH phpunit/phpunit:$PHPUNIT --no-update --no-interaction --dev; fi;
- if [ "$USE_COMPOSER_JSON" != "1" ]; then composer require laravel/framework:$LARAVEL illuminate/support:$LARAVEL orchestra/testbench:$TESTBENCH phpunit/phpunit:$PHPUNIT sentry/sentry:$SENTRY --no-update --no-interaction --dev; fi;

install:
- travis_retry composer install --no-suggest --no-interaction --prefer-dist --no-progress
Expand Down
5 changes: 2 additions & 3 deletions src/Sentry/Laravel/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use RuntimeException;
use Sentry\State\Scope;
use Sentry\Breadcrumb;
use Sentry\State\Hub;

class EventHandler
{
Expand Down Expand Up @@ -115,7 +114,7 @@ public function subscribeQueueEvents(QueueManager $queue)
Integration::flushEvents();

// We have added a scope when a job starts processing
Hub::getCurrent()->popScope();
Integration::getCurrentHub()->popScope();
});

foreach (static::$queueEventHandlerMap as $eventName => $handler) {
Expand Down Expand Up @@ -283,7 +282,7 @@ protected function authenticatedHandler(Authenticated $event)
protected function queueJobProcessingHandler(JobProcessing $event)
{
// When a job starts, we want to push a new scope
Hub::getCurrent()->pushScope();
Integration::getCurrentHub()->pushScope();

$job = [
'job' => $event->job->getName(),
Expand Down
71 changes: 54 additions & 17 deletions src/Sentry/Laravel/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Sentry\Laravel;

use Sentry\FlushableClientInterface;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use function Sentry\addBreadcrumb;
use function Sentry\configureScope;
use Sentry\Breadcrumb;
use Sentry\Event;
use Sentry\Client;
use Sentry\Integration\IntegrationInterface;
use Sentry\State\Hub;
use Sentry\State\Scope;
use Sentry\Transport\HttpTransport;

class Integration implements IntegrationInterface
{
Expand All @@ -25,7 +26,7 @@ class Integration implements IntegrationInterface
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(function (Event $event): Event {
$self = Hub::getCurrent()->getIntegration(self::class);
$self = static::getCurrentHub()->getIntegration(self::class);

if (!$self instanceof self) {
return $event;
Expand All @@ -44,7 +45,7 @@ public function setupOnce(): void
*/
public static function addBreadcrumb(Breadcrumb $breadcrumb): void
{
$self = Hub::getCurrent()->getIntegration(self::class);
$self = static::getCurrentHub()->getIntegration(self::class);

if (!$self instanceof self) {
return;
Expand All @@ -60,7 +61,7 @@ public static function addBreadcrumb(Breadcrumb $breadcrumb): void
*/
public static function configureScope(callable $callback): void
{
$self = Hub::getCurrent()->getIntegration(self::class);
$self = static::getCurrentHub()->getIntegration(self::class);

if (!$self instanceof self) {
return;
Expand Down Expand Up @@ -93,21 +94,57 @@ public static function setTransaction($transaction): void
*/
public static function flushEvents(): void
{
$client = Hub::getCurrent()->getClient();
$client = static::getCurrentHub()->getClient();

if ($client instanceof Client) {
$transportProperty = new \ReflectionProperty(Client::class, 'transport');
$transportProperty->setAccessible(true);
if ($client instanceof FlushableClientInterface) {
$client->flush();
}
}

$transport = $transportProperty->getValue($client);
/**
* Gets the current hub. If it's not initialized then creates a new instance
* and sets it as current hub.
*
* The is here for legacy reasons where we used the Hub directly as a singleton.
*
* @TODO: This method should be removed and replaced with calls to `SentrySdk::getCurrentHub()` directly once
* `sentry/sentry` 3.0 is released and pinned as an dependency.
*
* @internal This is not part of the public API and is here temporarily.
*
* @return \Sentry\State\HubInterface
*/
public static function getCurrentHub(): HubInterface
{
if (class_exists(SentrySdk::class)) {
SentrySdk::getCurrentHub();
}

if ($transport instanceof HttpTransport) {
$closure = \Closure::bind(function () {
$this->cleanupPendingRequests();
}, $transport, $transport);
return Hub::getCurrent();
}

$closure();
}
/**
* Sets the current hub.
*
* The is here for legacy reasons where we used the Hub directly as a singleton.
*
* @TODO: This method should be removed and replaced with calls to `SentrySdk::getCurrentHub()` directly once
* `sentry/sentry` 3.0 is released and pinned as an dependency.
*
* @internal This is not part of the public API and is here temporarily.
*
* @param \Sentry\State\HubInterface $hub
*
* @return void
*/
public static function setCurrentHub(HubInterface $hub): void
{
if (class_exists(SentrySdk::class)) {
SentrySdk::setCurrentHub($hub);

return;
}

Hub::setCurrent($hub);
}
}
8 changes: 2 additions & 6 deletions src/Sentry/Laravel/SentryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,17 @@ protected function getLogLevel($logLevel)
switch ($logLevel) {
case Logger::DEBUG:
return Severity::debug();
case Logger::INFO:
return Severity::info();
case Logger::NOTICE:
case Logger::INFO:
return Severity::info();
case Logger::WARNING:
return Severity::warning();
case Logger::ERROR:
return Severity::error();
case Logger::CRITICAL:
return Severity::fatal();
case Logger::ALERT:
return Severity::fatal();
case Logger::EMERGENCY:
case Logger::CRITICAL:
return Severity::fatal();

}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ protected function configureAndRegisterClient(): void
$clientBuilder->setSdkIdentifier(Version::SDK_IDENTIFIER);
$clientBuilder->setSdkVersion(Version::SDK_VERSION);

Hub::setCurrent(new Hub($clientBuilder->getClient()));
$hub = new Hub($clientBuilder->getClient());

return Hub::getCurrent();
Integration::setCurrentHub($hub);

return $hub;
});

$this->app->alias(static::$abstract, HubInterface::class);
Expand Down
7 changes: 3 additions & 4 deletions test/Sentry/IntegrationsOptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Sentry\Laravel\Tests;

use Sentry\State\Hub;
use Sentry\Integration\IntegrationInterface;

class IntegrationsOptionTest extends SentryLaravelTestCase
Expand All @@ -24,7 +23,7 @@ public function testCustomIntegrationIsResolvedFromContainerByAlias()
],
]);

$this->assertNotNull(Hub::getCurrent()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
$this->assertNotNull($this->getHubFromContainer()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
}

public function testCustomIntegrationIsResolvedFromContainerByClass()
Expand All @@ -35,7 +34,7 @@ public function testCustomIntegrationIsResolvedFromContainerByClass()
],
]);

$this->assertNotNull(Hub::getCurrent()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
$this->assertNotNull($this->getHubFromContainer()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
}

public function testCustomIntegrationByInstance()
Expand All @@ -46,7 +45,7 @@ public function testCustomIntegrationByInstance()
],
]);

$this->assertNotNull(Hub::getCurrent()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
$this->assertNotNull($this->getHubFromContainer()->getClient()->getIntegration(IntegrationsOptionTestIntegrationStub::class));
}

/**
Expand Down
19 changes: 15 additions & 4 deletions test/Sentry/SentryLaravelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Sentry\Laravel\Tests;

use Sentry\State\Scope;
use Sentry\State\HubInterface;
use Sentry\Laravel\ServiceProvider;
use Orchestra\Testbench\TestCase as LaravelTestCase;
Expand Down Expand Up @@ -47,12 +46,24 @@ protected function dispatchLaravelEvent($event, array $payload = [])
: $dispatcher->fire($event, $payload);
}

protected function getScope(HubInterface $hub): Scope
protected function getHubFromContainer(): HubInterface
{
$method = new \ReflectionMethod($hub, 'getScope');
return $this->app->make('sentry');
}

protected function getCurrentBreadcrumbs(): array
{
$hub = $this->getHubFromContainer();

$method = new \ReflectionMethod($hub, 'getScope');
$method->setAccessible(true);

return $method->invoke($hub);
/** @var \Sentry\State\Scope $scope */
$scope = $method->invoke($hub);

$property = new \ReflectionProperty($scope, 'breadcrumbs');
$property->setAccessible(true);

return $property->getValue($scope);
}
}
6 changes: 2 additions & 4 deletions test/Sentry/SqlBindingsInBreadcrumbsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Sentry\Laravel\Tests;

use Sentry\State\Hub;

class SqlBindingsInBreadcrumbsTest extends SentryLaravelTestCase
{
public function testSqlBindingsAreRecordedWhenEnabled()
Expand All @@ -21,7 +19,7 @@ public function testSqlBindingsAreRecordedWhenEnabled()
'test',
]);

$breadcrumbs = $this->getScope(Hub::getCurrent())->getBreadcrumbs();
$breadcrumbs = $this->getCurrentBreadcrumbs();

/** @var \Sentry\Breadcrumb $lastBreadcrumb */
$lastBreadcrumb = end($breadcrumbs);
Expand All @@ -45,7 +43,7 @@ public function testSqlBindingsAreRecordedWhenDisabled()
'test',
]);

$breadcrumbs = $this->getScope(Hub::getCurrent())->getBreadcrumbs();
$breadcrumbs = $this->getCurrentBreadcrumbs();

/** @var \Sentry\Breadcrumb $lastBreadcrumb */
$lastBreadcrumb = end($breadcrumbs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Sentry\Laravel\Tests;

use Sentry\State\Hub;
use Illuminate\Config\Repository;

class SqlBindingsInBreadcrumbsWithOldConfigKeyDisabledTest extends SentryLaravelTestCase
Expand All @@ -29,7 +28,7 @@ public function testSqlBindingsAreRecordedWhenDisabledByOldConfigKey()
'test',
]);

$breadcrumbs = $this->getScope(Hub::getCurrent())->getBreadcrumbs();
$breadcrumbs = $this->getCurrentBreadcrumbs();

/** @var \Sentry\Breadcrumb $lastBreadcrumb */
$lastBreadcrumb = end($breadcrumbs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Sentry\Laravel\Tests;

use Sentry\State\Hub;
use Illuminate\Config\Repository;

class SqlBindingsInBreadcrumbsWithOldConfigKeyEnabledTest extends SentryLaravelTestCase
Expand All @@ -29,7 +28,7 @@ public function testSqlBindingsAreRecordedWhenEnabledByOldConfigKey()
'test',
]);

$breadcrumbs = $this->getScope(Hub::getCurrent())->getBreadcrumbs();
$breadcrumbs = $this->getCurrentBreadcrumbs();

/** @var \Sentry\Breadcrumb $lastBreadcrumb */
$lastBreadcrumb = end($breadcrumbs);
Expand Down