Skip to content

Commit 52283d4

Browse files
committed
feat(scope): update aggregator flush to use a client
1 parent 3a5d496 commit 52283d4

5 files changed

Lines changed: 94 additions & 17 deletions

File tree

src/Logs/LogsAggregator.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Sentry\Attributes\Attribute;
88
use Sentry\Client;
9+
use Sentry\ClientInterface;
910
use Sentry\Event;
1011
use Sentry\EventId;
1112
use Sentry\SentrySdk;
@@ -159,20 +160,22 @@ public function add(
159160
$logs->push($log);
160161

161162
if ($logFlushThreshold !== null && \count($logs) >= $logFlushThreshold) {
162-
$this->flush($hub);
163+
$this->flush($client);
163164
}
164165
}
165166

166-
public function flush(?HubInterface $hub = null): ?EventId
167+
public function flush(?ClientInterface $client = null): ?EventId
167168
{
168-
if ($this->logs === null || $this->logs->isEmpty()) {
169+
$logs = $this->logs;
170+
171+
if ($logs === null || $logs->isEmpty()) {
169172
return null;
170173
}
171174

172-
$hub = $hub ?? SentrySdk::getCurrentHub();
173-
$event = Event::createLogs()->setLogs($this->logs->drain());
175+
$client = $client ?? SentrySdk::getCurrentHub()->getClient();
176+
$event = Event::createLogs()->setLogs($logs->drain());
174177

175-
return $hub->captureEvent($event);
178+
return $client->captureEvent($event);
176179
}
177180

178181
/**

src/Metrics/MetricsAggregator.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Sentry\Metrics;
66

77
use Sentry\Client;
8+
use Sentry\ClientInterface;
89
use Sentry\Event;
910
use Sentry\EventId;
1011
use Sentry\Metrics\Types\CounterMetric;
@@ -124,20 +125,22 @@ public function add(
124125
$metrics->push($metric);
125126

126127
if ($metricFlushThreshold !== null && \count($metrics) >= $metricFlushThreshold) {
127-
$this->flush($hub);
128+
$this->flush($client);
128129
}
129130
}
130131

131-
public function flush(?HubInterface $hub = null): ?EventId
132+
public function flush(?ClientInterface $client = null): ?EventId
132133
{
133-
if ($this->metrics === null || $this->metrics->isEmpty()) {
134+
$metrics = $this->metrics;
135+
136+
if ($metrics === null || $metrics->isEmpty()) {
134137
return null;
135138
}
136139

137-
$hub = $hub ?? SentrySdk::getCurrentHub();
138-
$event = Event::createMetrics()->setMetrics($this->metrics->drain());
140+
$client = $client ?? SentrySdk::getCurrentHub()->getClient();
141+
$event = Event::createMetrics()->setMetrics($metrics->drain());
139142

140-
return $hub->captureEvent($event);
143+
return $client->captureEvent($event);
141144
}
142145

143146
/**

src/State/RuntimeContextManager.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ private function removeContextById(string $runtimeContextId, ?int $timeout = nul
161161

162162
private function flushRuntimeContextResources(RuntimeContext $runtimeContext, ?int $timeout, LoggerInterface $logger): void
163163
{
164-
$hub = $runtimeContext->getHub();
164+
$client = $runtimeContext->getHub()->getClient();
165165

166166
// captureEvent can throw before transport send (for example from scope event processors
167167
// or before_send callbacks), so we isolate failures and continue flushing other resources.
168168
try {
169-
$runtimeContext->getLogsAggregator()->flush($hub);
169+
$runtimeContext->getLogsAggregator()->flush($client);
170170
} catch (\Throwable $exception) {
171171
$logger->error('Failed to flush logs while ending a runtime context.', [
172172
'exception' => $exception,
@@ -176,16 +176,14 @@ private function flushRuntimeContextResources(RuntimeContext $runtimeContext, ?i
176176

177177
// Keep metrics flush independent from logs flush so one bad callback does not block the rest.
178178
try {
179-
$runtimeContext->getMetricsAggregator()->flush($hub);
179+
$runtimeContext->getMetricsAggregator()->flush($client);
180180
} catch (\Throwable $exception) {
181181
$logger->error('Failed to flush trace metrics while ending a runtime context.', [
182182
'exception' => $exception,
183183
'runtime_context_id' => $runtimeContext->getId(),
184184
]);
185185
}
186186

187-
$client = $hub->getClient();
188-
189187
// Custom transports may throw from close(); endContext must stay best-effort and non-fatal.
190188
try {
191189
$client->flush($timeout);

tests/Logs/LogsAggregatorTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
use PHPUnit\Framework\TestCase;
88
use Sentry\Client;
99
use Sentry\ClientBuilder;
10+
use Sentry\ClientInterface;
11+
use Sentry\Event;
1012
use Sentry\Logs\LogLevel;
1113
use Sentry\Logs\LogsAggregator;
14+
use Sentry\Options;
1215
use Sentry\SentrySdk;
1316
use Sentry\State\Hub;
1417
use Sentry\State\Scope;
@@ -271,6 +274,39 @@ public function testFlushesImmediatelyWhenThresholdIsReached(): void
271274
$this->assertSame('Second message', StubTransport::$events[0]->getLogs()[1]->getBody());
272275
}
273276

277+
public function testFlushCapturesLogsWithProvidedClient(): void
278+
{
279+
$client = $this->createMock(ClientInterface::class);
280+
$client->method('getOptions')
281+
->willReturn(new Options([
282+
'enable_logs' => true,
283+
]));
284+
285+
$fallbackClient = $this->createMock(ClientInterface::class);
286+
$fallbackClient->method('getOptions')
287+
->willReturn(new Options([
288+
'enable_logs' => true,
289+
]));
290+
$fallbackClient->expects($this->never())
291+
->method('captureEvent');
292+
SentrySdk::setCurrentHub(new Hub($fallbackClient));
293+
294+
$aggregator = new LogsAggregator();
295+
$aggregator->add(LogLevel::info(), 'Test message');
296+
297+
$client->expects($this->once())
298+
->method('captureEvent')
299+
->with(
300+
$this->callback(function (Event $event): bool {
301+
$this->assertCount(1, $event->getLogs());
302+
303+
return true;
304+
})
305+
);
306+
307+
$aggregator->flush($client);
308+
}
309+
274310
public function testDoesNotFlushImmediatelyWhenThresholdIsNull(): void
275311
{
276312
StubTransport::$events = [];

tests/Metrics/TraceMetricsTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Sentry\Client;
9+
use Sentry\ClientInterface;
10+
use Sentry\Event;
911
use Sentry\Metrics\MetricsAggregator;
1012
use Sentry\Metrics\Types\CounterMetric;
1113
use Sentry\Metrics\Types\DistributionMetric;
1214
use Sentry\Metrics\Types\GaugeMetric;
1315
use Sentry\Metrics\Types\Metric;
1416
use Sentry\Options;
17+
use Sentry\SentrySdk;
18+
use Sentry\State\Hub;
1519
use Sentry\State\HubAdapter;
1620
use Sentry\State\Scope;
1721

@@ -110,6 +114,39 @@ public function testDoesNotFlushImmediatelyWhenMetricFlushThresholdIsNull(): voi
110114
$this->assertCount(2, StubTransport::$events[0]->getMetrics());
111115
}
112116

117+
public function testFlushCapturesMetricsWithProvidedClient(): void
118+
{
119+
$client = $this->createMock(ClientInterface::class);
120+
$client->method('getOptions')
121+
->willReturn(new Options([
122+
'enable_metrics' => true,
123+
]));
124+
125+
$fallbackClient = $this->createMock(ClientInterface::class);
126+
$fallbackClient->method('getOptions')
127+
->willReturn(new Options([
128+
'enable_metrics' => true,
129+
]));
130+
$fallbackClient->expects($this->never())
131+
->method('captureEvent');
132+
SentrySdk::setCurrentHub(new Hub($fallbackClient));
133+
134+
$aggregator = new MetricsAggregator();
135+
$aggregator->add(CounterMetric::TYPE, 'test-count', 2, ['foo' => 'bar'], null);
136+
137+
$client->expects($this->once())
138+
->method('captureEvent')
139+
->with(
140+
$this->callback(function (Event $event): bool {
141+
$this->assertCount(1, $event->getMetrics());
142+
143+
return true;
144+
})
145+
);
146+
147+
$aggregator->flush($client);
148+
}
149+
113150
public function testMetricsBufferFullWhenMetricFlushThresholdIsNull(): void
114151
{
115152
HubAdapter::getInstance()->bindClient(new Client(new Options([

0 commit comments

Comments
 (0)