Skip to content

Commit

Permalink
changed default trace id field name & removed injection with otel logger
Browse files Browse the repository at this point in the history
  • Loading branch information
cappuc committed Apr 8, 2024
1 parent 19164f8 commit 8644e8b
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 65 deletions.
5 changes: 4 additions & 1 deletion config/opentelemetry.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@

/**
* Inject active trace id in log context
*
* When using the OpenTelemetry logger, the trace id is always injected in the exported log record.
* This option allows to inject the trace id in the log context for other loggers.
*/
'inject_trace_id' => true,

/**
* Context field name for trace id
*/
'trace_id_field' => 'traceId',
'trace_id_field' => 'traceid',
],

/**
Expand Down
19 changes: 0 additions & 19 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,6 @@ public function log(string $level, string $message, array $context = []): void
$logRecord->setAttribute($key, $value);
}

$this->injectTraceId($logRecord);

$this->logger->emit($logRecord);
}

protected function injectTraceId(LogRecord $logRecord): void
{
if (! config('opentelemetry.logs.inject_trace_id', true)) {
return;
}

$traceId = \Keepsuit\LaravelOpenTelemetry\Facades\Tracer::traceId();

if ($traceId === null) {
return;
}

$field = config('opentelemetry.logs.trace_id_field', 'traceId');

$logRecord->setAttribute($field, $traceId);
}
}
2 changes: 1 addition & 1 deletion src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function updateLogContext(): void
return;
}

$field = config('opentelemetry.logs.trace_id_field', 'traceId');
$field = config('opentelemetry.logs.trace_id_field', 'traceid');

Log::shareContext([
$field => $traceId,
Expand Down
2 changes: 1 addition & 1 deletion tests/Instrumentation/HttpServerInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
]);

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

Expand Down
6 changes: 3 additions & 3 deletions tests/Instrumentation/QueueInstrumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
->get('uuid')->not->toBeNull()
->get('traceparentInJob')->toBe(sprintf('00-%s-%s-01', $traceId, $spanId))
->get('traceIdInJob')->toBe($traceId)
->get('logContextInJob')->toMatchArray(['traceId' => $traceId]);
->get('logContextInJob')->toMatchArray(['traceid' => $traceId]);

expect($enqueueSpan)
->getAttributes()->toMatchArray([
Expand Down Expand Up @@ -112,7 +112,7 @@
->get('uuid')->not->toBeNull()
->get('traceparentInJob')->toBe(sprintf('00-%s-%s-01', $root->getTraceId(), $enqueueSpan->getSpanId()))
->get('traceIdInJob')->toBe($root->getTraceId())
->get('logContextInJob')->toMatchArray(['traceId' => $root->getTraceId()]);
->get('logContextInJob')->toMatchArray(['traceid' => $root->getTraceId()]);
});

it('can trace queue failing jobs', function () {
Expand All @@ -139,7 +139,7 @@
->get('uuid')->not->toBeNull()
->get('traceparentInJob')->toBe(sprintf('00-%s-%s-01', $traceId, $spanId))
->get('traceIdInJob')->toBe($traceId)
->get('logContextInJob')->toMatchArray(['traceId' => $traceId]);
->get('logContextInJob')->toMatchArray(['traceid' => $traceId]);

expect($enqueueSpan)
->getStatus()->getCode()->toBe(StatusCode::STATUS_UNSET)
Expand Down
39 changes: 0 additions & 39 deletions tests/Sdk/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Illuminate\Support\Facades\Log;
use Keepsuit\LaravelOpenTelemetry\Facades\Logger;
use Keepsuit\LaravelOpenTelemetry\Facades\Tracer;
use OpenTelemetry\API\Logs\Map\Psr3;
use OpenTelemetry\SDK\Common\Time\ClockFactory;
use OpenTelemetry\SDK\Logs\ReadableLogRecord;
Expand Down Expand Up @@ -62,41 +61,3 @@
LogLevel::INFO,
LogLevel::DEBUG,
]);

test('trace id is injected without log context sharing', function () {
$span = Tracer::newSpan('test')->start();
$scope = $span->activate();

$traceId = Tracer::traceId();
expect(\OpenTelemetry\API\Trace\SpanContextValidator::isValidTraceId($traceId))->toBeTrue();

Logger::info('test');

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

$log = getRecordedLogs()->first();

expect($log)
->getBody()->toBe('test')
->getAttributes()->toArray()->toBe([
'traceId' => $traceId,
]);
});

test('trace id is not injected when tracer scope is not valid', function () {
$span = Tracer::newSpan('test')->start();

$traceId = Tracer::traceId();
expect($traceId)->toBeNull();

Logger::info('test');

$span->end();

$log = getRecordedLogs()->first();

expect($log)
->getBody()->toBe('test')
->getAttributes()->toArray()->toBe([]);
});
2 changes: 1 addition & 1 deletion tests/TracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@

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

$scope->detach();
Expand Down

0 comments on commit 8644e8b

Please sign in to comment.