innmind / homeostasis
应用程序健康调节器
4.1.0
2021-02-14 10:26 UTC
Requires
- php: ~7.4|~8.0
- innmind/filesystem: ~4.0
- innmind/immutable: ~3.5
- innmind/json: ^1.2
- innmind/log-reader: ~4.0
- innmind/math: ~5.0
- innmind/server-status: ~2.0
- innmind/time-continuum: ~2.2
Requires (Dev)
- innmind/black-box: ^4.2
- innmind/coding-standard: ^1.1
- phpunit/phpunit: ~9.0
- vimeo/psalm: ~4.4
README
此库是一个机制,用于从您的应用程序的各种传感器(CPU使用率、日志中的错误等)收集指标,并根据系统的健康状况确定采取哪些行动。例如,如果应用程序长时间占用过多的CPU,则应减少服务器处理的过程数量。
本质上,这个过程总是这样的:收集传感器值 => 确定策略 => 调用执行器
。
注意:此库中没有实现执行器,因为这些是特定于应用程序的,因此您需要知道如何调节您的应用程序。
用法
use function Innmind\Homeostasis\bootstrap; use Innmind\Homeostasis\{ Factor, Factor\Cpu, Factor\Log, Sensor\Measure\Weight, }; use Innmind\OperatingSystem\Factory; use Innmind\Url\Path; use Innmind\Math\{ Polynom\Polynom, Algebra\Integer Algebra\Number\Number, }; use Innmind\LogReader\{ Reader\OnDemand, Log as LogLine, }; use Innmind\Immutable\Set; use Psr\Log\LogLevel; $os = Factory::build(); $clock = $os->clock(); $homeostasis = bootstrap( Set::of( Factor::class, new Cpu( $clock, $os->status(), new Weight(new Number(0.5)), (new Polynom)->withDegree(new Integer(1), new Integer(1)) ), new Log( $clock, new OnDemand(new Symfony($clock)), $os->filesystem()->mount(Path::of('var/logs')), new Weight(new Number(0.5)), (new Polynom)->withDegree(new Integer(1), new Integer(1)), static function(LogLine $line): bool { return $line->attributes()->contains('level') && $line->attributes()->get('level')->value() === LogLevel::CRITICAL; }, 'symfony', ), ), /*you need to implement the Actuator interface*/, $os->filesystem()->mount(Path::of('some/path/to/store/states')), $clock, ); $modulateStateHistory = $homeostasis['modulate_state_history']( $os->filesystem()->mount(Path::of('some/path/to/store/actions')), ); $regulate = $modulateStateHistory( $homeostasis['regulator'], ); $regulate();
在上文中,我们定义了一个调节器,它从CPU使用率和symfony日志中的错误收集值。每个传感器具有相同的重要性/权重。
传感器必须返回一个介于 0
和 1
之间的值,其中 0
表示活动不足,1
表示活动过多。因此,明显的目标是整体值趋于 0.5
。但对于每个传感器,计算此值的方式不同且非线性,您可以指定一个多项式来调节。在上面的示例中,我们为CPU和日志指定了一个线性多项式,但您应该根据“日志中错误不足”没有意义来更改这些值;多项式应类似于以下内容
1 | /
| /
| /
| ______________/ Means that between 20% and 80% of cpu usage you're good
| / otherwise there's not enough or too much activity
| /
| /
|_/_ _ _ _ _ _ _ _ _ _ _
0 CPU 1
1 | /--------------------
| /
|/
|
| As soon there are errors in the logs you're in alert
|
|
|_ _ _ _ _ _ _ _ _ _ _ _
0 LOGS 1
最后,ModulateStateHistory
包装器用于擦除部分历史记录,否则系统将难以找到活动趋势。