Skip to content

Commit b05c689

Browse files
committed
review
1 parent e873c31 commit b05c689

7 files changed

Lines changed: 44 additions & 31 deletions

File tree

src/ClientReport/ClientReportAggregator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function flush(): void
5858
$reports = [];
5959
foreach ($this->reports as $category => $reasons) {
6060
foreach ($reasons as $reason => $quantity) {
61-
$reports[] = new ClientReport($category, $reason, $quantity);
61+
$reports[] = new DiscardedEvent($category, $reason, $quantity);
6262
}
6363
}
6464
$event = Event::createClientReport();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Sentry\ClientReport;
66

7-
class ClientReport
7+
class DiscardedEvent
88
{
99
/**
1010
* @var string

src/Event.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Sentry;
66

7-
use Sentry\ClientReport\ClientReport;
7+
use Sentry\ClientReport\DiscardedEvent;
88
use Sentry\Context\OsContext;
99
use Sentry\Context\RuntimeContext;
1010
use Sentry\Logs\Log;
@@ -212,7 +212,7 @@ final class Event
212212
private $profile;
213213

214214
/**
215-
* @var ClientReport[]
215+
* @var DiscardedEvent[]
216216
*/
217217
private $clientReports = [];
218218

@@ -991,7 +991,7 @@ public function getTraceId(): ?string
991991
}
992992

993993
/**
994-
* @param ClientReport[] $clientReports
994+
* @param DiscardedEvent[] $clientReports
995995
*/
996996
public function setClientReports(array $clientReports): self
997997
{
@@ -1001,7 +1001,7 @@ public function setClientReports(array $clientReports): self
10011001
}
10021002

10031003
/**
1004-
* @return ClientReport[]
1004+
* @return DiscardedEvent[]
10051005
*/
10061006
public function getClientReports(): array
10071007
{

src/EventType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ public function requiresEventId(): bool
8686
}
8787
}
8888

89+
/**
90+
* Returns false if rate limiting should not be applied.
91+
*
92+
* @return bool
93+
*/
94+
public function requiresRateLimiting(): bool
95+
{
96+
return $this !== self::clientReport();
97+
}
98+
8999
public function __toString(): string
90100
{
91101
return $this->value;

src/Serializer/EnvelopItems/ClientReportItem.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
namespace Sentry\Serializer\EnvelopItems;
66

7-
use Sentry\ClientReport\ClientReport;
7+
use Sentry\ClientReport\DiscardedEvent;
88
use Sentry\Event;
9+
use Sentry\Util\JSON;
910

1011
class ClientReportItem implements EnvelopeItemInterface
1112
{
@@ -16,7 +17,7 @@ public static function toEnvelopeItem(Event $event): ?string
1617
$headers = ['type' => 'client_report'];
1718
$body = [
1819
'timestamp' => $event->getTimestamp(),
19-
'discarded_events' => array_map(static function (ClientReport $report) {
20+
'discarded_events' => array_map(static function (DiscardedEvent $report) {
2021
return [
2122
'category' => $report->getCategory(),
2223
'reason' => $report->getReason(),
@@ -25,6 +26,6 @@ public static function toEnvelopeItem(Event $event): ?string
2526
}, $reports),
2627
];
2728

28-
return \sprintf("%s\n%s", json_encode($headers), json_encode($body));
29+
return \sprintf("%s\n%s", JSON::encode($headers), JSON::encode($body));
2930
}
3031
}

src/Transport/HttpTransport.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,28 @@ public function send(Event $event): Result
9191
$this->logger->info(\sprintf('Sending %s to %s.', $eventDescription, $targetDescription), ['event' => $event]);
9292

9393
$eventType = $event->getType();
94-
if ($this->rateLimiter->isRateLimited((string) $eventType)) {
95-
$this->logger->warning(
96-
\sprintf('Rate limit exceeded for sending requests of type "%s".', (string) $eventType),
97-
['event' => $event]
98-
);
99-
100-
return new Result(ResultStatus::rateLimit());
101-
}
102-
103-
// Since profiles are attached to transaction we have to check separately if they are rate limited.
104-
// We can do this after transactions have been checked because if transactions are rate limited,
105-
// so are profiles but not the other way around.
106-
if ($event->getSdkMetadata('profile') !== null) {
107-
if ($this->rateLimiter->isRateLimited(RateLimiter::DATA_CATEGORY_PROFILE)) {
108-
// Just remove profiling data so the normal transaction can be sent.
109-
$event->setSdkMetadata('profile', null);
94+
if ($eventType->requiresRateLimiting()) {
95+
if ($this->rateLimiter->isRateLimited((string)$eventType)) {
11096
$this->logger->warning(
111-
'Rate limit exceeded for sending requests of type "profile". The profile has been dropped.',
97+
\sprintf('Rate limit exceeded for sending requests of type "%s".', (string)$eventType),
11298
['event' => $event]
11399
);
100+
101+
return new Result(ResultStatus::rateLimit());
102+
}
103+
104+
// Since profiles are attached to transaction we have to check separately if they are rate limited.
105+
// We can do this after transactions have been checked because if transactions are rate limited,
106+
// so are profiles but not the other way around.
107+
if ($event->getSdkMetadata('profile') !== null) {
108+
if ($this->rateLimiter->isRateLimited(RateLimiter::DATA_CATEGORY_PROFILE)) {
109+
// Just remove profiling data so the normal transaction can be sent.
110+
$event->setSdkMetadata('profile', null);
111+
$this->logger->warning(
112+
'Rate limit exceeded for sending requests of type "profile". The profile has been dropped.',
113+
['event' => $event]
114+
);
115+
}
114116
}
115117
}
116118

tests/Serializer/PayloadSerializerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Sentry\CheckIn;
1010
use Sentry\CheckInStatus;
1111
use Sentry\Client;
12-
use Sentry\ClientReport\ClientReport;
12+
use Sentry\ClientReport\DiscardedEvent;
1313
use Sentry\Context\OsContext;
1414
use Sentry\Context\RuntimeContext;
1515
use Sentry\Event;
@@ -488,8 +488,8 @@ public static function serializeAsEnvelopeDataProvider(): iterable
488488

489489
$event = Event::createClientReport();
490490
$event->setClientReports([
491-
new ClientReport('log_item', 'buffer_overflow', 1),
492-
new ClientReport('log_byte', 'buffer_overflow', 256),
491+
new DiscardedEvent('log_item', 'buffer_overflow', 1),
492+
new DiscardedEvent('log_byte', 'buffer_overflow', 256),
493493
]);
494494

495495
yield [
@@ -504,8 +504,8 @@ public static function serializeAsEnvelopeDataProvider(): iterable
504504

505505
$event = Event::createClientReport();
506506
$event->setClientReports([
507-
new ClientReport('error', 'before_send', 10),
508-
new ClientReport('profile', 'internal_sdk_error', 50),
507+
new DiscardedEvent('error', 'before_send', 10),
508+
new DiscardedEvent('profile', 'internal_sdk_error', 50),
509509
]);
510510

511511
yield [

0 commit comments

Comments
 (0)