Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/sentry/publish/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@

'enable_default_metrics' => env('SENTRY_ENABLE_DEFAULT_METRICS', true),
'enable_command_metrics' => env('SENTRY_ENABLE_COMMAND_METRICS', true),
'enable_pool_metrics' => env('SENTRY_ENABLE_POOL_METRICS', true),
'enable_queue_metrics' => env('SENTRY_ENABLE_QUEUE_METRICS', true),
'metrics_interval' => (int) env('SENTRY_METRICS_INTERVAL', 10),

// @see: https://docs.sentry.io/platforms/php/guides/laravel/configuration/options/#send_default_pii
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/src/Factory/ClientBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ClientBuilderFactory
'logs_channel_level',
'enable_default_metrics',
'enable_command_metrics',
'enable_queue_metrics',
'enable_pool_metrics',
'metrics_interval',
'transport_channel_size',
'transport_concurrent_limit',
Expand Down
18 changes: 18 additions & 0 deletions src/sentry/src/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ public function isCommandMetricsEnabled(bool $default = true): bool
return (bool) $this->config->get('sentry.enable_command_metrics', $default);
}

public function isPoolMetricsEnabled(bool $default = true): bool
{
if (! $this->isMetricsEnabled()) {
return false;
}

return (bool) $this->config->get('sentry.enable_pool_metrics', $default);
}

public function isQueueMetricsEnabled(bool $default = true): bool
{
if (! $this->isMetricsEnabled()) {
return false;
}

return (bool) $this->config->get('sentry.enable_queue_metrics', $default);
}

public function getMetricsInterval(int $default = 10): int
{
$interval = (int) $this->config->get('sentry.metrics_interval', $default);
Expand Down
57 changes: 32 additions & 25 deletions src/sentry/src/Metrics/Listener/OnBeforeHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

use FriendsOfHyperf\Sentry\Constants;
use FriendsOfHyperf\Sentry\Feature;
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
use FriendsOfHyperf\Sentry\SentryContext;
use Hyperf\Command\Event\BeforeHandle;
use Hyperf\Coordinator\Timer;
use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Sentry\Unit;

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

protected Timer $timer;

public function __construct(protected Feature $feature)
{
public function __construct(
protected ContainerInterface $container,
protected Feature $feature
) {
$this->timer = new Timer();
}

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

Constants::$runningInCommand = true;

if ($this->feature->isCommandMetricsEnabled() && $this->container->has(EventDispatcherInterface::class)) {
$this->container->get(EventDispatcherInterface::class)->dispatch(new MetricFactoryReady());
}

if (! $this->feature->isDefaultMetricsEnabled()) {
return;
}
Expand Down Expand Up @@ -87,28 +95,27 @@ public function process(object $event): void
'ru_stime_tv_sec',
];

$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) {
if ($isClosing) {
return Timer::STOP;
$this->timer->tick(
$this->feature->getMetricsInterval(),
function () use ($metrics) {
$this->trySet('gc_', $metrics, gc_status());
$this->trySet('', $metrics, getrusage());

metrics()->gauge(
'memory_usage',
memory_get_usage(true) / 1024 / 1024,
['worker' => '0'],
Unit::megabyte()
);
metrics()->gauge(
'memory_peak_usage',
memory_get_peak_usage(true) / 1024 / 1024,
['worker' => '0'],
Unit::megabyte()
);

metrics()->flush();
}

$this->trySet('gc_', $metrics, gc_status());
$this->trySet('', $metrics, getrusage());

metrics()->gauge(
'memory_usage',
memory_get_usage(true) / 1024 / 1024,
['worker' => '0'],
Unit::megabyte()
);
metrics()->gauge(
'memory_peak_usage',
memory_get_peak_usage(true) / 1024 / 1024,
['worker' => '0'],
Unit::megabyte()
);

metrics()->flush();
});
);
}
}
52 changes: 24 additions & 28 deletions src/sentry/src/Metrics/Listener/OnCoroutineServerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,15 @@ public function listen(): array
*/
public function process(object $event): void
{
if (! $this->feature->isMetricsEnabled()) {
return;
}

if ($this->running) {
return;
}

$this->running = true;

$eventDispatcher = $this->container->get(EventDispatcherInterface::class);
$eventDispatcher->dispatch(new MetricFactoryReady());
if ($this->feature->isMetricsEnabled() && $this->container->has(EventDispatcherInterface::class)) {
$this->container->get(EventDispatcherInterface::class)->dispatch(new MetricFactoryReady());
}

if (! $this->feature->isDefaultMetricsEnabled()) {
return;
Expand Down Expand Up @@ -96,28 +93,27 @@ public function process(object $event): void
'ru_stime_tv_sec',
];

$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics) {
if ($isClosing) {
return Timer::STOP;
$this->timer->tick(
$this->feature->getMetricsInterval(),
function () use ($metrics) {
$this->trySet('gc_', $metrics, gc_status());
$this->trySet('', $metrics, getrusage());

metrics()->gauge(
'memory_usage',
memory_get_usage(true) / 1024 / 1024,
['worker' => '0'],
Unit::megabyte()
);
metrics()->gauge(
'memory_peak_usage',
memory_get_peak_usage(true) / 1024 / 1024,
['worker' => '0'],
Unit::megabyte()
);

metrics()->flush();
}

$this->trySet('gc_', $metrics, gc_status());
$this->trySet('', $metrics, getrusage());

metrics()->gauge(
'memory_usage',
memory_get_usage(true) / 1024 / 1024,
['worker' => '0'],
Unit::megabyte()
);
metrics()->gauge(
'memory_peak_usage',
memory_get_peak_usage(true) / 1024 / 1024,
['worker' => '0'],
Unit::megabyte()
);

metrics()->flush();
});
);
}
}
69 changes: 34 additions & 35 deletions src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use FriendsOfHyperf\Sentry\Metrics\Event\MetricFactoryReady;
use FriendsOfHyperf\Sentry\Metrics\Traits\MetricSetter;
use Hyperf\Coordinator\Timer;
use Hyperf\Engine\Coroutine;
use Hyperf\Engine\Coroutine as Co;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Support\System;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -95,43 +95,42 @@ public function process(object $event): void
}
}

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

$this->trySet('', $metrics, Coroutine::stats(), $workerId);
$this->trySet('timer_', $metrics, Timer::stats(), $workerId);
if ($serverStatsFactory) {
$this->trySet('', $metrics, $serverStatsFactory(), $workerId);
}

if ($serverStatsFactory) {
$this->trySet('', $metrics, $serverStatsFactory(), $workerId);
}
if (class_exists('Swoole\Timer')) {
$this->trySet('swoole_timer_', $metrics, \Swoole\Timer::stats(), $workerId);
}

if (class_exists('Swoole\Timer')) {
$this->trySet('swoole_timer_', $metrics, \Swoole\Timer::stats(), $workerId);
$load = sys_getloadavg();

metrics()->gauge(
'sys_load',
round($load[0] / System::getCpuCoresNum(), 2),
['worker' => (string) $workerId],
);
metrics()->gauge(
'metric_process_memory_usage',
memory_get_usage(true) / 1024 / 1024,
['worker' => (string) $workerId],
Unit::megabyte()
);
metrics()->gauge(
'metric_process_memory_peak_usage',
memory_get_peak_usage(true) / 1024 / 1024,
['worker' => (string) $workerId],
Unit::megabyte()
);

metrics()->flush();
}

$load = sys_getloadavg();

metrics()->gauge(
'sys_load',
round($load[0] / System::getCpuCoresNum(), 2),
['worker' => (string) $workerId],
);
metrics()->gauge(
'metric_process_memory_usage',
memory_get_usage(true) / 1024 / 1024,
['worker' => (string) $workerId],
Unit::megabyte()
);
metrics()->gauge(
'metric_process_memory_peak_usage',
memory_get_peak_usage(true) / 1024 / 1024,
['worker' => (string) $workerId],
Unit::megabyte()
);

metrics()->flush();
});
);
}
}
75 changes: 38 additions & 37 deletions src/sentry/src/Metrics/Listener/OnWorkerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ public function listen(): array
*/
public function process(object $event): void
{
if ($this->feature->isMetricsEnabled() && $event->workerId == 0) {
$workerId = $event->workerId;
$eventDispatcher = $this->container->get(EventDispatcherInterface::class);
$eventDispatcher->dispatch(new MetricFactoryReady($workerId));
}

if (! $this->feature->isDefaultMetricsEnabled()) {
return;
}

$workerId = $event->workerId;
$eventDispatcher = $this->container->get(EventDispatcherInterface::class);
$eventDispatcher->dispatch(new MetricFactoryReady($workerId));

// The following metrics MUST be collected in worker.
$metrics = [
'worker_request_count',
Expand Down Expand Up @@ -86,40 +88,39 @@ public function process(object $event): void
'ru_stime_tv_sec',
];

$this->timer->tick($this->feature->getMetricsInterval(), function ($isClosing = false) use ($metrics, $event) {
if ($isClosing) {
return Timer::STOP;
}

$server = $this->container->get(Server::class);
$serverStats = $server->stats();
$this->trySet('gc_', $metrics, gc_status());
$this->trySet('', $metrics, getrusage());
$this->timer->tick(
$this->feature->getMetricsInterval(),
function () use ($metrics, $event) {
$server = $this->container->get(Server::class);
$serverStats = $server->stats();
$this->trySet('gc_', $metrics, gc_status());
$this->trySet('', $metrics, getrusage());

metrics()->gauge(
'worker_request_count',
(float) $serverStats['worker_request_count'],
['worker' => (string) ($event->workerId ?? 0)],
);
metrics()->gauge(
'worker_dispatch_count',
(float) $serverStats['worker_dispatch_count'],
['worker' => (string) ($event->workerId ?? 0)],
);
metrics()->gauge(
'memory_usage',
memory_get_usage(true) / 1024 / 1024,
['worker' => (string) ($event->workerId ?? 0)],
Unit::megabyte()
);
metrics()->gauge(
'memory_peak_usage',
memory_get_peak_usage(true) / 1024 / 1024,
['worker' => (string) ($event->workerId ?? 0)],
Unit::megabyte()
);
metrics()->gauge(
'worker_request_count',
(float) $serverStats['worker_request_count'],
['worker' => (string) ($event->workerId ?? 0)],
);
metrics()->gauge(
'worker_dispatch_count',
(float) $serverStats['worker_dispatch_count'],
['worker' => (string) ($event->workerId ?? 0)],
);
metrics()->gauge(
'memory_usage',
memory_get_usage(true) / 1024 / 1024,
['worker' => (string) ($event->workerId ?? 0)],
Unit::megabyte()
);
metrics()->gauge(
'memory_peak_usage',
memory_get_peak_usage(true) / 1024 / 1024,
['worker' => (string) ($event->workerId ?? 0)],
Unit::megabyte()
);

metrics()->flush();
});
metrics()->flush();
}
);
}
}
Loading