|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | 5 | use LaravelTrace\LaravelTrace\Context\InMemoryTraceContextStore; |
| 6 | +use LaravelTrace\LaravelTrace\Span\Span; |
| 7 | +use LaravelTrace\LaravelTrace\Span\SpanStatus; |
6 | 8 | use LaravelTrace\LaravelTrace\Span\SpanType; |
| 9 | +use LaravelTrace\LaravelTrace\Trace\TraceId; |
7 | 10 | use LaravelTrace\LaravelTrace\Tracing\Tracer; |
8 | 11 |
|
9 | 12 | it('restores the previous context when closed', function () { |
|
32 | 35 | expect($tracer->context()) |
33 | 36 | ->toBe($parentContext); |
34 | 37 | }); |
| 38 | + |
| 39 | +it('fails a span and restores the previous context', function () { |
| 40 | + $store = new InMemoryTraceContextStore(); |
| 41 | + $tracer = new Tracer($store); |
| 42 | + |
| 43 | + $tracer->start('CreateOrder'); |
| 44 | + |
| 45 | + $parent = $tracer->span( |
| 46 | + 'CreateOrder', |
| 47 | + SpanType::Action, |
| 48 | + ); |
| 49 | + |
| 50 | + $parentContext = $tracer->context(); |
| 51 | + |
| 52 | + $child = $tracer->span( |
| 53 | + 'ReserveInventory', |
| 54 | + SpanType::Action, |
| 55 | + ); |
| 56 | + |
| 57 | + $exception = new RuntimeException( |
| 58 | + 'Inventory service failed', |
| 59 | + ); |
| 60 | + |
| 61 | + $failed = $child->fail($exception); |
| 62 | + |
| 63 | + expect($failed->status) |
| 64 | + ->toBe(SpanStatus::Failed) |
| 65 | + ->and($failed->error?->type) |
| 66 | + ->toBe(RuntimeException::class) |
| 67 | + ->and($failed->error?->message) |
| 68 | + ->toBe('Inventory service failed') |
| 69 | + ->and($tracer->context()) |
| 70 | + ->toBe($parentContext); |
| 71 | +}); |
| 72 | + |
| 73 | +it('does not retain an error when completing a span', function () { |
| 74 | + $traceId = TraceId::generate(); |
| 75 | + |
| 76 | + $span = Span::start( |
| 77 | + traceId: $traceId, |
| 78 | + name: 'ReserveInventory', |
| 79 | + type: SpanType::Action, |
| 80 | + ); |
| 81 | + |
| 82 | + $completed = $span->complete( |
| 83 | + new DateTimeImmutable(), |
| 84 | + ); |
| 85 | + |
| 86 | + expect($completed->status) |
| 87 | + ->toBe(SpanStatus::Completed) |
| 88 | + ->and($completed->error) |
| 89 | + ->toBeNull(); |
| 90 | +}); |
0 commit comments