Skip to content

Commit 112772b

Browse files
authored
Record exception with stack trace on log records (#410)
* Update LogWatcher.php * add test coverage * removes exception from context
1 parent 9fea410 commit 112772b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/Instrumentation/Laravel/src/Watchers/LogWatcher.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
1111
use OpenTelemetry\API\Logs\LogRecord;
1212
use OpenTelemetry\API\Logs\Severity;
13+
use OpenTelemetry\SDK\Common\Exception\StackTraceFormatter;
14+
use Throwable;
1315
use TypeError;
1416

1517
class LogWatcher extends Watcher
@@ -52,8 +54,21 @@ public function recordLog(MessageLogged $log): void
5254
// Should this fail, we should continue to emit the LogRecord.
5355
}
5456

57+
$contextToEncode = array_filter($log->context);
58+
59+
$exception = $this->getExceptionFromContext($log->context);
60+
61+
if ($exception !== null) {
62+
unset($contextToEncode['exception']);
63+
}
64+
5565
$attributes = [
56-
'context' => json_encode(array_filter($log->context)),
66+
'context' => json_encode($contextToEncode),
67+
...$exception !== null ? [
68+
'exception.type' => $exception::class,
69+
'exception.message' => $exception->getMessage(),
70+
'exception.stacktrace' => StackTraceFormatter::format($exception),
71+
] : [],
5772
];
5873

5974
$logger = $this->instrumentation->logger();
@@ -65,4 +80,16 @@ public function recordLog(MessageLogged $log): void
6580

6681
$logger->emit($record);
6782
}
83+
84+
private function getExceptionFromContext(array $context): ?Throwable
85+
{
86+
if (
87+
! isset($context['exception']) ||
88+
! $context['exception'] instanceof Throwable
89+
) {
90+
return null;
91+
}
92+
93+
return $context['exception'];
94+
}
6895
}

src/Instrumentation/Laravel/tests/Integration/LaravelInstrumentationTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace OpenTelemetry\Tests\Contrib\Instrumentation\Laravel\Integration;
66

7+
use Exception;
78
use Illuminate\Routing\Router;
89
use Illuminate\Support\Facades\DB;
910
use Illuminate\Support\Facades\Http;
@@ -106,6 +107,16 @@ public function test_route_span_name_if_not_found(): void
106107
$this->assertSame('GET', $span->getName());
107108
}
108109

110+
public function test_records_exception_in_logs(): void
111+
{
112+
$this->router()->get('/exception', fn () => throw new Exception('Test exception'));
113+
$this->call('GET', '/exception');
114+
$logRecord = $this->storage[0];
115+
$this->assertEquals(Exception::class, $logRecord->getAttributes()->get(TraceAttributes::EXCEPTION_TYPE));
116+
$this->assertEquals('Test exception', $logRecord->getAttributes()->get(TraceAttributes::EXCEPTION_MESSAGE));
117+
$this->assertNotNull($logRecord->getAttributes()->get(TraceAttributes::EXCEPTION_STACKTRACE));
118+
}
119+
109120
private function router(): Router
110121
{
111122
/** @psalm-suppress PossiblyNullReference */

0 commit comments

Comments
 (0)