Skip to content

Commit

Permalink
test failing job tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Feb 14, 2024
1 parent 4cb3fbf commit 1cb0ed7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Instrumentation/QueueInstrumentation.php
Expand Up @@ -12,6 +12,7 @@
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\SpanKind;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SemConv\TraceAttributes;

class QueueInstrumentation implements Instrumentation
Expand Down Expand Up @@ -113,7 +114,8 @@ protected function recordJobProcessing(): void
$scope = Tracer::activeScope();
$span = Tracer::activeSpan();

$span->recordException($event->exception);
$span->recordException($event->exception)
->setStatus(StatusCode::STATUS_ERROR);

$scope?->detach();
$span->end();
Expand Down
51 changes: 51 additions & 0 deletions tests/Instrumentation/QueueInstrumentationTest.php
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Keepsuit\LaravelOpenTelemetry\Tests\Support\TestJob;
use OpenTelemetry\API\Trace\StatusCode;
use OpenTelemetry\SDK\Trace\ImmutableSpan;
use OpenTelemetry\SemConv\TraceAttributes;
use Spatie\Valuestore\Valuestore;
Expand Down Expand Up @@ -113,3 +114,53 @@
->get('traceIdInJob')->toBe($root->getTraceId())
->get('logContextInJob')->toMatchArray(['traceId' => $root->getTraceId()]);
});

it('can trace queue failing jobs', function () {
withRootSpan(function () {
dispatch(new TestJob($this->valuestore, fail: true));
});

Artisan::call('queue:work', [
'--once' => true,
]);

$root = getRecordedSpans()->first(fn (ImmutableSpan $span) => $span->getName() === 'root');
$enqueueSpan = getRecordedSpans()->first(fn (ImmutableSpan $span) => $span->getName() === TestJob::class.' enqueue');
$processSpan = getRecordedSpans()->first(fn (ImmutableSpan $span) => $span->getName() === TestJob::class.' process');

assert($root instanceof ImmutableSpan);
assert($enqueueSpan instanceof ImmutableSpan);
assert($processSpan instanceof ImmutableSpan);

$traceId = $enqueueSpan->getTraceId();
$spanId = $enqueueSpan->getSpanId();

expect($this->valuestore)
->get('uuid')->not->toBeNull()
->get('traceparentInJob')->toBe(sprintf('00-%s-%s-01', $traceId, $spanId))
->get('traceIdInJob')->toBe($traceId)
->get('logContextInJob')->toMatchArray(['traceId' => $traceId]);

expect($enqueueSpan)
->getStatus()->getCode()->toBe(StatusCode::STATUS_UNSET)
->getAttributes()->toMatchArray([
TraceAttributes::MESSAGING_SYSTEM => 'redis',
TraceAttributes::MESSAGING_OPERATION => 'enqueue',
TraceAttributes::MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_DESTINATION_NAME => 'default',
TraceAttributes::MESSAGING_DESTINATION_TEMPLATE => TestJob::class,
]);

expect($processSpan)
->not->toBeNull()
->getStatus()->getCode()->toBe(StatusCode::STATUS_ERROR)
->getEvents()->toHaveCount(1)
->getEvents()->{0}->getName()->toBe('exception')
->getAttributes()->toMatchArray([
TraceAttributes::MESSAGING_SYSTEM => 'redis',
TraceAttributes::MESSAGING_OPERATION => 'process',
TraceAttributes::MESSAGE_ID => $this->valuestore->get('uuid'),
TraceAttributes::MESSAGING_DESTINATION_NAME => 'default',
TraceAttributes::MESSAGING_DESTINATION_TEMPLATE => TestJob::class,
]);
});
10 changes: 8 additions & 2 deletions tests/Support/TestJob.php
Expand Up @@ -15,8 +15,10 @@ class TestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(protected Valuestore $valuestore)
{
public function __construct(
protected Valuestore $valuestore,
protected bool $fail = false
) {
}

public function handle(): void
Expand All @@ -25,5 +27,9 @@ public function handle(): void
$this->valuestore->put('traceparentInJob', $this->job->payload()['traceparent'] ?? null);
$this->valuestore->put('traceIdInJob', Tracer::traceId());
$this->valuestore->put('logContextInJob', Log::sharedContext());

if ($this->fail) {
throw new \Exception('Job failed');
}
}
}

0 comments on commit 1cb0ed7

Please sign in to comment.