-
-
Notifications
You must be signed in to change notification settings - Fork 27
feat: Support Trace Metrics #1032
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
23f2a15
feat: add additional Sentry singleton classes to aspect configuration
huangdijia 83dfd96
fix: reorder Sentry singleton classes in SingletonAspect
huangdijia f18ffc7
fix: update SingletonAspect to use TraceMetrics singleton instance
huangdijia 14c9808
feat: add enable_metrics configuration option to Sentry settings
huangdijia 6fd4f57
feat: add metrics tracking and related listeners for enhanced perform…
huangdijia 255a74f
feat: add new metrics listeners for enhanced monitoring capabilities
huangdijia 94cd380
fix: remove PoolWatcher listener from ConfigProvider
huangdijia 52a049f
feat: update event parameter types in metrics listeners for improved …
huangdijia c263c76
fix: optimize queue retrieval in QueueWatcher listener
huangdijia 7d273b6
feat: add metrics interval configuration and update timer intervals i…
huangdijia 082920d
feat: add metrics interval configuration to Sentry settings
huangdijia 65a56ef
feat: remove unnecessary comment and improve PoolWatcher with metric …
huangdijia a8cff6f
fix: add return type declaration to watch method in PoolWatcher class
huangdijia 60f7e90
fix: add missing newline for better code readability in PoolWatcher c…
huangdijia e4e6d1e
feat: enforce minimum metrics interval of 5 in getMetricsInterval method
huangdijia cdf795b
refactor: simplify process method in RequestWatcher class by removing…
huangdijia bb3a2df
fix: add missing newline for improved readability in RedisPoolWatcher…
huangdijia 87c9180
feat: add Counter and CounterAspect classes for metrics annotation an…
huangdijia 95e01d8
feat: add CounterAspect to ConfigProvider for metrics tracking
huangdijia 70ff61e
feat: add HistogramAspect and Histogram annotation for enhanced metri…
huangdijia 0e3eeb4
feat: add Unit::second() to HistogramAspect for improved metrics trac…
huangdijia 58d6468
feat: enhance RequestWatcher to track HTTP request metrics with Trace…
huangdijia 9e62103
feat: add defer calls to flush TraceMetrics in various aspects and li…
huangdijia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?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\Annotation; | ||
|
|
||
| use Attribute; | ||
| use Hyperf\Di\Annotation\AbstractAnnotation; | ||
|
|
||
| #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] | ||
| class Counter extends AbstractAnnotation | ||
| { | ||
| public function __construct(public string $name = '') | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?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\Annotation; | ||
|
|
||
| use Attribute; | ||
| use Hyperf\Di\Annotation\AbstractAnnotation; | ||
|
|
||
| #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] | ||
| class Histogram extends AbstractAnnotation | ||
| { | ||
| public function __construct(public string $name = '') | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?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\Aspect; | ||
|
|
||
| use FriendsOfHyperf\Sentry\Feature; | ||
| use FriendsOfHyperf\Sentry\Metrics\Annotation\Counter; | ||
| use Hyperf\Di\Aop\AbstractAspect; | ||
| use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
| use Sentry\Metrics\TraceMetrics; | ||
|
|
||
| use function Hyperf\Coroutine\defer; | ||
|
|
||
| class CounterAspect extends AbstractAspect | ||
| { | ||
| public array $classes = []; | ||
|
|
||
| public array $annotations = [ | ||
| Counter::class, | ||
| ]; | ||
|
|
||
| public function __construct(protected Feature $feature) | ||
| { | ||
| } | ||
|
|
||
| public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed | ||
| { | ||
| if ($this->feature->isMetricsEnabled()) { | ||
| $metadata = $proceedingJoinPoint->getAnnotationMetadata(); | ||
| $source = $this->fromCamelCase($proceedingJoinPoint->className . '::' . $proceedingJoinPoint->methodName); | ||
|
|
||
| /** @var null|Counter $annotation */ | ||
| $annotation = $metadata->method[Counter::class] ?? null; | ||
|
|
||
| if ($annotation) { | ||
| $name = $annotation->name ?: $source; | ||
| } else { | ||
| $name = $source; | ||
| } | ||
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| defer(fn () => TraceMetrics::getInstance()->flush()); | ||
|
|
||
| TraceMetrics::getInstance() | ||
| ->count($name, 1, [ | ||
| 'class' => $proceedingJoinPoint->className, | ||
| 'method' => $proceedingJoinPoint->methodName, | ||
| ]); | ||
huangdijia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return $proceedingJoinPoint->process(); | ||
| } | ||
|
|
||
| private function fromCamelCase(string $input): string | ||
| { | ||
| preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); | ||
| $ret = $matches[0]; | ||
| foreach ($ret as &$match) { | ||
| $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); | ||
| } | ||
| return implode('_', $ret); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?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\Aspect; | ||
|
|
||
| use FriendsOfHyperf\Sentry\Feature; | ||
| use FriendsOfHyperf\Sentry\Metrics\Annotation\Histogram; | ||
| use Hyperf\Di\Aop\AbstractAspect; | ||
| use Hyperf\Di\Aop\ProceedingJoinPoint; | ||
| use Sentry\Metrics\TraceMetrics; | ||
| use Sentry\Unit; | ||
|
|
||
| use function Hyperf\Coroutine\defer; | ||
| use function Hyperf\Tappable\tap; | ||
|
|
||
| class HistogramAspect extends AbstractAspect | ||
| { | ||
| public array $classes = []; | ||
|
|
||
| public array $annotations = [ | ||
| Histogram::class, | ||
| ]; | ||
|
|
||
| public function __construct(protected Feature $feature) | ||
| { | ||
| } | ||
|
|
||
| public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed | ||
| { | ||
| if (! $this->feature->isMetricsEnabled()) { | ||
| return $proceedingJoinPoint->process(); | ||
| } | ||
|
|
||
| $metadata = $proceedingJoinPoint->getAnnotationMetadata(); | ||
| $source = $this->fromCamelCase($proceedingJoinPoint->className . '::' . $proceedingJoinPoint->methodName); | ||
| /** @var null|Histogram $annotation */ | ||
| $annotation = $metadata->method[Histogram::class] ?? null; | ||
| if ($annotation) { | ||
| $name = $annotation->name ?: $source; | ||
| } else { | ||
| $name = $source; | ||
| } | ||
|
|
||
| $startAt = microtime(true); | ||
|
|
||
| return tap($proceedingJoinPoint->process(), function () use ($name, $proceedingJoinPoint, $startAt) { | ||
| defer(fn () => TraceMetrics::getInstance()->flush()); | ||
|
|
||
| TraceMetrics::getInstance()->distribution( | ||
| $name, | ||
| (microtime(true) - $startAt) * 1000, | ||
| [ | ||
| 'class' => $proceedingJoinPoint->className, | ||
| 'method' => $proceedingJoinPoint->methodName, | ||
| ], | ||
| Unit::second() | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| private function fromCamelCase(string $input): string | ||
| { | ||
| preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); | ||
| $ret = $matches[0]; | ||
| foreach ($ret as &$match) { | ||
| $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); | ||
| } | ||
| return implode('_', $ret); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <?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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Hyperf\Contract\Arrayable; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $start_time 服务器启动的时间 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $connection_num 当前连接的数量 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $accept_count 接受了多少个连接 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $close_count 关闭的连接数量 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $worker_num 开启了多少个 worker 进程 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $idle_worker_num 空闲的 worker 进程数 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $task_worker_num 开启了多少个 task_worker 进程【v4.5.7 可用】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $tasking_num 当前正在排队的任务数 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $request_count Server 收到的请求次数【只有 onReceive、onMessage、onRequset、onPacket 四种数据请求计算 request_count】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $response_count Server 发送的响应次数【只有 onReceive、onMessage、onRequset、onPacket 四种数据请求计算 response_count】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $dispatch_count Server 发送到 Worker 的包数量【v4.5.7 可用,仅在 SWOOLE_PROCESS 模式下有效】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $worker_request_count 当前 Worker 进程收到的请求次数【worker_request_count 超过 max_request 时工作进程将退出】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $worker_dispatch_count master 进程向当前 Worker 进程投递任务的计数,在 master 进程进行 dispatch 时增加计数 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $task_queue_num 消息队列中的 task 数量【用于 Task】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $task_queue_bytes 消息队列的内存占用字节数【用于 Task】 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $task_idle_worker_num 空闲的 task 进程数量 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $coroutine_num 当前协程数量【用于 Coroutine】,想获取更多信息参考此节 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+19
to
+33
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @property int $accept_count 接受了多少个连接 | |
| * @property int $close_count 关闭的连接数量 | |
| * @property int $worker_num 开启了多少个 worker 进程 | |
| * @property int $idle_worker_num 空闲的 worker 进程数 | |
| * @property int $task_worker_num 开启了多少个 task_worker 进程【v4.5.7 可用】 | |
| * @property int $tasking_num 当前正在排队的任务数 | |
| * @property int $request_count Server 收到的请求次数【只有 onReceive、onMessage、onRequset、onPacket 四种数据请求计算 request_count】 | |
| * @property int $response_count Server 发送的响应次数【只有 onReceive、onMessage、onRequset、onPacket 四种数据请求计算 response_count】 | |
| * @property int $dispatch_count Server 发送到 Worker 的包数量【v4.5.7 可用,仅在 SWOOLE_PROCESS 模式下有效】 | |
| * @property int $worker_request_count 当前 Worker 进程收到的请求次数【worker_request_count 超过 max_request 时工作进程将退出】 | |
| * @property int $worker_dispatch_count master 进程向当前 Worker 进程投递任务的计数,在 master 进程进行 dispatch 时增加计数 | |
| * @property int $task_queue_num 消息队列中的 task 数量【用于 Task】 | |
| * @property int $task_queue_bytes 消息队列的内存占用字节数【用于 Task】 | |
| * @property int $task_idle_worker_num 空闲的 task 进程数量 | |
| * @property int $coroutine_num 当前协程数量【用于 Coroutine】,想获取更多信息参考此节 | |
| * @property int $accept_count 已接受的连接数量 | |
| * @property int $close_count 关闭的连接数量 | |
| * @property int $worker_num 开启了多少个 worker 进程 | |
| * @property int $idle_worker_num 空闲的 worker 进程数 | |
| * @property int $task_worker_num 开启了多少个 task_worker 进程【v4.5.7 可用】 | |
| * @property int $tasking_num 当前正在排队的任务数 | |
| * @property int $request_count Server 收到的请求次数。【只有 onReceive、onMessage、onRequset、onPacket 四种数据请求计算 request_count】 | |
| * @property int $response_count Server 发送的响应次数。【只有 onReceive、onMessage、onRequset、onPacket 四种数据请求计算 response_count】 | |
| * @property int $dispatch_count Server 发送到 Worker 的包数量【v4.5.7 可用,仅在 SWOOLE_PROCESS 模式下有效】 | |
| * @property int $worker_request_count 当前 Worker 进程收到的请求次数【worker_request_count 超过 max_request 时工作进程将退出】 | |
| * @property int $worker_dispatch_count master 进程向当前 Worker 进程投递任务的计数,在 master 进程进行 dispatch 时增加计数 | |
| * @property int $task_queue_num 消息队列中的 task 数量【用于 Task】 | |
| * @property int $task_queue_bytes 消息队列的内存占用字节数【用于 Task】 | |
| * @property int $task_idle_worker_num 空闲的 task 进程数量 | |
| * @property int $coroutine_num 当前协程数量【用于 Coroutine】。如需获取更多信息,请参考相关章节。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?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\Event; | ||
|
|
||
| class MetricFactoryReady | ||
| { | ||
| public function __construct(public int $workerId = 0) | ||
| { | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.