Skip to content

Commit 7fb2775

Browse files
authored
feat: enhance Sentry metrics with configurable default metrics collection (#1034)
* feat: enhance Sentry metrics with configurable default metrics collection - Add SENTRY_ENABLE_DEFAULT_METRICS configuration option - Implement isDefaultMetricsEnabled() method in Feature class - Extend OnBeforeHandle listener to collect command-line metrics - Add worker identification tags to memory metrics - Separate default metrics from custom metrics collection - Add Timer-based periodic metrics collection for commands - Include garbage collection and resource usage metrics This enhancement provides more granular control over metrics collection and extends monitoring capabilities to command-line operations. * feat: 添加命令指标启用选项并更新相关逻辑 * feat: 使用静态变量替代上下文管理命令运行状态 * feat: 移除AfterExecute事件监听并优化命令指标处理逻辑 * fix: 更新注释以更清晰地说明命令自动退出的条件 * fix: 修复命令指标处理逻辑,确保在适当条件下启用指标收集 * fix: 优化命令指标启用逻辑,确保在适当条件下收集指标 * fix: 修复命令指标启用逻辑,确保在适当条件下收集默认指标 * fix: 更新指标收集逻辑,将工人标识从'N/A'更改为'0' * fix: 修复默认指标启用逻辑,确保在禁用时正确返回 --------- Co-authored-by: Deeka Wong <[email protected]>
1 parent a70e1b8 commit 7fb2775

File tree

9 files changed

+120
-16
lines changed

9 files changed

+120
-16
lines changed

publish/sentry.php

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

4343
// @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#enable_metrics
4444
'enable_metrics' => env('SENTRY_ENABLE_METRICS', true),
45+
'enable_default_metrics' => env('SENTRY_ENABLE_DEFAULT_METRICS', true),
46+
'enable_command_metrics' => env('SENTRY_ENABLE_COMMAND_METRICS', true),
4547
'metrics_interval' => (int) env('SENTRY_METRICS_INTERVAL', 10),
4648

4749
'logs_channel_level' => env('SENTRY_LOGS_CHANNEL_LEVEL', Sentry\Logs\LogLevel::debug()),

src/Constants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ class Constants
3939

4040
public const TRACEPARENT = 'traceparent';
4141

42-
public const RUN_IN_COMMAND = 'sentry.run_in_command';
42+
public static bool $runningInCommand = false;
4343
}

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
'http_chanel_size', // deprecated, will be removed in v3.2
3232
'http_concurrent_limit', // deprecated, will be removed in v3.2
3333
'logs_channel_level',
34+
'enable_default_metrics',
35+
'enable_command_metrics',
3436
'metrics_interval',
3537
'transport_channel_size',
3638
'transport_concurrent_limit',

src/Feature.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ public function isMetricsEnabled(bool $default = true): bool
3636
return (bool) $this->config->get('sentry.enable_metrics', $default);
3737
}
3838

39+
public function isDefaultMetricsEnabled(bool $default = true): bool
40+
{
41+
if (! $this->isMetricsEnabled()) {
42+
return false;
43+
}
44+
45+
return (bool) $this->config->get('sentry.enable_default_metrics', $default);
46+
}
47+
48+
public function isCommandMetricsEnabled(bool $default = true): bool
49+
{
50+
if (! $this->isMetricsEnabled()) {
51+
return false;
52+
}
53+
54+
return (bool) $this->config->get('sentry.enable_command_metrics', $default);
55+
}
56+
3957
public function getMetricsInterval(int $default = 10): int
4058
{
4159
$interval = (int) $this->config->get('sentry.metrics_interval', $default);

src/Metrics/Listener/OnBeforeHandle.php

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,104 @@
1212
namespace FriendsOfHyperf\Sentry\Metrics\Listener;
1313

1414
use FriendsOfHyperf\Sentry\Constants;
15-
use Hyperf\Command\Event\AfterExecute;
15+
use FriendsOfHyperf\Sentry\Feature;
16+
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
1617
use Hyperf\Command\Event\BeforeHandle;
17-
use Hyperf\Context\Context;
18+
use Hyperf\Coordinator\CoordinatorManager;
19+
use Hyperf\Coordinator\Timer;
20+
use Hyperf\Engine\Coroutine;
1821
use Hyperf\Event\Contract\ListenerInterface;
22+
use Sentry\Unit;
23+
24+
use function FriendsOfHyperf\Sentry\metrics;
1925

2026
class OnBeforeHandle implements ListenerInterface
2127
{
28+
use MetricSetter;
29+
30+
protected Timer $timer;
31+
32+
public function __construct(protected Feature $feature)
33+
{
34+
$this->timer = new Timer();
35+
}
36+
2237
public function listen(): array
2338
{
2439
return [
2540
BeforeHandle::class,
26-
AfterExecute::class,
2741
];
2842
}
2943

3044
/**
31-
* @param object|BeforeHandle|AfterExecute $event
45+
* @param object|BeforeHandle $event
3246
*/
3347
public function process(object $event): void
3448
{
35-
match (true) {
36-
$event instanceof BeforeHandle => Context::set(Constants::RUN_IN_COMMAND, true),
37-
$event instanceof AfterExecute => Context::destroy(Constants::RUN_IN_COMMAND),
38-
default => null,
39-
};
49+
if (
50+
! $event instanceof BeforeHandle
51+
|| ! $event->getCommand()->getApplication()->isAutoExitEnabled() // Only enable in the command with auto exit.
52+
|| ! $this->feature->isCommandMetricsEnabled()
53+
) {
54+
return;
55+
}
56+
57+
Constants::$runningInCommand = true;
58+
59+
if (! $this->feature->isDefaultMetricsEnabled()) {
60+
return;
61+
}
62+
63+
// The following metrics MUST be collected in worker.
64+
$metrics = [
65+
'memory_usage',
66+
'memory_peak_usage',
67+
'gc_runs',
68+
'gc_collected',
69+
'gc_threshold',
70+
'gc_roots',
71+
'ru_oublock',
72+
'ru_inblock',
73+
'ru_msgsnd',
74+
'ru_msgrcv',
75+
'ru_maxrss',
76+
'ru_ixrss',
77+
'ru_idrss',
78+
'ru_minflt',
79+
'ru_majflt',
80+
'ru_nsignals',
81+
'ru_nvcsw',
82+
'ru_nivcsw',
83+
'ru_nswap',
84+
'ru_utime_tv_usec',
85+
'ru_utime_tv_sec',
86+
'ru_stime_tv_usec',
87+
'ru_stime_tv_sec',
88+
];
89+
90+
$timerId = $this->timer->tick($this->feature->getMetricsInterval(), function () use ($metrics) {
91+
defer(fn () => metrics()->flush());
92+
93+
$this->trySet('gc_', $metrics, gc_status());
94+
$this->trySet('', $metrics, getrusage());
95+
96+
metrics()->gauge(
97+
'memory_usage',
98+
(float) memory_get_usage(),
99+
['worker' => '0'],
100+
Unit::byte()
101+
);
102+
metrics()->gauge(
103+
'memory_peak_usage',
104+
(float) memory_get_peak_usage(),
105+
['worker' => '0'],
106+
Unit::byte()
107+
);
108+
});
109+
110+
Coroutine::create(function () use ($timerId) {
111+
CoordinatorManager::until(\Hyperf\Coordinator\Constants::WORKER_EXIT)->yield();
112+
$this->timer->clear($timerId);
113+
});
40114
}
41115
}

src/Metrics/Listener/OnCoroutineServerStart.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function process(object $event): void
6767
$eventDispatcher = $this->container->get(EventDispatcherInterface::class);
6868
$eventDispatcher->dispatch(new MetricFactoryReady());
6969

70+
if (! $this->feature->isDefaultMetricsEnabled()) {
71+
return;
72+
}
73+
7074
// The following metrics MUST be collected in worker.
7175
$metrics = [
7276
// 'worker_request_count',
@@ -105,13 +109,13 @@ public function process(object $event): void
105109
metrics()->gauge(
106110
'memory_usage',
107111
(float) memory_get_usage(),
108-
[],
112+
['worker' => '0'],
109113
Unit::byte()
110114
);
111115
metrics()->gauge(
112116
'memory_peak_usage',
113117
(float) memory_get_peak_usage(),
114-
[],
118+
['worker' => '0'],
115119
Unit::byte()
116120
);
117121
});

src/Metrics/Listener/OnMetricFactoryReady.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
namespace FriendsOfHyperf\Sentry\Metrics\Listener;
1313

14+
use FriendsOfHyperf\Sentry\Constants as SentryConstants;
1415
use FriendsOfHyperf\Sentry\Feature;
1516
use FriendsOfHyperf\Sentry\Metrics\CoroutineServerStats;
1617
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
1718
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
18-
use Hyperf\Context\Context;
1919
use Hyperf\Coordinator\Constants;
2020
use Hyperf\Coordinator\CoordinatorManager;
2121
use Hyperf\Coordinator\Timer;
@@ -54,7 +54,7 @@ public function listen(): array
5454
*/
5555
public function process(object $event): void
5656
{
57-
if (! $this->feature->isMetricsEnabled()) {
57+
if (! $this->feature->isDefaultMetricsEnabled()) {
5858
return;
5959
}
6060

@@ -86,7 +86,7 @@ public function process(object $event): void
8686

8787
$serverStatsFactory = null;
8888

89-
if (! Context::get(\FriendsOfHyperf\Sentry\Constants::RUN_IN_COMMAND, false)) {
89+
if (! SentryConstants::$runningInCommand) {
9090
if ($this->container->has(SwooleServer::class) && $server = $this->container->get(SwooleServer::class)) {
9191
if ($server instanceof SwooleServer) {
9292
$serverStatsFactory = fn (): array => $server->stats();

src/Metrics/Listener/OnWorkerStart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function listen(): array
5353
*/
5454
public function process(object $event): void
5555
{
56-
if (! $this->feature->isMetricsEnabled()) {
56+
if (! $this->feature->isDefaultMetricsEnabled()) {
5757
return;
5858
}
5959

src/Metrics/Traits/MetricSetter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ protected function trySet(string $prefix, array $metrics, array $stats, int $wor
3131
}
3232
}
3333
}
34+
35+
// protected function spawnDefaultMetrics()
36+
// {
37+
// }
3438
}

0 commit comments

Comments
 (0)