Skip to content

Commit 7b6f344

Browse files
committed
Monolog: Use proper setters for trace IDs
1 parent 203a7da commit 7b6f344

File tree

2 files changed

+264
-32
lines changed

2 files changed

+264
-32
lines changed

src/Lunr/Ticks/Monolog/ProcessingHandler.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Monolog\Level;
1818
use Monolog\LogRecord;
1919
use Psr\Log\LogLevel;
20+
use RuntimeException;
2021
use Throwable;
2122

2223
/**
@@ -71,12 +72,9 @@ public function __construct(
7172
protected function write(LogRecord $record): void
7273
{
7374
$fields = [
74-
'message' => is_string($record->formatted) ? $record->formatted : NULL,
75-
'level' => $record->level->value,
76-
'line' => is_string($record->extra['line'] ?? NULL) ? $record->extra['line'] : NULL,
77-
'traceID' => $this->tracingController->getTraceId(),
78-
'spanID' => $this->tracingController->getSpanId(),
79-
'parentSpanID' => $this->tracingController->getParentSpanId(),
75+
'message' => is_string($record->formatted) ? $record->formatted : NULL,
76+
'level' => $record->level->value,
77+
'line' => is_string($record->extra['line'] ?? NULL) ? $record->extra['line'] : NULL,
8078
];
8179

8280
$tags = [
@@ -111,6 +109,9 @@ protected function write(LogRecord $record): void
111109
$event = $this->eventLogger->newEvent('php_log');
112110

113111
$event->setTimestamp($record->datetime->format('Uu'));
112+
$event->setTraceId($this->tracingController->getTraceId() ?? throw new RuntimeException('Trace ID not available!'));
113+
$event->setSpanId($this->tracingController->getSpanId() ?? throw new RuntimeException('Span ID not available!'));
114+
$event->setParentSpanId($this->tracingController->getParentSpanId() ?? throw new RuntimeException('Parent Span ID not available!'));
114115
$event->addTags(array_merge($this->tracingController->getSpanSpecificTags(), $tags));
115116
$event->addFields($fields);
116117
$event->record(Precision::MicroSeconds);

src/Lunr/Ticks/Monolog/Tests/ProcessingHandlerTest.php

Lines changed: 257 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Monolog\Formatter\LineFormatter;
1919
use Monolog\Level;
2020
use Monolog\Test\MonologTestCase;
21+
use RuntimeException;
2122
use stdClass;
2223

2324
/**
@@ -28,6 +29,200 @@
2829
class ProcessingHandlerTest extends MonologTestCase
2930
{
3031

32+
/**
33+
* Tests the processing handler implementation.
34+
*
35+
* @covers \Lunr\Ticks\Monolog\ProcessingHandler::handle
36+
*/
37+
public function testHandleWithTraceIDUnavailable(): void
38+
{
39+
$record = $this->getRecord(Level::Warning, 'test', [ 'data' => new stdClass(), 'foo' => 34 ]);
40+
41+
$event = $this->createMock(EventInterface::class);
42+
43+
$event->expects($this->once())
44+
->method('setTimestamp')
45+
->with($record->datetime->format('Uu'));
46+
47+
$event->expects($this->never())
48+
->method('addTags');
49+
50+
$event->expects($this->never())
51+
->method('addFields');
52+
53+
$event->expects($this->never())
54+
->method('setTraceId');
55+
56+
$event->expects($this->never())
57+
->method('setSpanId');
58+
59+
$event->expects($this->never())
60+
->method('setParentSpanId');
61+
62+
$eventLogger = $this->createMock(EventLoggerInterface::class);
63+
64+
$eventLogger->expects($this->once())
65+
->method('newEvent')
66+
->willReturn($event);
67+
68+
$controller = $this->createMockForIntersectionOfInterfaces([
69+
TracingControllerInterface::class,
70+
TracingInfoInterface::class,
71+
]);
72+
73+
$controller->expects($this->once())
74+
->method('getTraceId')
75+
->willReturn(NULL);
76+
77+
$controller->expects($this->never())
78+
->method('getSpanId');
79+
80+
$controller->expects($this->never())
81+
->method('getParentSpanId');
82+
83+
$controller->expects($this->never())
84+
->method('getSpanSpecificTags');
85+
86+
$this->expectException(RuntimeException::class);
87+
$this->expectExceptionMessage('Trace ID not available!');
88+
89+
$handler = new ProcessingHandler($eventLogger, $controller);
90+
$handler->handle($record);
91+
}
92+
93+
/**
94+
* Tests the processing handler implementation.
95+
*
96+
* @covers \Lunr\Ticks\Monolog\ProcessingHandler::handle
97+
*/
98+
public function testHandleWithSpanIDUnavailable(): void
99+
{
100+
$traceID = '7b333e15-aa78-4957-a402-731aecbb358e';
101+
102+
$record = $this->getRecord(Level::Warning, 'test', [ 'data' => new stdClass(), 'foo' => 34 ]);
103+
104+
$event = $this->createMock(EventInterface::class);
105+
106+
$event->expects($this->once())
107+
->method('setTimestamp')
108+
->with($record->datetime->format('Uu'));
109+
110+
$event->expects($this->never())
111+
->method('addTags');
112+
113+
$event->expects($this->never())
114+
->method('addFields');
115+
116+
$event->expects($this->once())
117+
->method('setTraceId')
118+
->with($traceID);
119+
120+
$event->expects($this->never())
121+
->method('setSpanId');
122+
123+
$event->expects($this->never())
124+
->method('setParentSpanId');
125+
126+
$eventLogger = $this->createMock(EventLoggerInterface::class);
127+
128+
$eventLogger->expects($this->once())
129+
->method('newEvent')
130+
->willReturn($event);
131+
132+
$controller = $this->createMockForIntersectionOfInterfaces([
133+
TracingControllerInterface::class,
134+
TracingInfoInterface::class,
135+
]);
136+
137+
$controller->expects($this->once())
138+
->method('getTraceId')
139+
->willReturn($traceID);
140+
141+
$controller->expects($this->once())
142+
->method('getSpanId')
143+
->willReturn(NULL);
144+
145+
$controller->expects($this->never())
146+
->method('getParentSpanId');
147+
148+
$controller->expects($this->never())
149+
->method('getSpanSpecificTags');
150+
151+
$this->expectException(RuntimeException::class);
152+
$this->expectExceptionMessage('Span ID not available!');
153+
154+
$handler = new ProcessingHandler($eventLogger, $controller);
155+
$handler->handle($record);
156+
}
157+
158+
/**
159+
* Tests the processing handler implementation.
160+
*
161+
* @covers \Lunr\Ticks\Monolog\ProcessingHandler::handle
162+
*/
163+
public function testHandleWithParentSpanIDUnavailable(): void
164+
{
165+
$traceID = '7b333e15-aa78-4957-a402-731aecbb358e';
166+
$spanID = '24ec5f90-7458-4dd5-bb51-7a1e8f4baafe';
167+
168+
$record = $this->getRecord(Level::Warning, 'test', [ 'data' => new stdClass(), 'foo' => 34 ]);
169+
170+
$event = $this->createMock(EventInterface::class);
171+
172+
$event->expects($this->once())
173+
->method('setTimestamp')
174+
->with($record->datetime->format('Uu'));
175+
176+
$event->expects($this->never())
177+
->method('addTags');
178+
179+
$event->expects($this->never())
180+
->method('addFields');
181+
182+
$event->expects($this->once())
183+
->method('setTraceId')
184+
->with($traceID);
185+
186+
$event->expects($this->once())
187+
->method('setSpanId')
188+
->with($spanID);
189+
190+
$event->expects($this->never())
191+
->method('setParentSpanId');
192+
193+
$eventLogger = $this->createMock(EventLoggerInterface::class);
194+
195+
$eventLogger->expects($this->once())
196+
->method('newEvent')
197+
->willReturn($event);
198+
199+
$controller = $this->createMockForIntersectionOfInterfaces([
200+
TracingControllerInterface::class,
201+
TracingInfoInterface::class,
202+
]);
203+
204+
$controller->expects($this->once())
205+
->method('getTraceId')
206+
->willReturn($traceID);
207+
208+
$controller->expects($this->once())
209+
->method('getSpanId')
210+
->willReturn($spanID);
211+
212+
$controller->expects($this->once())
213+
->method('getParentSpanId')
214+
->willReturn(NULL);
215+
216+
$controller->expects($this->never())
217+
->method('getSpanSpecificTags');
218+
219+
$this->expectException(RuntimeException::class);
220+
$this->expectExceptionMessage('Span ID not available!');
221+
222+
$handler = new ProcessingHandler($eventLogger, $controller);
223+
$handler->handle($record);
224+
}
225+
31226
/**
32227
* Tests the processing handler implementation.
33228
*
@@ -61,14 +256,23 @@ public function testHandle(): void
61256
$event->expects($this->once())
62257
->method('addFields')
63258
->with([
64-
'message' => (new LineFormatter())->format($record),
65-
'level' => $record->level->value,
66-
'line' => NULL,
67-
'traceID' => $traceID,
68-
'spanID' => $spanID,
69-
'parentSpanID' => $parentSpanID,
259+
'message' => (new LineFormatter())->format($record),
260+
'level' => $record->level->value,
261+
'line' => NULL,
70262
]);
71263

264+
$event->expects($this->once())
265+
->method('setTraceId')
266+
->with($traceID);
267+
268+
$event->expects($this->once())
269+
->method('setSpanId')
270+
->with($spanID);
271+
272+
$event->expects($this->once())
273+
->method('setParentSpanId')
274+
->with($parentSpanID);
275+
72276
$eventLogger = $this->createMock(EventLoggerInterface::class);
73277

74278
$eventLogger->expects($this->once())
@@ -140,14 +344,23 @@ public function testHandleWithIntrospectionData(): void
140344
$event->expects($this->once())
141345
->method('addFields')
142346
->with([
143-
'message' => (new LineFormatter())->format($record),
144-
'level' => $record->level->value,
145-
'line' => 101,
146-
'traceID' => $traceID,
147-
'spanID' => $spanID,
148-
'parentSpanID' => $parentSpanID,
347+
'message' => (new LineFormatter())->format($record),
348+
'level' => $record->level->value,
349+
'line' => 101,
149350
]);
150351

352+
$event->expects($this->once())
353+
->method('setTraceId')
354+
->with($traceID);
355+
356+
$event->expects($this->once())
357+
->method('setSpanId')
358+
->with($spanID);
359+
360+
$event->expects($this->once())
361+
->method('setParentSpanId')
362+
->with($parentSpanID);
363+
151364
$eventLogger = $this->createMock(EventLoggerInterface::class);
152365

153366
$eventLogger->expects($this->once())
@@ -221,15 +434,24 @@ public function testHandleWithExceptionData(): void
221434
$event->expects($this->once())
222435
->method('addFields')
223436
->with([
224-
'message' => (new LineFormatter())->format($record),
225-
'level' => $record->level->value,
226-
'line' => NULL,
227-
'traceID' => $traceID,
228-
'spanID' => $spanID,
229-
'parentSpanID' => $parentSpanID,
230-
'stacktrace' => $exception->getTraceAsString(),
437+
'message' => (new LineFormatter())->format($record),
438+
'level' => $record->level->value,
439+
'line' => NULL,
440+
'stacktrace' => $exception->getTraceAsString(),
231441
]);
232442

443+
$event->expects($this->once())
444+
->method('setTraceId')
445+
->with($traceID);
446+
447+
$event->expects($this->once())
448+
->method('setSpanId')
449+
->with($spanID);
450+
451+
$event->expects($this->once())
452+
->method('setParentSpanId')
453+
->with($parentSpanID);
454+
233455
$eventLogger = $this->createMock(EventLoggerInterface::class);
234456

235457
$eventLogger->expects($this->once())
@@ -299,15 +521,24 @@ public function testHandleWithGenericExtraData(): void
299521
$event->expects($this->once())
300522
->method('addFields')
301523
->with([
302-
'message' => (new LineFormatter())->format($record),
303-
'level' => $record->level->value,
304-
'line' => NULL,
305-
'traceID' => $traceID,
306-
'spanID' => $spanID,
307-
'parentSpanID' => $parentSpanID,
308-
'meta' => 'bar',
524+
'message' => (new LineFormatter())->format($record),
525+
'level' => $record->level->value,
526+
'line' => NULL,
527+
'meta' => 'bar',
309528
]);
310529

530+
$event->expects($this->once())
531+
->method('setTraceId')
532+
->with($traceID);
533+
534+
$event->expects($this->once())
535+
->method('setSpanId')
536+
->with($spanID);
537+
538+
$event->expects($this->once())
539+
->method('setParentSpanId')
540+
->with($parentSpanID);
541+
311542
$eventLogger = $this->createMock(EventLoggerInterface::class);
312543

313544
$eventLogger->expects($this->once())

0 commit comments

Comments
 (0)