-
-
Notifications
You must be signed in to change notification settings - Fork 28
fix(sentry): flush global telemetry buffers periodically #1087
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Sentry\Metrics\Listener; | ||
|
|
||
| use FriendsOfHyperf\Sentry\Feature; | ||
| use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady; | ||
| use Hyperf\Coordinator\Timer; | ||
| use Hyperf\Event\Contract\ListenerInterface; | ||
| use Psr\Container\ContainerInterface; | ||
| use Sentry\Logs\Logs; | ||
| use Sentry\Metrics\TraceMetrics; | ||
| use Sentry\SentrySdk; | ||
| use Throwable; | ||
|
|
||
| class TelemetryFlushListener implements ListenerInterface | ||
| { | ||
| private Timer $timer; | ||
|
|
||
| private bool $ticking = false; | ||
|
|
||
| public function __construct( | ||
| protected ContainerInterface $container, | ||
| protected Feature $feature, | ||
| ?Timer $timer = null, | ||
| ) { | ||
| $this->timer = $timer ?? new Timer(); | ||
| } | ||
|
|
||
| public function listen(): array | ||
| { | ||
| return [ | ||
| MetricFactoryReady::class, | ||
| ]; | ||
|
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With the published defaults ( Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| /** | ||
| * @param object|MetricFactoryReady $event | ||
| */ | ||
| public function process(object $event): void | ||
| { | ||
| if ($this->ticking) { | ||
| return; | ||
| } | ||
|
|
||
| if (! $this->feature->isMetricsEnabled() && ! $this->feature->isLogsEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| $this->ticking = true; | ||
|
|
||
| $this->timer->tick( | ||
| $this->feature->getMetricsInterval(), | ||
| function (): void { | ||
| // End this tick coroutine's own runtime context (if any), so the | ||
| // following flush operations target the global context aggregators. | ||
| SentrySdk::endContext(); | ||
|
|
||
| try { | ||
| Logs::getInstance()->flush(); | ||
| } catch (Throwable) { | ||
| } | ||
|
|
||
| try { | ||
| TraceMetrics::getInstance()->flush(); | ||
| } catch (Throwable) { | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Tests\Sentry\Metrics\Listener; | ||
|
|
||
| use FriendsOfHyperf\Sentry\Feature; | ||
| use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady; | ||
| use FriendsOfHyperf\Sentry\Metrics\Listener\TelemetryFlushListener; | ||
| use Hyperf\Coordinator\Constants; | ||
| use Hyperf\Coordinator\Timer; | ||
| use Mockery as m; | ||
| use Psr\Container\ContainerInterface; | ||
|
|
||
| class FakeTimer extends Timer | ||
| { | ||
| public int $tickCount = 0; | ||
|
|
||
| /** | ||
| * @var array<int, callable> | ||
| */ | ||
| public array $closures = []; | ||
|
|
||
| public function tick(float $timeout, callable $closure, string $identifier = Constants::WORKER_EXIT): int | ||
| { | ||
| ++$this->tickCount; | ||
| $this->closures[$this->tickCount] = $closure; | ||
|
|
||
| return $this->tickCount; | ||
| } | ||
| } | ||
|
|
||
| beforeEach(function () { | ||
| $this->feature = m::mock(Feature::class); | ||
| $this->feature->shouldReceive('getMetricsInterval')->andReturn(10); | ||
|
|
||
| $this->container = m::mock(ContainerInterface::class); | ||
| $this->timer = new FakeTimer(); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| m::close(); | ||
| }); | ||
|
|
||
| test('process schedules a single tick for repeated calls', function () { | ||
| $this->feature->shouldReceive('isMetricsEnabled')->andReturn(true); | ||
| $this->feature->shouldReceive('isLogsEnabled')->andReturn(true); | ||
|
|
||
| $listener = new TelemetryFlushListener($this->container, $this->feature, $this->timer); | ||
|
|
||
| $listener->process(new MetricFactoryReady()); | ||
| $listener->process(new MetricFactoryReady()); | ||
|
|
||
| expect($this->timer->tickCount)->toBe(1); | ||
| }); | ||
|
|
||
| test('saved tick closure can be invoked without throwing', function () { | ||
| $this->feature->shouldReceive('isMetricsEnabled')->andReturn(true); | ||
| $this->feature->shouldReceive('isLogsEnabled')->andReturn(true); | ||
|
|
||
| $listener = new TelemetryFlushListener($this->container, $this->feature, $this->timer); | ||
| $listener->process(new MetricFactoryReady()); | ||
|
|
||
| $closure = $this->timer->closures[1]; | ||
|
|
||
| $closure(false); | ||
|
|
||
| expect(true)->toBeTrue(); | ||
| }); | ||
|
|
||
| test('no tick is scheduled when logs and metrics are disabled', function () { | ||
| $this->feature->shouldReceive('isMetricsEnabled')->andReturn(false); | ||
| $this->feature->shouldReceive('isLogsEnabled')->andReturn(false); | ||
|
|
||
| $listener = new TelemetryFlushListener($this->container, $this->feature, $this->timer); | ||
|
|
||
| $listener->process(new MetricFactoryReady()); | ||
|
|
||
| expect($this->timer->tickCount)->toBe(0); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds user-facing behavior and threshold guidance to the published configuration, but both component READMEs and all four localized Sentry pages still omit
log_flush_threshold, the periodic fallback, and its memory recommendation. Update those six sources so users who follow the package documentation receive the same telemetry behavior and configuration guidance.AGENTS.md reference: AGENTS.md:L129-L130
Useful? React with 👍 / 👎.