diff --git a/README.md b/README.md index 1acc187..77378e1 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ You can adding following block into `config/logging.php`. ```php use Monolog\Formatter\JsonFormatter; use Onramplab\LaravelLogEnhancement\Handlers\DatadogHandler; +use Onramplab\LaravelLogEnhancement\Processors\DatadogProcessor; return [ //... @@ -106,6 +107,7 @@ return [ ], ], 'formatter' => JsonFormatter::class, + 'processors' => [DatadogProcessor::class], ], ] ]; diff --git a/composer.json b/composer.json index 1878e9a..95ade99 100644 --- a/composer.json +++ b/composer.json @@ -54,8 +54,7 @@ "extra": { "laravel": { "providers": [ - "Onramplab\\LaravelLogEnhancement\\LaravelLogEnhancementServiceProvider", - "Onramplab\\LaravelLogEnhancement\\DatadogLoggingServiceProvider" + "Onramplab\\LaravelLogEnhancement\\LaravelLogEnhancementServiceProvider" ] } } diff --git a/src/DatadogLoggingServiceProvider.php b/src/DatadogLoggingServiceProvider.php deleted file mode 100644 index aae52a2..0000000 --- a/src/DatadogLoggingServiceProvider.php +++ /dev/null @@ -1,53 +0,0 @@ -getLogger(); - if (!$monolog instanceof \Monolog\Logger) { - return; - } - - foreach ($monolog->getHandlers() as $handler) { - if (method_exists($handler, 'setFormatter')) { - $handler->setFormatter(new \Monolog\Formatter\JsonFormatter()); - } - } - - // Inject the trace and span ID to connect the log entry with the APM trace - $monolog->pushProcessor(function ($record) { - $context = \DDTrace\current_context(); - $record->extra['dd'] = [ - 'trace_id' => $context['trace_id'], - 'span_id' => $context['span_id'], - ]; - return $record; - }); - } - - /** - * Bootstrap any application services. - * - * @return void - */ - public function boot() - { - // - } -} \ No newline at end of file diff --git a/src/Processors/DatadogProcessor.php b/src/Processors/DatadogProcessor.php new file mode 100644 index 0000000..083d46b --- /dev/null +++ b/src/Processors/DatadogProcessor.php @@ -0,0 +1,44 @@ +isContextExisted()) { + return $record; + } + + $context = $this->getContext(); + $record->extra['dd'] = [ + 'trace_id' => $context['trace_id'], + 'span_id' => $context['span_id'], + ]; + + return $record; + } + + public function isContextExisted(): bool + { + // can get function after installing Datadog php extension + return function_exists('\DDTrace\current_context'); + } + + /** + * @return array{trace_id: string, span_id: string, version: string, env: string} + */ + public function getContext(): array + { + return \DDTrace\current_context(); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index c9ac580..1cfefe4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,16 +7,11 @@ class TestCase extends BaseTestCase { - public function setup() : void + protected function setUp() : void { parent::setUp(); - $this->withoutExceptionHandling(); - // $this->artisan('migrate', ['--database' => 'testing']); - - // $this->loadMigrationsFrom(__DIR__ . '/../src/database/migrations'); - // $this->loadLaravelMigrations(['--database' => 'testing']); - // $this->withFactories(__DIR__.'/../src/database/factories'); + $this->withoutExceptionHandling(); } protected function getEnvironmentSetUp($app) diff --git a/tests/Unit/DatadogLoggingServiceProviderTest.php b/tests/Unit/DatadogLoggingServiceProviderTest.php deleted file mode 100644 index 5a80693..0000000 --- a/tests/Unit/DatadogLoggingServiceProviderTest.php +++ /dev/null @@ -1,23 +0,0 @@ -assertInstanceOf(DatadogLoggingServiceProvider::class, $this->app->getProvider(DatadogLoggingServiceProvider::class)); - } -} diff --git a/tests/Unit/Processors/DatadogProcessorTest.php b/tests/Unit/Processors/DatadogProcessorTest.php new file mode 100644 index 0000000..2c54bf4 --- /dev/null +++ b/tests/Unit/Processors/DatadogProcessorTest.php @@ -0,0 +1,76 @@ +processor = Mockery::mock(DatadogProcessor::class)->makePartial(); + $this->record = new LogRecord( + datetime: now()->toDateTimeImmutable(), + channel: 'fake-channel', + level: Level::Debug, + message: 'fake message', + context: [], + extra: [], + ); + } + + /** + * @test + */ + public function processor_should_inject_extra_data_when_context_is_existed(): void + { + $context = [ + 'trace_id' => $this->faker->uuid(), + 'span_id' => $this->faker->uuid(), + ]; + + $this->processor + ->shouldReceive('isContextExisted') + ->once() + ->andReturn(true); + + $this->processor + ->shouldReceive('getContext') + ->once() + ->andReturn($context); + + $record = ($this->processor)($this->record); + + $this->assertSame($context['trace_id'], $record->extra['dd']['trace_id']); + $this->assertSame($context['span_id'], $record->extra['dd']['span_id']); + } + + /** + * @test + */ + public function processor_should_not_inject_extra_data_when_context_is_not_existed(): void + { + $this->processor + ->shouldReceive('isContextExisted') + ->once() + ->andReturn(false); + + $record = ($this->processor)($this->record); + + $this->assertArrayNotHasKey('dd', $record->extra); + } +}