Skip to content

Commit e302601

Browse files
committed
refactor: cover monolog telemetry bridge with mago
1 parent a6a7c47 commit e302601

5 files changed

Lines changed: 61 additions & 47 deletions

File tree

Justfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ analyze-mago *args:
7878
src/bridge/telemetry/otlp \
7979
src/bridge/filesystem/async-aws \
8080
src/bridge/filesystem/azure \
81-
src/bridge/monolog/http
81+
src/bridge/monolog/http \
82+
src/bridge/monolog/telemetry
8283

8384
# Auto-fix code style (Mago format + lint --fix) and GitHub Actions findings (zizmor --fix).
8485
fix:

src/bridge/monolog/telemetry/src/Flow/Bridge/Monolog/Telemetry/LogRecordConverter.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,27 @@ private function applyAttributes(TelemetryLogRecord $telemetryRecord, LogRecord
3939
$record->level->name,
4040
);
4141

42-
foreach ($record->context as $key => $value) {
43-
if ($value instanceof Throwable) {
44-
$telemetryRecord = $telemetryRecord->setException($value);
42+
$context = $record->context;
43+
44+
foreach (array_keys($context) as $key) {
45+
if ($context[$key] instanceof Throwable) {
46+
$telemetryRecord = $telemetryRecord->setException($context[$key]);
4547

4648
continue;
4749
}
4850

4951
$telemetryRecord = $telemetryRecord->setAttribute(
5052
"context.{$key}",
51-
$this->valueNormalizer->normalize($value),
53+
$this->valueNormalizer->normalize($context[$key]),
5254
);
5355
}
5456

55-
foreach ($record->extra as $key => $value) {
57+
$extra = $record->extra;
58+
59+
foreach (array_keys($extra) as $key) {
5660
$telemetryRecord = $telemetryRecord->setAttribute(
5761
"extra.{$key}",
58-
$this->valueNormalizer->normalize($value),
62+
$this->valueNormalizer->normalize($extra[$key]),
5963
);
6064
}
6165

src/bridge/monolog/telemetry/src/Flow/Bridge/Monolog/Telemetry/ValueNormalizer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
final readonly class ValueNormalizer
2424
{
2525
/**
26-
* Normalize a value to a type acceptable by Telemetry attributes.
27-
*
28-
* @return array<bool|\DateTimeInterface|float|int|string|\Throwable>|bool|\DateTimeInterface|float|int|string|\Throwable
26+
* @return string|int|float|bool|DateTimeInterface|Throwable|array<array-key, mixed>
2927
*/
3028
public function normalize(mixed $value): string|int|float|bool|DateTimeInterface|Throwable|array
3129
{
@@ -46,7 +44,6 @@ public function normalize(mixed $value): string|int|float|bool|DateTimeInterface
4644
}
4745

4846
if (is_array($value)) {
49-
/** @phpstan-ignore return.type */
5047
return array_map(fn($v) => $this->normalize($v), $value);
5148
}
5249

src/bridge/monolog/telemetry/tests/Flow/Bridge/Monolog/Telemetry/Tests/Integration/TelemetryHandlerIntegrationTest.php

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,15 @@ protected function write(LogRecord $record): void
226226
public function test_logs_after_span_completion_have_no_trace_context(): void
227227
{
228228
$context = TelemetryTestContext::createWithTracing();
229-
static::assertNotNull($context->tracer);
229+
$tracer = $context->tracer;
230+
static::assertNotNull($tracer);
230231

231232
$monolog = new MonologLogger('application');
232233
$monolog->pushHandler(telemetry_handler($context->logger));
233234

234-
$span = $context->tracer->span('short-operation');
235+
$span = $tracer->span('short-operation');
235236
$monolog->info('Inside span');
236-
$context->tracer->complete($span);
237+
$tracer->complete($span);
237238

238239
$monolog->info('After span completed');
239240

@@ -247,34 +248,37 @@ public function test_logs_after_span_completion_have_no_trace_context(): void
247248
public function test_logs_within_active_span_include_trace_and_span_ids(): void
248249
{
249250
$context = TelemetryTestContext::createWithTracing();
250-
static::assertNotNull($context->tracer);
251+
$tracer = $context->tracer;
252+
static::assertNotNull($tracer);
251253

252254
$monolog = new MonologLogger('application');
253255
$monolog->pushHandler(telemetry_handler($context->logger));
254256

255-
$span = $context->tracer->span('process-order');
257+
$span = $tracer->span('process-order');
256258

257259
$monolog->info('Processing order', ['order_id' => 123]);
258260
$monolog->warning('Low inventory', ['product_id' => 456]);
259261

260-
$context->tracer->complete($span);
262+
$tracer->complete($span);
261263

262264
$entries = $context->processor->entries();
263265
static::assertCount(2, $entries);
264266

265-
static::assertNotNull($entries[0]->spanContext);
266-
static::assertNotNull($entries[0]->spanContext->traceId);
267-
static::assertNotNull($entries[0]->spanContext->spanId);
267+
$spanContext0 = $entries[0]->spanContext;
268+
$spanContext1 = $entries[1]->spanContext;
269+
static::assertNotNull($spanContext0);
270+
static::assertNotNull($spanContext0->traceId);
271+
static::assertNotNull($spanContext0->spanId);
268272

269-
static::assertNotNull($entries[1]->spanContext);
273+
static::assertNotNull($spanContext1);
270274
static::assertSame(
271-
$entries[0]->spanContext->traceId->toHex(),
272-
$entries[1]->spanContext->traceId->toHex(),
275+
$spanContext0->traceId->toHex(),
276+
$spanContext1->traceId->toHex(),
273277
'Both logs should share the same trace_id',
274278
);
275279
static::assertSame(
276-
$entries[0]->spanContext->spanId->toHex(),
277-
$entries[1]->spanContext->spanId->toHex(),
280+
$spanContext0->spanId->toHex(),
281+
$spanContext1->spanId->toHex(),
278282
'Both logs should share the same span_id',
279283
);
280284
}
@@ -425,32 +429,36 @@ public function test_nested_data_structures_in_context(): void
425429
public function test_nested_spans_propagate_child_span_id_to_logs(): void
426430
{
427431
$context = TelemetryTestContext::createWithTracing();
428-
static::assertNotNull($context->tracer);
432+
$tracer = $context->tracer;
433+
static::assertNotNull($tracer);
429434

430435
$monolog = new MonologLogger('application');
431436
$monolog->pushHandler(telemetry_handler($context->logger));
432437

433-
$parentSpan = $context->tracer->span('parent-operation');
438+
$parentSpan = $tracer->span('parent-operation');
434439
$monolog->info('In parent span');
435440

436-
$childSpan = $context->tracer->span('child-operation');
441+
$childSpan = $tracer->span('child-operation');
437442
$monolog->info('In child span');
438443

439-
$context->tracer->complete($childSpan);
444+
$tracer->complete($childSpan);
440445
$monolog->info('Back in parent span');
441446

442-
$context->tracer->complete($parentSpan);
447+
$tracer->complete($parentSpan);
443448

444449
$entries = $context->processor->entries();
445450
static::assertCount(3, $entries);
446451

447-
$parentSpanId = $entries[0]->spanContext?->spanId->toHex();
448-
$childSpanId = $entries[1]->spanContext?->spanId->toHex();
449-
$backInParentSpanId = $entries[2]->spanContext?->spanId->toHex();
452+
$spanContext0 = $entries[0]->spanContext;
453+
$spanContext1 = $entries[1]->spanContext;
454+
$spanContext2 = $entries[2]->spanContext;
455+
static::assertNotNull($spanContext0);
456+
static::assertNotNull($spanContext1);
457+
static::assertNotNull($spanContext2);
450458

451-
static::assertNotNull($parentSpanId);
452-
static::assertNotNull($childSpanId);
453-
static::assertNotNull($backInParentSpanId);
459+
$parentSpanId = $spanContext0->spanId->toHex();
460+
$childSpanId = $spanContext1->spanId->toHex();
461+
$backInParentSpanId = $spanContext2->spanId->toHex();
454462

455463
static::assertNotSame($parentSpanId, $childSpanId, 'Child span should have different span_id');
456464
static::assertSame(
@@ -460,13 +468,13 @@ public function test_nested_spans_propagate_child_span_id_to_logs(): void
460468
);
461469

462470
static::assertSame(
463-
$entries[0]->spanContext->traceId->toHex(),
464-
$entries[1]->spanContext->traceId->toHex(),
471+
$spanContext0->traceId->toHex(),
472+
$spanContext1->traceId->toHex(),
465473
'All logs should share the same trace_id',
466474
);
467475
static::assertSame(
468-
$entries[1]->spanContext->traceId->toHex(),
469-
$entries[2]->spanContext->traceId->toHex(),
476+
$spanContext1->traceId->toHex(),
477+
$spanContext2->traceId->toHex(),
470478
'All logs should share the same trace_id',
471479
);
472480
}

src/bridge/monolog/telemetry/tests/Flow/Bridge/Monolog/Telemetry/Tests/Unit/ValueNormalizerTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,17 @@ public function test_normalizes_nested_arrays(): void
151151
],
152152
];
153153

154-
$result = $normalizer->normalize($input);
155-
156-
static::assertIsArray($result);
157-
static::assertIsArray($result['user']);
158-
static::assertSame('John', $result['user']['name']);
159-
static::assertIsArray($result['user']['metadata']);
160-
static::assertSame('admin', $result['user']['metadata']['role']);
154+
static::assertSame(
155+
[
156+
'user' => [
157+
'name' => 'John',
158+
'metadata' => [
159+
'role' => 'admin',
160+
],
161+
],
162+
],
163+
$normalizer->normalize($input),
164+
);
161165
}
162166

163167
public function test_normalizes_null_to_string(): void

0 commit comments

Comments
 (0)