Skip to content

Commit dc4bc7e

Browse files
JohJohanSeldaek
andauthored
Add LoadAverageProcessor (#1803)
Co-authored-by: johan Vlaar <johan@adivare.nl> Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
1 parent 8561130 commit dc4bc7e

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

doc/02-handlers-formatters-processors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
## Processors
161161

162162
- [_PsrLogMessageProcessor_](https://github.com/Seldaek/monolog/blob/main/src/Monolog/Processor/PsrLogMessageProcessor.php): Processes a log record's message according to PSR-3 rules, replacing `{foo}` with the value from `$context['foo']`.
163+
- [_LoadAverageProcessor_](https://github.com/Seldaek/monolog/blob/main/src/Monolog/Processor/LoadAverageProcessor.php): Adds the current system load average to a log record.
163164
- [_ClosureContextProcessor_](https://github.com/Seldaek/monolog/blob/main/src/Monolog/Processor/ClosureContextProcessor.php): Allows delaying the creation of context data by setting a Closure in context which is called when the log record is used
164165
- [_IntrospectionProcessor_](https://github.com/Seldaek/monolog/blob/main/src/Monolog/Processor/IntrospectionProcessor.php): Adds the line/file/class/method from which the log call originated.
165166
- [_WebProcessor_](https://github.com/Seldaek/monolog/blob/main/src/Monolog/Processor/WebProcessor.php): Adds the current request URI, request method and client IP to a log record.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of the Monolog package.
5+
*
6+
* (c) Jordi Boggiano <j.boggiano@seld.be>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Monolog\Processor;
13+
14+
use Monolog\LogRecord;
15+
16+
/**
17+
* Injects sys_getloadavg in all records @see https://www.php.net/manual/en/function.sys-getloadavg.php
18+
*
19+
* @author Johan Vlaar <johan.vlaar.1994@gmail.com>
20+
*/
21+
class LoadAverageProcessor implements ProcessorInterface
22+
{
23+
public const LOAD_1_MINUTE = 0;
24+
public const LOAD_5_MINUTE = 1;
25+
public const LOAD_15_MINUTE = 2;
26+
27+
private const AVAILABLE_LOAD = [
28+
self::LOAD_1_MINUTE,
29+
self::LOAD_5_MINUTE,
30+
self::LOAD_15_MINUTE,
31+
];
32+
33+
/**
34+
* @var int
35+
*/
36+
protected $avgSystemLoad;
37+
38+
/**
39+
* @param self::LOAD_* $avgSystemLoad
40+
*/
41+
public function __construct(int $avgSystemLoad = self::LOAD_1_MINUTE)
42+
{
43+
if (!in_array($avgSystemLoad, self::AVAILABLE_LOAD, true)) {
44+
throw new \InvalidArgumentException(sprintf('Invalid average system load: `%s`', $avgSystemLoad));
45+
}
46+
$this->avgSystemLoad = $avgSystemLoad;
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
public function __invoke(LogRecord $record): LogRecord
53+
{
54+
if (!function_exists('sys_getloadavg')) {
55+
return $record;
56+
}
57+
$usage = sys_getloadavg();
58+
if (false === $usage) {
59+
return $record;
60+
}
61+
62+
$record->extra['load_average'] = $usage[$this->avgSystemLoad];
63+
64+
return $record;
65+
}
66+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of the Monolog package.
5+
*
6+
* (c) Jordi Boggiano <j.boggiano@seld.be>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Monolog\Processor;
13+
14+
use Monolog\Test\TestCase;
15+
16+
class LoadAverageProcessorTest extends TestCase
17+
{
18+
/**
19+
* @covers Monolog\Processor\LoadAverageProcessor::__invoke
20+
*/
21+
public function testProcessor()
22+
{
23+
$processor = new LoadAverageProcessor();
24+
$record = $processor($this->getRecord());
25+
$this->assertArrayHasKey('load_average', $record->extra);
26+
$this->assertIsFloat($record->extra['load_average']);
27+
}
28+
29+
/**
30+
* @covers Monolog\Processor\LoadAverageProcessor::__invoke
31+
*/
32+
public function testProcessorWithInvalidAvgSystemLoad()
33+
{
34+
$this->expectException(\InvalidArgumentException::class);
35+
$this->expectExceptionMessage('Invalid average system load: `3`');
36+
new LoadAverageProcessor(3);
37+
}
38+
}

0 commit comments

Comments
 (0)