Skip to content

Commit

Permalink
Use correlation id from butler-audit when logging to Graylog.
Browse files Browse the repository at this point in the history
Use the correlation-id published by butler-audit when logging
to graylog to allow for grouping all graylog entries originating
from the same request.

This change is made to be able to trace requests between systems.
  • Loading branch information
Emil Andersson authored and emil-nasso committed Mar 11, 2021
1 parent 5fc4ffb commit 96897ac
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## Changed
- Use the correlation id from butler-audit when logging to graylog to enable tracing requests/executions into graylog.

## [0.6.1] - 2021-02-18

Expand Down
6 changes: 3 additions & 3 deletions src/Logging/GraylogLoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Butler\Service\Logging;

use Butler\Audit\Facades\Auditor;
use Gelf\Publisher;
use Gelf\Transport\IgnoreErrorTransportWrapper;
use Gelf\Transport\UdpTransport;
use Illuminate\Support\Str;
use Monolog\Formatter\GelfMessageFormatter;
use Monolog\Handler\GelfHandler;
use Monolog\Logger;
Expand All @@ -18,7 +18,7 @@ class GraylogLoggerFactory
public function __invoke(array $config): Logger
{
$transport = new IgnoreErrorTransportWrapper(
new UdpTransport($config['host'], $config['port'])
app()->makeWith(UdpTransport::class, ['host' => $config['host'], 'port' => $config['port']])
);

$handler = new GelfHandler(new Publisher($transport));
Expand All @@ -27,7 +27,7 @@ public function __invoke(array $config): Logger
->setFormatter(new GelfMessageFormatter())
->pushProcessor(function ($record) use ($config) {
$record['extra'][$config['name_key']] = $config['name'];
$record['extra']['trace_id'] = (string) Str::uuid(12);
$record['extra']['trace_id'] = Auditor::correlationId();
return $record;
});

Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/GraylogLoggerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Butler\Service\Tests\Unit;

use Butler\Audit\Facades\Auditor;
use Butler\Service\Logging\GraylogLoggerFactory;
use Butler\Service\Tests\TestCase;
use Gelf\Message;
use Gelf\Transport\UdpTransport;
use Mockery;

class GraylogLoggerFactoryTest extends TestCase
{
public function test_correctly_uses_configuration_and_correlation_id_from_container()
{
Auditor::correlationId('example-correlation-id');

app()->bind(UdpTransport::class, function () {
$transport = Mockery::mock(UdpTransport::class);
$transport->expects()->send(
Mockery::on(function (Message $message) {
return $message->getShortMessage() === 'Testing'
&& $message->getAdditional('trace_id') === 'example-correlation-id'
&& $message->getAdditional('service') === 'butler-service';
})
);
return $transport;
});

$config = [
'name' => 'butler-service',
'name_key' => 'service',
'host' => '127.0.0.1',
'port' => 12201,
];

$factory = new GraylogLoggerFactory($config);
$logger = $factory($config);
$logger->info('Testing');
}
}

0 comments on commit 96897ac

Please sign in to comment.