Skip to content

Extract processor of Datadog logger #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
//...
Expand All @@ -106,6 +107,7 @@ return [
],
],
'formatter' => JsonFormatter::class,
'processors' => [DatadogProcessor::class],
],
]
];
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
"extra": {
"laravel": {
"providers": [
"Onramplab\\LaravelLogEnhancement\\LaravelLogEnhancementServiceProvider",
"Onramplab\\LaravelLogEnhancement\\DatadogLoggingServiceProvider"
"Onramplab\\LaravelLogEnhancement\\LaravelLogEnhancementServiceProvider"
]
}
}
Expand Down
53 changes: 0 additions & 53 deletions src/DatadogLoggingServiceProvider.php

This file was deleted.

44 changes: 44 additions & 0 deletions src/Processors/DatadogProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Onramplab\LaravelLogEnhancement\Processors;

use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;

/**
* Inject the trace ID and span ID to connect the log entry with the APM trace
*/
class DatadogProcessor implements ProcessorInterface
{
/**
* @inheritDoc
*/
public function __invoke(LogRecord $record): LogRecord
{
if (!$this->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();
}
}
9 changes: 2 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 0 additions & 23 deletions tests/Unit/DatadogLoggingServiceProviderTest.php

This file was deleted.

76 changes: 76 additions & 0 deletions tests/Unit/Processors/DatadogProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Onramplab\LaravelLogEnhancement\Tests\Unit\Processors;

use Illuminate\Foundation\Testing\WithFaker;
use Mockery;
use Mockery\MockInterface;
use Monolog\Level;
use Monolog\LogRecord;
use Onramplab\LaravelLogEnhancement\Processors\DatadogProcessor;
use Onramplab\LaravelLogEnhancement\Tests\TestCase;

class DatadogProcessorTest extends TestCase
{
use WithFaker;

private MockInterface $processor;

private LogRecord $record;

protected function setUp(): void
{
parent::setUp();

$this->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);
}
}