-
-
Notifications
You must be signed in to change notification settings - Fork 463
[2.0] feat: Add global SDK #680
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ | |
| "bin/sentry" | ||
| ], | ||
| "autoload": { | ||
| "files": ["src/Sdk.php"], | ||
| "psr-4" : { | ||
| "Sentry\\" : "src/" | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?php | ||
|
|
||
| namespace Sentry; | ||
|
|
||
| use Sentry\Breadcrumbs\Breadcrumb; | ||
| use Sentry\State\Hub; | ||
|
|
||
| /** | ||
| * Creates a new Client and Hub which will be set as current. | ||
| * | ||
| * @param array $options | ||
| */ | ||
| function init(array $options = []): void | ||
| { | ||
| Hub::setCurrent(new Hub(ClientBuilder::create($options)->getClient())); | ||
| } | ||
|
|
||
| /** | ||
| * Captures a message event and sends it to Sentry. | ||
| * | ||
| * @param string $message The message | ||
| * @param Severity $level The severity level of the message | ||
| * | ||
| * @return null|string | ||
| */ | ||
| function captureMessage(string $message, ?Severity $level = null): ?string | ||
| { | ||
| return Hub::getCurrent()->captureMessage($message, $level); | ||
| } | ||
|
|
||
| /** | ||
| * Captures an exception event and sends it to Sentry. | ||
| * | ||
| * @param \Throwable $exception The exception | ||
| * | ||
| * @return null|string | ||
| */ | ||
| function captureException(\Throwable $exception): ?string | ||
| { | ||
| return Hub::getCurrent()->captureException($exception); | ||
| } | ||
|
|
||
| /** | ||
| * Captures a new event using the provided data. | ||
| * | ||
| * @param array $payload The data of the event being captured | ||
| * | ||
| * @return null|string | ||
| */ | ||
| function captureEvent(array $payload): ?string | ||
| { | ||
| return Hub::getCurrent()->captureEvent($payload); | ||
| } | ||
|
|
||
| /** | ||
| * Records a new breadcrumb which will be attached to future events. They | ||
| * will be added to subsequent events to provide more context on user's | ||
| * actions prior to an error or crash. | ||
| * | ||
| * @param Breadcrumb $breadcrumb The breadcrumb to record | ||
| */ | ||
| function addBreadcrumb(Breadcrumb $breadcrumb): void | ||
| { | ||
| Hub::getCurrent()->addBreadcrumb($breadcrumb); | ||
| } | ||
|
|
||
| /** | ||
| * Calls the given callback passing to it the current scope so that any | ||
| * operation can be run within its context. | ||
| * | ||
| * @param callable $callback The callback to be executed | ||
| */ | ||
| function configureScope(callable $callback): void | ||
| { | ||
| Hub::getCurrent()->configureScope($callback); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new scope with and executes the given operation within. The scope | ||
| * is automatically removed once the operation finishes or throws. | ||
| * | ||
| * @param callable $callback The callback to be executed | ||
| */ | ||
| function withScope(callable $callback): void | ||
| { | ||
| Hub::getCurrent()->withScope($callback); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Tests; | ||
|
|
||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Sentry\Breadcrumbs\Breadcrumb; | ||
| use Sentry\ClientInterface; | ||
| use Sentry\State\Hub; | ||
| use function Sentry\addBreadcrumb; | ||
| use function Sentry\captureEvent; | ||
| use function Sentry\captureException; | ||
| use function Sentry\captureMessage; | ||
| use function Sentry\configureScope; | ||
| use function Sentry\init; | ||
| use function Sentry\withScope; | ||
|
|
||
| class SdkTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| init(); | ||
| } | ||
|
|
||
| public function testInit(): void | ||
| { | ||
| $this->assertNotNull(Hub::getCurrent()->getClient()); | ||
| } | ||
|
|
||
| public function testCaptureMessage(): void | ||
| { | ||
| /** @var ClientInterface|MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->once()) | ||
| ->method('captureMessage') | ||
| ->with('foo', [], ['level' => null]) | ||
| ->willReturn('92db40a886c0458288c7c83935a350ef'); | ||
|
|
||
| Hub::getCurrent()->bindClient($client); | ||
| $this->assertEquals($client, Hub::getCurrent()->getClient()); | ||
| $this->assertEquals('92db40a886c0458288c7c83935a350ef', captureMessage('foo')); | ||
| } | ||
|
|
||
| public function testCaptureException(): void | ||
| { | ||
| $exception = new \RuntimeException('foo'); | ||
|
|
||
| /** @var ClientInterface|MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->once()) | ||
| ->method('captureException') | ||
| ->with($exception) | ||
| ->willReturn('2b867534eead412cbdb882fd5d441690'); | ||
|
|
||
| Hub::getCurrent()->bindClient($client); | ||
|
|
||
| $this->assertEquals('2b867534eead412cbdb882fd5d441690', captureException($exception)); | ||
| } | ||
|
|
||
| public function testCaptureEvent(): void | ||
| { | ||
| /** @var ClientInterface|MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->once()) | ||
| ->method('capture') | ||
| ->with(['message' => 'test']) | ||
| ->willReturn('2b867534eead412cbdb882fd5d441690'); | ||
|
|
||
| Hub::getCurrent()->bindClient($client); | ||
|
|
||
| $this->assertEquals('2b867534eead412cbdb882fd5d441690', captureEvent(['message' => 'test'])); | ||
| } | ||
|
|
||
| public function testAddBreadcrumb(): void | ||
| { | ||
| $breadcrumb = new Breadcrumb(Breadcrumb::LEVEL_ERROR, Breadcrumb::TYPE_ERROR, 'error_reporting'); | ||
|
|
||
| /** @var ClientInterface|MockObject $client */ | ||
| $client = $this->createMock(ClientInterface::class); | ||
| $client->expects($this->once()) | ||
| ->method('addBreadcrumb') | ||
| ->with($breadcrumb, Hub::getCurrent()->getScope()); | ||
|
|
||
| Hub::getCurrent()->bindClient($client); | ||
| addBreadcrumb($breadcrumb); | ||
| } | ||
|
|
||
| public function testWithScope(): void | ||
| { | ||
| $callbackInvoked = false; | ||
|
|
||
| withScope(function () use (&$callbackInvoked): void { | ||
| $callbackInvoked = true; | ||
| }); | ||
|
|
||
| $this->assertTrue($callbackInvoked); | ||
| } | ||
|
|
||
| public function configureScope(): void | ||
| { | ||
| $callbackInvoked = false; | ||
|
|
||
| configureScope(function () use (&$callbackInvoked): void { | ||
| $callbackInvoked = true; | ||
| }); | ||
|
|
||
| $this->assertTrue($callbackInvoked); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.