-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Support buffer mechanism in standalone process of metric (#6030)
Co-authored-by: 李铭昕 <[email protected]>
- Loading branch information
1 parent
1de58b1
commit 5668bd5
Showing
11 changed files
with
335 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Metric\Adapter\RemoteProxy; | ||
|
||
use Hyperf\Metric\Contract\MetricCollectorInterface; | ||
use Hyperf\Process\ProcessCollector; | ||
|
||
class MetricCollector implements MetricCollectorInterface | ||
{ | ||
protected const TARGET_PROCESS_NAME = 'metric'; | ||
|
||
protected array $buffer = []; | ||
|
||
public function __construct( | ||
protected int $bufferSize = 200 | ||
) { | ||
} | ||
|
||
public function add(object $data): void | ||
{ | ||
$this->buffer[] = $data; | ||
|
||
if (count($this->buffer) >= $this->bufferSize) { | ||
$this->flush(); | ||
} | ||
} | ||
|
||
public function flush(): void | ||
{ | ||
$process = ProcessCollector::get(static::TARGET_PROCESS_NAME)[0]; | ||
$buffer = $this->buffer; | ||
$this->buffer = []; | ||
$process->write(serialize($buffer)); | ||
} | ||
|
||
public function getBuffer(): array | ||
{ | ||
return $this->buffer; | ||
} | ||
} |
This file contains 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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Metric\Adapter\RemoteProxy; | ||
|
||
use Hyperf\Contract\ConfigInterface; | ||
use Hyperf\Metric\Contract\MetricCollectorInterface; | ||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
class MetricCollectorFactory | ||
{ | ||
/** | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
*/ | ||
public function __invoke(ContainerInterface $container): MetricCollectorInterface | ||
{ | ||
$config = $container->get(ConfigInterface::class); | ||
|
||
return new MetricCollector( | ||
(int) $config->get('metric.buffer_size', 200) | ||
); | ||
} | ||
} |
This file contains 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 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 Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Metric\Contract; | ||
|
||
interface MetricCollectorInterface | ||
{ | ||
public function add(object $data): void; | ||
|
||
public function flush(): void; | ||
} |
This file contains 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,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Metric\Listener; | ||
|
||
use Hyperf\Contract\ConfigInterface; | ||
use Hyperf\Coordinator\Constants; | ||
use Hyperf\Coordinator\CoordinatorManager; | ||
use Hyperf\Coordinator\Timer; | ||
use Hyperf\Coroutine\Coroutine; | ||
use Hyperf\Event\Contract\ListenerInterface; | ||
use Hyperf\Framework\Event\BeforeWorkerStart; | ||
use Hyperf\Metric\Contract\MetricCollectorInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Collect and handle metrics before worker start. | ||
* Only used for swoole process mode. | ||
*/ | ||
class MetricBufferWatcher implements ListenerInterface | ||
{ | ||
private ConfigInterface $config; | ||
|
||
private MetricCollectorInterface $collector; | ||
|
||
private Timer $timer; | ||
|
||
public function __construct(protected ContainerInterface $container) | ||
{ | ||
$this->config = $container->get(ConfigInterface::class); | ||
$this->collector = $container->get(MetricCollectorInterface::class); | ||
$this->timer = new Timer(); | ||
} | ||
|
||
/** | ||
* @return string[] returns the events that you want to listen | ||
*/ | ||
public function listen(): array | ||
{ | ||
return [ | ||
BeforeWorkerStart::class, | ||
]; | ||
} | ||
|
||
/** | ||
* Handle the Event when the event is triggered, all listeners will | ||
* complete before the event is returned to the EventDispatcher. | ||
*/ | ||
public function process(object $event): void | ||
{ | ||
if ($event->workerId === null) { | ||
return; | ||
} | ||
|
||
/* | ||
* Only start buffer watcher in standalone process mode | ||
*/ | ||
if (! $this->config->get('metric.use_standalone_process', true)) { | ||
return; | ||
} | ||
|
||
$timerInterval = $this->config->get('metric.buffer_interval', 5); | ||
$timerId = $this->timer->tick($timerInterval, function () { | ||
$this->collector->flush(); | ||
}); | ||
// Clean up timer on worker exit; | ||
Coroutine::create(function () use ($timerId) { | ||
CoordinatorManager::until(Constants::WORKER_EXIT)->yield(); | ||
$this->timer->clear($timerId); | ||
}); | ||
} | ||
} |
Oops, something went wrong.