Skip to content

Commit 203e978

Browse files
committed
feat(sentry): add separate configuration options for pool and queue metrics (#1053)
* feat(sentry): add separate configuration options for pool and queue metrics - Add enable_pool_metrics and enable_queue_metrics config options - Add isPoolMetricsEnabled() and isQueueMetricsEnabled() methods to Feature class - Update PoolWatcher and QueueWatcher to use their respective enable flags - Move MetricFactoryReady event dispatch earlier in OnCoroutineServerStart for proper initialization - Remove Timer::STOP callback logic from timer tick handlers (no longer needed) - Refactor timer tick callback formatting for consistency * fix(sentry): alias Coroutine class for consistency in metric processing * fix(metrics): remove unused parameter from timer callback functions * fix(metrics): check for EventDispatcherInterface existence before dispatching MetricFactoryReady event * fix(metrics): ensure command metrics are enabled before dispatching MetricFactoryReady event * feat(sentry): add enable_queue_metrics and enable_pool_metrics options to ClientBuilderFactory --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
1 parent d97c2f9 commit 203e978

File tree

9 files changed

+213
-194
lines changed

9 files changed

+213
-194
lines changed

src/sentry/publish/sentry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565

6666
'enable_default_metrics' => env('SENTRY_ENABLE_DEFAULT_METRICS', true),
6767
'enable_command_metrics' => env('SENTRY_ENABLE_COMMAND_METRICS', true),
68+
'enable_pool_metrics' => env('SENTRY_ENABLE_POOL_METRICS', true),
69+
'enable_queue_metrics' => env('SENTRY_ENABLE_QUEUE_METRICS', true),
6870
'metrics_interval' => (int) env('SENTRY_METRICS_INTERVAL', 10),
6971

7072
// @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#send_default_pii

src/sentry/src/Factory/ClientBuilderFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class ClientBuilderFactory
3131
'logs_channel_level',
3232
'enable_default_metrics',
3333
'enable_command_metrics',
34+
'enable_queue_metrics',
35+
'enable_pool_metrics',
3436
'metrics_interval',
3537
'transport_channel_size',
3638
'transport_concurrent_limit',

src/sentry/src/Feature.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ public function isCommandMetricsEnabled(bool $default = true): bool
5353
return (bool) $this->config->get('sentry.enable_command_metrics', $default);
5454
}
5555

56+
public function isPoolMetricsEnabled(bool $default = true): bool
57+
{
58+
if (! $this->isMetricsEnabled()) {
59+
return false;
60+
}
61+
62+
return (bool) $this->config->get('sentry.enable_pool_metrics', $default);
63+
}
64+
65+
public function isQueueMetricsEnabled(bool $default = true): bool
66+
{
67+
if (! $this->isMetricsEnabled()) {
68+
return false;
69+
}
70+
71+
return (bool) $this->config->get('sentry.enable_queue_metrics', $default);
72+
}
73+
5674
public function getMetricsInterval(int $default = 10): int
5775
{
5876
$interval = (int) $this->config->get('sentry.metrics_interval', $default);

src/sentry/src/Metrics/Listener/OnBeforeHandle.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
use FriendsOfHyperf\Sentry\Constants;
1515
use FriendsOfHyperf\Sentry\Feature;
16+
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
1617
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
1718
use FriendsOfHyperf\Sentry\SentryContext;
1819
use Hyperf\Command\Event\BeforeHandle;
1920
use Hyperf\Coordinator\Timer;
2021
use Hyperf\Event\Contract\ListenerInterface;
22+
use Psr\Container\ContainerInterface;
23+
use Psr\EventDispatcher\EventDispatcherInterface;
2124
use Sentry\Unit;
2225

2326
use function FriendsOfHyperf\Sentry\metrics;
@@ -28,8 +31,10 @@ class OnBeforeHandle implements ListenerInterface
2831

2932
protected Timer $timer;
3033

31-
public function __construct(protected Feature $feature)
32-
{
34+
public function __construct(
35+
protected ContainerInterface $container,
36+
protected Feature $feature
37+
) {
3338
$this->timer = new Timer();
3439
}
3540

@@ -49,13 +54,16 @@ public function process(object $event): void
4954
! $event instanceof BeforeHandle
5055
|| SentryContext::getCronCheckInId() // Prevent duplicate metrics in cron job.
5156
|| ! $event->getCommand()->getApplication()->isAutoExitEnabled() // Only enable in the command with auto exit.
52-
|| ! $this->feature->isCommandMetricsEnabled()
5357
) {
5458
return;
5559
}
5660

5761
Constants::$runningInCommand = true;
5862

63+
if ($this->feature->isCommandMetricsEnabled() && $this->container->has(EventDispatcherInterface::class)) {
64+
$this->container->get(EventDispatcherInterface::class)->dispatch(new MetricFactoryReady());
65+
}
66+
5967
if (! $this->feature->isDefaultMetricsEnabled()) {
6068
return;
6169
}
@@ -87,28 +95,27 @@ public function process(object $event): void
8795
'ru_stime_tv_sec',
8896
];
8997

90-
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) {
91-
if ($isClosing) {
92-
return Timer::STOP;
98+
$this->timer->tick(
99+
$this->feature->getMetricsInterval(),
100+
function () use ($metrics) {
101+
$this->trySet('gc_', $metrics, gc_status());
102+
$this->trySet('', $metrics, getrusage());
103+
104+
metrics()->gauge(
105+
'memory_usage',
106+
memory_get_usage(true) / 1024 / 1024,
107+
['worker' => '0'],
108+
Unit::megabyte()
109+
);
110+
metrics()->gauge(
111+
'memory_peak_usage',
112+
memory_get_peak_usage(true) / 1024 / 1024,
113+
['worker' => '0'],
114+
Unit::megabyte()
115+
);
116+
117+
metrics()->flush();
93118
}
94-
95-
$this->trySet('gc_', $metrics, gc_status());
96-
$this->trySet('', $metrics, getrusage());
97-
98-
metrics()->gauge(
99-
'memory_usage',
100-
memory_get_usage(true) / 1024 / 1024,
101-
['worker' => '0'],
102-
Unit::megabyte()
103-
);
104-
metrics()->gauge(
105-
'memory_peak_usage',
106-
memory_get_peak_usage(true) / 1024 / 1024,
107-
['worker' => '0'],
108-
Unit::megabyte()
109-
);
110-
111-
metrics()->flush();
112-
});
119+
);
113120
}
114121
}

src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,15 @@ public function listen(): array
5050
*/
5151
public function process(object $event): void
5252
{
53-
if (! $this->feature->isMetricsEnabled()) {
54-
return;
55-
}
56-
5753
if ($this->running) {
5854
return;
5955
}
6056

6157
$this->running = true;
6258

63-
$eventDispatcher = $this->container->get(EventDispatcherInterface::class);
64-
$eventDispatcher->dispatch(new MetricFactoryReady());
59+
if ($this->feature->isMetricsEnabled() && $this->container->has(EventDispatcherInterface::class)) {
60+
$this->container->get(EventDispatcherInterface::class)->dispatch(new MetricFactoryReady());
61+
}
6562

6663
if (! $this->feature->isDefaultMetricsEnabled()) {
6764
return;
@@ -96,28 +93,27 @@ public function process(object $event): void
9693
'ru_stime_tv_sec',
9794
];
9895

99-
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) {
100-
if ($isClosing) {
101-
return Timer::STOP;
96+
$this->timer->tick(
97+
$this->feature->getMetricsInterval(),
98+
function () use ($metrics) {
99+
$this->trySet('gc_', $metrics, gc_status());
100+
$this->trySet('', $metrics, getrusage());
101+
102+
metrics()->gauge(
103+
'memory_usage',
104+
memory_get_usage(true) / 1024 / 1024,
105+
['worker' => '0'],
106+
Unit::megabyte()
107+
);
108+
metrics()->gauge(
109+
'memory_peak_usage',
110+
memory_get_peak_usage(true) / 1024 / 1024,
111+
['worker' => '0'],
112+
Unit::megabyte()
113+
);
114+
115+
metrics()->flush();
102116
}
103-
104-
$this->trySet('gc_', $metrics, gc_status());
105-
$this->trySet('', $metrics, getrusage());
106-
107-
metrics()->gauge(
108-
'memory_usage',
109-
memory_get_usage(true) / 1024 / 1024,
110-
['worker' => '0'],
111-
Unit::megabyte()
112-
);
113-
metrics()->gauge(
114-
'memory_peak_usage',
115-
memory_get_peak_usage(true) / 1024 / 1024,
116-
['worker' => '0'],
117-
Unit::megabyte()
118-
);
119-
120-
metrics()->flush();
121-
});
117+
);
122118
}
123119
}

src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
1818
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
1919
use Hyperf\Coordinator\Timer;
20-
use Hyperf\Engine\Coroutine;
20+
use Hyperf\Engine\Coroutine as Co;
2121
use Hyperf\Event\Contract\ListenerInterface;
2222
use Hyperf\Support\System;
2323
use Psr\Container\ContainerInterface;
@@ -95,43 +95,42 @@ public function process(object $event): void
9595
}
9696
}
9797

98-
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $serverStatsFactory, $workerId) {
99-
if ($isClosing) {
100-
return Timer::STOP;
101-
}
98+
$this->timer->tick(
99+
$this->feature->getMetricsInterval(),
100+
function () use ($metrics, $serverStatsFactory, $workerId) {
101+
$this->trySet('', $metrics, Co::stats(), $workerId);
102+
$this->trySet('timer_', $metrics, Timer::stats(), $workerId);
102103

103-
$this->trySet('', $metrics, Coroutine::stats(), $workerId);
104-
$this->trySet('timer_', $metrics, Timer::stats(), $workerId);
104+
if ($serverStatsFactory) {
105+
$this->trySet('', $metrics, $serverStatsFactory(), $workerId);
106+
}
105107

106-
if ($serverStatsFactory) {
107-
$this->trySet('', $metrics, $serverStatsFactory(), $workerId);
108-
}
108+
if (class_exists('Swoole\Timer')) {
109+
$this->trySet('swoole_timer_', $metrics, \Swoole\Timer::stats(), $workerId);
110+
}
109111

110-
if (class_exists('Swoole\Timer')) {
111-
$this->trySet('swoole_timer_', $metrics, \Swoole\Timer::stats(), $workerId);
112+
$load = sys_getloadavg();
113+
114+
metrics()->gauge(
115+
'sys_load',
116+
round($load[0] / System::getCpuCoresNum(), 2),
117+
['worker' => (string) $workerId],
118+
);
119+
metrics()->gauge(
120+
'metric_process_memory_usage',
121+
memory_get_usage(true) / 1024 / 1024,
122+
['worker' => (string) $workerId],
123+
Unit::megabyte()
124+
);
125+
metrics()->gauge(
126+
'metric_process_memory_peak_usage',
127+
memory_get_peak_usage(true) / 1024 / 1024,
128+
['worker' => (string) $workerId],
129+
Unit::megabyte()
130+
);
131+
132+
metrics()->flush();
112133
}
113-
114-
$load = sys_getloadavg();
115-
116-
metrics()->gauge(
117-
'sys_load',
118-
round($load[0] / System::getCpuCoresNum(), 2),
119-
['worker' => (string) $workerId],
120-
);
121-
metrics()->gauge(
122-
'metric_process_memory_usage',
123-
memory_get_usage(true) / 1024 / 1024,
124-
['worker' => (string) $workerId],
125-
Unit::megabyte()
126-
);
127-
metrics()->gauge(
128-
'metric_process_memory_peak_usage',
129-
memory_get_peak_usage(true) / 1024 / 1024,
130-
['worker' => (string) $workerId],
131-
Unit::megabyte()
132-
);
133-
134-
metrics()->flush();
135-
});
134+
);
136135
}
137136
}

src/sentry/src/Metrics/Listener/OnWorkerStart.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,16 @@ public function listen(): array
4949
*/
5050
public function process(object $event): void
5151
{
52+
if ($this->feature->isMetricsEnabled() && $event->workerId == 0) {
53+
$workerId = $event->workerId;
54+
$eventDispatcher = $this->container->get(EventDispatcherInterface::class);
55+
$eventDispatcher->dispatch(new MetricFactoryReady($workerId));
56+
}
57+
5258
if (! $this->feature->isDefaultMetricsEnabled()) {
5359
return;
5460
}
5561

56-
$workerId = $event->workerId;
57-
$eventDispatcher = $this->container->get(EventDispatcherInterface::class);
58-
$eventDispatcher->dispatch(new MetricFactoryReady($workerId));
59-
6062
// The following metrics MUST be collected in worker.
6163
$metrics = [
6264
'worker_request_count',
@@ -86,40 +88,39 @@ public function process(object $event): void
8688
'ru_stime_tv_sec',
8789
];
8890

89-
$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $event) {
90-
if ($isClosing) {
91-
return Timer::STOP;
92-
}
93-
94-
$server = $this->container->get(Server::class);
95-
$serverStats = $server->stats();
96-
$this->trySet('gc_', $metrics, gc_status());
97-
$this->trySet('', $metrics, getrusage());
91+
$this->timer->tick(
92+
$this->feature->getMetricsInterval(),
93+
function () use ($metrics, $event) {
94+
$server = $this->container->get(Server::class);
95+
$serverStats = $server->stats();
96+
$this->trySet('gc_', $metrics, gc_status());
97+
$this->trySet('', $metrics, getrusage());
9898

99-
metrics()->gauge(
100-
'worker_request_count',
101-
(float) $serverStats['worker_request_count'],
102-
['worker' => (string) ($event->workerId ?? 0)],
103-
);
104-
metrics()->gauge(
105-
'worker_dispatch_count',
106-
(float) $serverStats['worker_dispatch_count'],
107-
['worker' => (string) ($event->workerId ?? 0)],
108-
);
109-
metrics()->gauge(
110-
'memory_usage',
111-
memory_get_usage(true) / 1024 / 1024,
112-
['worker' => (string) ($event->workerId ?? 0)],
113-
Unit::megabyte()
114-
);
115-
metrics()->gauge(
116-
'memory_peak_usage',
117-
memory_get_peak_usage(true) / 1024 / 1024,
118-
['worker' => (string) ($event->workerId ?? 0)],
119-
Unit::megabyte()
120-
);
99+
metrics()->gauge(
100+
'worker_request_count',
101+
(float) $serverStats['worker_request_count'],
102+
['worker' => (string) ($event->workerId ?? 0)],
103+
);
104+
metrics()->gauge(
105+
'worker_dispatch_count',
106+
(float) $serverStats['worker_dispatch_count'],
107+
['worker' => (string) ($event->workerId ?? 0)],
108+
);
109+
metrics()->gauge(
110+
'memory_usage',
111+
memory_get_usage(true) / 1024 / 1024,
112+
['worker' => (string) ($event->workerId ?? 0)],
113+
Unit::megabyte()
114+
);
115+
metrics()->gauge(
116+
'memory_peak_usage',
117+
memory_get_peak_usage(true) / 1024 / 1024,
118+
['worker' => (string) ($event->workerId ?? 0)],
119+
Unit::megabyte()
120+
);
121121

122-
metrics()->flush();
123-
});
122+
metrics()->flush();
123+
}
124+
);
124125
}
125126
}

0 commit comments

Comments
 (0)