Skip to content

Commit 2db5cc4

Browse files
committed
chore(processor): [DatadogProcessor] new
1 parent 2996b9a commit 2db5cc4

File tree

3 files changed

+122
-7
lines changed

3 files changed

+122
-7
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Onramplab\LaravelLogEnhancement\Processors;
4+
5+
use Monolog\LogRecord;
6+
use Monolog\Processor\ProcessorInterface;
7+
8+
/**
9+
* Inject the trace ID and span ID to connect the log entry with the APM trace
10+
*/
11+
class DatadogProcessor implements ProcessorInterface
12+
{
13+
/**
14+
* @inheritDoc
15+
*/
16+
public function __invoke(LogRecord $record): LogRecord
17+
{
18+
// can get function after install Datadog php extension
19+
if (!$this->isContextExisted()) {
20+
return $record;
21+
}
22+
23+
$context = $this->getContext();
24+
$record->extra['dd'] = [
25+
'trace_id' => $context['trace_id'],
26+
'span_id' => $context['span_id'],
27+
];
28+
29+
return $record;
30+
}
31+
32+
public function isContextExisted(): bool
33+
{
34+
return !function_exists('\DDTrace\current_context');
35+
}
36+
37+
/**
38+
* @return array{trace_id: string, span_id: string, version: string, env: string}
39+
*/
40+
public function getContext(): array
41+
{
42+
return \DDTrace\current_context();
43+
}
44+
}

tests/TestCase.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@
77

88
class TestCase extends BaseTestCase
99
{
10-
public function setup() : void
10+
protected function setUp() : void
1111
{
1212
parent::setUp();
13-
$this->withoutExceptionHandling();
14-
// $this->artisan('migrate', ['--database' => 'testing']);
15-
16-
// $this->loadMigrationsFrom(__DIR__ . '/../src/database/migrations');
17-
// $this->loadLaravelMigrations(['--database' => 'testing']);
1813

19-
// $this->withFactories(__DIR__.'/../src/database/factories');
14+
$this->withoutExceptionHandling();
2015
}
2116

2217
protected function getEnvironmentSetUp($app)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Onramplab\LaravelLogEnhancement\Tests\Unit\Processors;
4+
5+
use Illuminate\Foundation\Testing\WithFaker;
6+
use Mockery;
7+
use Mockery\MockInterface;
8+
use Monolog\Level;
9+
use Monolog\LogRecord;
10+
use Onramplab\LaravelLogEnhancement\Processors\DatadogProcessor;
11+
use Onramplab\LaravelLogEnhancement\Tests\TestCase;
12+
13+
class DatadogProcessorTest extends TestCase
14+
{
15+
use WithFaker;
16+
17+
private MockInterface $processor;
18+
19+
private LogRecord $record;
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->processor = Mockery::mock(DatadogProcessor::class)->makePartial();
26+
$this->record = new LogRecord(
27+
datetime: now()->toDateTimeImmutable(),
28+
channel: 'fake-channel',
29+
level: Level::Debug,
30+
message: 'fake message',
31+
context: [],
32+
extra: [],
33+
);
34+
}
35+
36+
/**
37+
* @test
38+
*/
39+
public function processor_should_inject_extra_data_when_context_is_existed(): void
40+
{
41+
$context = [
42+
'trace_id' => $this->faker->uuid(),
43+
'span_id' => $this->faker->uuid(),
44+
];
45+
46+
$this->processor
47+
->shouldReceive('isContextExisted')
48+
->once()
49+
->andReturn(true);
50+
51+
$this->processor
52+
->shouldReceive('getContext')
53+
->once()
54+
->andReturn($context);
55+
56+
$record = ($this->processor)($this->record);
57+
58+
$this->assertSame($context['trace_id'], $record->extra['dd']['trace_id']);
59+
$this->assertSame($context['span_id'], $record->extra['dd']['span_id']);
60+
}
61+
62+
/**
63+
* @test
64+
*/
65+
public function processor_should_not_inject_extra_data_when_context_is_not_existed(): void
66+
{
67+
$this->processor
68+
->shouldReceive('isContextExisted')
69+
->once()
70+
->andReturn(false);
71+
72+
$record = ($this->processor)($this->record);
73+
74+
$this->assertArrayNotHasKey('dd', $record->extra);
75+
}
76+
}

0 commit comments

Comments
 (0)