Skip to content

Commit

Permalink
CR
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Sep 24, 2020
1 parent da935d0 commit c6ccfdf
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 122 deletions.
10 changes: 5 additions & 5 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@
- Removed the `Stacktrace::toArray()` and `Stacktrace::jsonSerialize()` methods
- Removed the `SpoolTransport` class and the `SpoolInterface` interface with related implementation
- Made the `Event::__construct()` method `private`, use the named constructors instead
- The signature of `ClientInterface::captureEvent` changed to `ClientInterface::captureEvent(Event $event, ?EventHint $hint = null, ?Scope $scope = null)`
- The signature of `HubInterface::captureEvent` changed to `HubInterface::captureEvent(Event $event, ?EventHint $hint = null)`
- The signature of `captureEvent` changed to `captureEvent(Event $event, ?EventHint $hint = null)`
- The signature of `Scope::applyToEvent` changed to `Scope::applyToEvent(Event $event)`
- Global and scope event processors will no longer receive a second argument, callable should now have the signature `callable(Event $event)`
- The signature of `ClientInterface::captureEvent()` changed to `ClientInterface::captureEvent(Event $event, ?EventHint $hint = null, ?Scope $scope = null)`
- The signature of `HubInterface::captureEvent()` changed to `HubInterface::captureEvent(Event $event, ?EventHint $hint = null)`
- The signature of `captureEvent()` changed to `captureEvent(Event $event, ?EventHint $hint = null)`
- The signature of `Scope::applyToEvent()` changed to `Scope::applyToEvent(Event $event, ?EventHint $hint = null)`
- Global and scope event processors will now receive a `EventHint` as the second parameter, callable should now have the signature `callable(Event $event, EventHint $hint)`
31 changes: 21 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public function getOptions(): Options
public function captureMessage(string $message, ?Severity $level = null, ?Scope $scope = null): ?EventId
{
$event = Event::createEvent();

$event->setMessage($message);
$event->setLevel($level);

Expand Down Expand Up @@ -207,7 +206,7 @@ public function flush(?int $timeout = null): PromiseInterface
* Assembles an event and prepares it to be sent of to Sentry.
*
* @param Event $event The payload that will be converted to an Event
* @param EventHint|null $hint May contain additional information about the original exception
* @param EventHint|null $hint May contain additional information about the event
* @param Scope|null $scope Optional scope which enriches the Event
*
* @return Event|null The prepared event object or null if it must be discarded
Expand Down Expand Up @@ -243,7 +242,7 @@ private function prepareEvent(Event $event, ?EventHint $hint = null, ?Scope $sco

if (null !== $scope) {
$previousEvent = $event;
$event = $scope->applyToEvent($event);
$event = $scope->applyToEvent($event, $hint);

if (null === $event) {
$this->logger->info('The event will be discarded because one of the event processors returned "null".', ['event' => $previousEvent]);
Expand All @@ -266,17 +265,29 @@ private function prepareEvent(Event $event, ?EventHint $hint = null, ?Scope $sco
* Optionally adds a missing stacktrace to the Event if the client is configured to do so.
*
* @param Event $event The Event to add the missing stacktrace to
* @param EventHint|null $hint May contain additional information about the original exception
* @param EventHint|null $hint May contain additional information about the event
*/
private function addMissingStacktraceToEvent(Event $event, ?EventHint $hint): void
{
if ($this->options->shouldAttachStacktrace() && (null === $hint || null === $hint->exception) && null === $event->getStacktrace()) {
$event->setStacktrace($this->stacktraceBuilder->buildFromBacktrace(
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
__FILE__,
__LINE__ - 3
));
if (!$this->options->shouldAttachStacktrace()) {
return;
}

// If the hint contains an exception the stacktrace will be generated for that exception
if (null !== $hint && null !== $hint->exception) {
return;
}

// We should not add a stacktrace when the event already has one or contains exceptions
if (null !== $event->getStacktrace() || !empty($event->getExceptions())) {
return;
}

$event->setStacktrace($this->stacktraceBuilder->buildFromBacktrace(
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
__FILE__,
__LINE__ - 3
));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function captureLastError(?Scope $scope = null): ?EventId;
* Captures a new event using the provided data.
*
* @param Event $event The event being captured
* @param EventHint|null $hint May contain additional information about the original exception
* @param EventHint|null $hint May contain additional information about the event
* @param Scope|null $scope An optional scope keeping the state
*/
public function captureEvent(Event $event, ?EventHint $hint = null, ?Scope $scope = null): ?EventId;
Expand Down
8 changes: 8 additions & 0 deletions src/EventHint.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ final class EventHint
*/
public $stacktrace;

/**
* Any extra data that might be needed to process the event.
*
* @var array
*/
public $extra = [];

/**
* Create a EventHint instance from an array of values.
*
* @psalm-param array{
* exception?: \Throwable,
* stacktrace?: Event
* extra?: array
* } $hintData
*/
public static function fromArray(array $hintData): self
Expand Down
1 change: 0 additions & 1 deletion src/Monolog/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bub
protected function write(array $record): void
{
$event = Event::createEvent();

$event->setLevel(self::getSeverityFromLevel($record['level']));
$event->setMessage($record['message']);
$event->setLogger(sprintf('monolog.%s', $record['channel']));
Expand Down
2 changes: 1 addition & 1 deletion src/State/HubInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function captureException(\Throwable $exception): ?EventId;
* Captures a new event using the provided data.
*
* @param Event $event The event being captured
* @param EventHint|null $hint May contain additional information about the original exception
* @param EventHint|null $hint May contain additional information about the event
*/
public function captureEvent(Event $event, ?EventHint $hint = null): ?EventId;

Expand Down
10 changes: 8 additions & 2 deletions src/State/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Sentry\Breadcrumb;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\Severity;
use Sentry\Tracing\Span;
use Sentry\Tracing\Transaction;
Expand Down Expand Up @@ -305,7 +306,7 @@ public function clear(): self
*
* @param Event $event The event object that will be enriched with scope data
*/
public function applyToEvent(Event $event): ?Event
public function applyToEvent(Event $event, ?EventHint $hint = null): ?Event
{
$event->setFingerprint(array_merge($event->getFingerprint(), $this->fingerprint));

Expand Down Expand Up @@ -346,8 +347,13 @@ public function applyToEvent(Event $event): ?Event
$event->setContext($name, $data);
}

// We create a empty `EventHint` instance to allow processors to always recieve a `EventHint` instance even if there wasn't one
if (null === $hint) {
$hint = new EventHint();
}

foreach (array_merge(self::$globalEventProcessors, $this->eventProcessors) as $processor) {
$event = $processor($event);
$event = $processor($event, $hint);

if (null === $event) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function captureException(\Throwable $exception): ?EventId
/**
* Captures a new event using the provided data.
*
* @param Event $event The event being captured
* @param \Sentry\EventHint|null $hint May contain additional information about the original exception
* @param Event $event The event being captured
* @param EventHint|null $hint May contain additional information about the event
*/
function captureEvent(Event $event, ?EventHint $hint = null): ?EventId
{
Expand Down
1 change: 0 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public function testCaptureEvent(): void
->getClient();

$event = Event::createEvent();

$event->setTransaction('foo bar');
$event->setLevel(Severity::debug());
$event->setLogger('foo');
Expand Down
8 changes: 3 additions & 5 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,12 @@ public function testCaptureEvent(): void
{
$event = Event::createEvent($eventId = EventId::generate());

$event->setMessage('foo');

/** @var ClientInterface|MockObject $client */
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('captureEvent')
->with($event)
->willReturn($eventId);
->method('captureEvent')
->with($event)
->willReturn($eventId);

SentrySdk::getCurrentHub()->bindClient($client);

Expand Down
11 changes: 4 additions & 7 deletions tests/Integration/TransactionIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ public function setupOnceDataProvider(): \Generator
null,
];

yield [
(static function () {
$event = Event::createEvent();

$event->setTransaction('/foo/bar');
$event = Event::createEvent();
$event->setTransaction('/foo/bar');

return $event;
})(),
yield [
$event,
true,
[],
'/foo/bar',
Expand Down

0 comments on commit c6ccfdf

Please sign in to comment.