Skip to content

Commit

Permalink
share traceid with logs
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Feb 9, 2024
1 parent 188bc3d commit 6133666
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Facades/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @method static SpanBuilder newSpan(string $name)
* @method static SpanInterface start(string $name)
* @method static mixed measure(string $name, Closure $callback)
* @method static void updateLogContext()
*/
class Tracer extends Facade
{
Expand Down
2 changes: 2 additions & 0 deletions src/Instrumentation/QueueInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected function recordJobStart(): void
->startSpan();

$span->activate();

Tracer::updateLogContext();
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/Support/HttpServer/TraceRequestMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function handle(Request $request, Closure $next): mixed
$span = $this->startTracing($request);
$scope = $span->activate();

Tracer::updateLogContext();

try {
$response = $next($request);

Expand Down
22 changes: 11 additions & 11 deletions src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function start(string $name): SpanInterface
*
* @param non-empty-string $name
* @param Closure(SpanInterface $span): U $callback
* @throws \Throwable
* @return (U is PendingDispatch ? null : U)
*
* @throws \Throwable
*/
public function measure(string $name, Closure $callback)
{
Expand Down Expand Up @@ -101,14 +101,14 @@ public function isRecording(): bool
return false;
}

// protected function setTraceIdForLogs(SpanInterface $span): void
// {
// if (config('opentelemetry.logs.inject_trace_id', true)) {
// $field = config('opentelemetry.logs.trace_id_field', 'traceId');
//
// Log::shareContext([
// $field => $span->getContext()->getTraceId(),
// ]);
// }
// }
public function updateLogContext(): void
{
if (config('opentelemetry.logs.inject_trace_id', true)) {
$field = config('opentelemetry.logs.trace_id_field', 'traceId');

Log::shareContext([
$field => $this->traceId(),
]);
}
}
}
5 changes: 5 additions & 0 deletions tests/Instrumentation/HttpServerInstrumentationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer;
use Keepsuit\LaravelOpenTelemetry\Instrumentation\HttpServerInstrumentation;
Expand Down Expand Up @@ -46,6 +47,10 @@
'http.response.status_code' => 200,
'http.response.body.size' => 32,
]);

expect(Log::sharedContext())->toMatchArray([
'traceId' => $traceId,
]);
});

it('can record route exception', function () {
Expand Down
3 changes: 2 additions & 1 deletion tests/Instrumentation/QueueInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@

expect($this->valuestore)
->get('traceparentInJob')->toBe(sprintf('00-%s-%s-01', $traceId, $spanId))
->get('traceIdInJob')->toBe($traceId);
->get('traceIdInJob')->toBe($traceId)
->get('logContextInJob')->toMatchArray(['traceId' => $traceId]);
});
4 changes: 3 additions & 1 deletion tests/Support/TestJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer;
use Spatie\Valuestore\Valuestore;

Expand All @@ -18,9 +19,10 @@ public function __construct(protected Valuestore $valuestore)
{
}

public function handle()
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());
}
}
18 changes: 18 additions & 0 deletions tests/TracerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Log;
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer;
use OpenTelemetry\API\Trace\SpanInterface;
use OpenTelemetry\API\Trace\SpanKind;
Expand Down Expand Up @@ -228,3 +229,20 @@
$scope->detach();
$span->end();
});

it('set traceId to log context', function () {
$span = Tracer::start('test span');
$scope = $span->activate();

expect(Log::sharedContext())->toBe([]);

Tracer::updateLogContext();

expect(Log::sharedContext())
->toMatchArray([
'traceId' => $span->getContext()->getTraceId(),
]);

$scope->detach();
$span->end();
});

0 comments on commit 6133666

Please sign in to comment.