Skip to content

Commit ead9e5e

Browse files
committed
test: assert nested span parent and context restoration directly
Replace the commented-out HTTP-route span-hierarchy stubs (and the redundant "same span recorder instance" case) with two tests that drive the Tracer directly: - nested span parentId chain plus the context.spanId unwinding back to each parent (and finally null) as scopes close; - the recorder receiving the completed spans with the correct parent relationships. No production changes.
1 parent 23bdcaf commit ead9e5e

1 file changed

Lines changed: 81 additions & 50 deletions

File tree

tests/Feature/Tracing/SpanHierarchyTest.php

Lines changed: 81 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,87 @@
22

33
declare(strict_types=1);
44

5-
use LaravelTrace\LaravelTrace\Contracts\SpanRecorder;
5+
use LaravelTrace\LaravelTrace\Contracts\Tracer;
6+
use LaravelTrace\LaravelTrace\Span\SpanType;
67
use LaravelTrace\LaravelTrace\Tracing\InMemorySpanRecorder;
78

8-
// it('maintains span parent relationships during an http request', function () {
9-
// $response = $this->postJson('/trace-test/nested');
10-
//
11-
// $response->assertSuccessful();
12-
//
13-
// $spans = app(InMemorySpanRecorder::class)->all();
14-
//
15-
// expect($spans)
16-
// ->toHaveCount(3);
17-
//
18-
// [$parent, $database, $inventory] = $spans;
19-
//
20-
// expect($parent->parentId)
21-
// ->toBeNull()
22-
// ->and($database->parentId)
23-
// ->toBe($parent->id)
24-
// ->and($inventory->parentId)
25-
// ->toBe($parent->id);
26-
// });
27-
//
28-
// it('maintains deeply nested span relationships', function () {
29-
// $this->postJson('/trace-test/deep')
30-
// ->assertSuccessful();
31-
//
32-
// $spans = app(InMemorySpanRecorder::class)->all();
33-
//
34-
// expect($spans)
35-
// ->toHaveCount(3);
36-
//
37-
// [$outer, $middle, $inner] = $spans;
38-
//
39-
// expect($outer->parentId)
40-
// ->toBeNull()
41-
// ->and($middle->parentId)
42-
// ->toBe($outer->id)
43-
// ->and($inner->parentId)
44-
// ->toBe($middle->id);
45-
// });
46-
// it('uses the same span recorder instance', function () {
47-
// expect(app(SpanRecorder::class))
48-
// ->toBe(app(InMemorySpanRecorder::class));
49-
// });
50-
51-
// use LaravelTrace\LaravelTrace\Contracts\SpanRecorder;
52-
// use LaravelTrace\LaravelTrace\Tracing\InMemorySpanRecorder;
53-
54-
it('uses the same span recorder instance', function (): void {
55-
expect(app(SpanRecorder::class))
56-
->toBe(app(InMemorySpanRecorder::class));
9+
it('maintains nested span parent relationships', function (): void {
10+
$tracer = app(Tracer::class);
11+
12+
$tracer->start('test');
13+
14+
$outer = $tracer->span(
15+
name: 'outer',
16+
type: SpanType::Database,
17+
);
18+
19+
$middle = $tracer->span(
20+
name: 'middle',
21+
type: SpanType::Database,
22+
);
23+
24+
$inner = $tracer->span(
25+
name: 'inner',
26+
type: SpanType::Database,
27+
);
28+
29+
expect($outer->span()->parentId)
30+
->toBeNull()
31+
->and($middle->span()->parentId)
32+
->toBe($outer->span()->id)
33+
->and($inner->span()->parentId)
34+
->toBe($middle->span()->id);
35+
36+
$inner->close();
37+
38+
expect($tracer->context()?->spanId)
39+
->toBe($middle->span()->id);
40+
41+
$middle->close();
42+
43+
expect($tracer->context()?->spanId)
44+
->toBe($outer->span()->id);
45+
46+
$outer->close();
47+
48+
expect($tracer->context()?->spanId)
49+
->toBeNull();
50+
});
51+
52+
it('records nested spans with their correct parent relationships', function (): void {
53+
$tracer = app(Tracer::class);
54+
55+
$tracer->start('test');
56+
57+
$outer = $tracer->span(
58+
name: 'outer',
59+
type: SpanType::Database,
60+
);
61+
62+
$inner = $tracer->span(
63+
name: 'inner',
64+
type: SpanType::Database,
65+
);
66+
67+
$inner->close();
68+
$outer->close();
69+
70+
$spans = app(InMemorySpanRecorder::class)->all();
71+
72+
$outerSpan = collect($spans)
73+
->firstWhere('name', 'outer');
74+
75+
$innerSpan = collect($spans)
76+
->firstWhere('name', 'inner');
77+
78+
expect($spans)
79+
->toHaveCount(2)
80+
->and($outerSpan)
81+
->not->toBeNull()
82+
->and($innerSpan)
83+
->not->toBeNull()
84+
->and($outerSpan->parentId)
85+
->toBeNull()
86+
->and($innerSpan->parentId)
87+
->toBe($outerSpan->id);
5788
});

0 commit comments

Comments
 (0)