Skip to content

Commit

Permalink
Feature flag to add timestamp_real (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Dec 12, 2023
1 parent 14e501c commit e5cde10
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ Notes:

* Use class `\JBZoo\Cli\Codes` to get all available exit codes.
* You can add extra context to any message. It will be serialized to JSON and displayed in the end of the message. Just use `CliHelper::getInstance()->appendExtraContext(['section' => ['var' => 'value']]);`
* You can define constant `\JBZOO_CLI_TIMESTAMP_REAL=true` to add `timestamp_real` as exta context. Sometimes it's useful for logstash if default value `@timestamp` doesn't work for you.


## Contributing
Expand Down
11 changes: 11 additions & 0 deletions src/OutputMods/Logstash.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use JBZoo\Cli\ProgressBars\AbstractProgressBar;
use JBZoo\Cli\ProgressBars\ProgressBarLight;
use JBZoo\Utils\Slug;
use Monolog\DateTimeImmutable;
use Monolog\Formatter\LogstashFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
Expand Down Expand Up @@ -134,6 +135,16 @@ protected function printMessage(

protected function prepareContext(array $context): array
{
// We use timestamp_real to use the value from it in @timestamp using the rules of the logstash service.
// In cases if the default field `@timestamp` doesn't work with the logstash service for some reason.
if (
\defined('JBZOO_CLI_TIMESTAMP_REAL')
&& JBZOO_CLI_TIMESTAMP_REAL
&& !isset($context['timestamp_real'])
) {
$context['timestamp_real'] = new DateTimeImmutable(true, new \DateTimeZone(\date_default_timezone_get()));
}

$newContext = [
'trace' => ['id' => CliHelper::createOrGetTraceId()],
'profile' => $this->getProfileInfo(),
Expand Down
7 changes: 6 additions & 1 deletion tests/CliOutputLogstashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function testFormatOfMessage(): void
'time_total_ms' => 'double',
'time_diff_ms' => 'double',
],
'timestamp_real' => 'string',
],
], $lineStruture);
}
Expand Down Expand Up @@ -97,6 +98,7 @@ public function testFormatOfMessageVerboseFisrt(): void
'process_command' => 'string',
'working_directory' => 'string',
],
'timestamp_real' => 'string',
],
], $lineStruture);
}
Expand Down Expand Up @@ -127,14 +129,16 @@ public function testFormatOfMessageVerboseLast(): void
'time_total_ms' => 'double',
'time_diff_ms' => 'double',
],
'process' => ['exit_code' => 'integer'],
'process' => ['exit_code' => 'integer'],
'timestamp_real' => 'string',
],
], $lineStruture);
}

public function testFormatOfMessageException(): void
{
$cmdResult = Helper::executeReal('test:output', ['output-mode' => 'logstash', 'exception' => 'Some message']);
dump($cmdResult);

$lineAsArray = Helper::prepareLogstash($cmdResult->std)[9]->getArrayCopy();
$lineStruture = self::replaceValues($lineAsArray);
Expand Down Expand Up @@ -166,6 +170,7 @@ public function testFormatOfMessageException(): void
'file' => 'string',
'stack_trace' => 'string',
],
'timestamp_real' => 'string',
],
], $lineStruture);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/TestApp/bin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
\define('JBZOO_PATH_BIN', JBZOO_PATH_ROOT . '/' . \pathinfo(__FILE__, \PATHINFO_BASENAME));
}

\define('JBZOO_CLI_TIMESTAMP_REAL', true);

require '../../vendor/autoload.php';

$application = new CliApplication('Dummy App', '@git-version@');
Expand Down

0 comments on commit e5cde10

Please sign in to comment.