diff --git a/src/FlareProvider.php b/src/FlareProvider.php index 9c3c48e..075540f 100644 --- a/src/FlareProvider.php +++ b/src/FlareProvider.php @@ -202,6 +202,7 @@ public function register(): void stacktraceMapper: $this->container->get(StacktraceMapper::class), time: $this->container->get(Time::class), ids: $this->container->get(Ids::class), + tracer: $this->container->get(Tracer::class), resource: $this->container->get(Resource::class), argumentReducers: $this->container->get(ArgumentReducers::class), collectStackTraceArguments: $collects->collectStackFrameArguments, diff --git a/src/ReportFactory.php b/src/ReportFactory.php index 9126024..18b4b46 100644 --- a/src/ReportFactory.php +++ b/src/ReportFactory.php @@ -48,6 +48,7 @@ public function __construct( protected StacktraceMapper $stacktraceMapper, protected Time $time, protected Ids $ids, + protected Tracer $tracer, public Resource $resource, public null|ArgumentReducers $argumentReducers, public bool $collectStackTraceArguments, @@ -180,6 +181,8 @@ public function toArray(): array 'openFrameIndex' => null, 'applicationPath' => $this->applicationPath, 'trackingUuid' => $this->trackingUuid ?? $this->ids->uuid(), + 'traceId' => $this->tracer->currentTraceId(), + 'spanId' => $this->tracer->currentSpanId(), 'handled' => $this->handled, 'attributes' => $attributes, 'code' => $this->throwable->getCode(), diff --git a/tests/ReportTest.php b/tests/ReportTest.php index e533dde..c85139d 100644 --- a/tests/ReportTest.php +++ b/tests/ReportTest.php @@ -51,6 +51,15 @@ ); }); +it('attaches the current trace and span id to a report', function () { + $flare = setupFlare(); + + $report = $flare->report(new Exception('this is an exception'))->toArray(); + + expect($report['traceId'])->toBe($flare->tracer->currentTraceId()); + expect($report['spanId'])->toBe($flare->tracer->currentSpanId()); +}); + it('can create a report with error exception and will cleanup the stack trace', function () { $flare = setupFlare(); diff --git a/tests/TestClasses/ReportDriver.php b/tests/TestClasses/ReportDriver.php index 5cd1769..a4ec7ce 100644 --- a/tests/TestClasses/ReportDriver.php +++ b/tests/TestClasses/ReportDriver.php @@ -14,6 +14,7 @@ public function serialize($data): string $data = $this->emptyStacktrace($data); $data = $this->removePhpunitArguments($data); $data = $this->removeUuid($data); + $data = $this->removeTraceInfo($data); $data = $this->removeResourceAttributes($data); $yaml = parent::serialize($data); @@ -27,6 +28,7 @@ public function match($expected, $actual) $actual = $this->emptyStacktrace($actual); $actual = $this->removePhpunitArguments($actual); $actual = $this->removeUuid($actual); + $actual = $this->removeTraceInfo($actual); $actual = $this->removeResourceAttributes($actual); if (is_array($actual)) { @@ -70,6 +72,14 @@ protected function removeUuid(array $data): array return $data; } + protected function removeTraceInfo(array $data): array + { + $data['traceId'] = 'fake-trace-id'; + $data['spanId'] = 'fake-span-id'; + + return $data; + } + protected function removeResourceAttributes(array $data): array { $data['attributes']['telemetry.sdk.language'] = 'fake-telemetry-sdk-language'; diff --git a/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml b/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml index 9bf6140..9f6994e 100644 --- a/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml +++ b/tests/__snapshots__/FlareTest__it_can_report_an_exception__1.yml @@ -6,6 +6,8 @@ previous: { } openFrameIndex: null applicationPath: null trackingUuid: fake-uuid +traceId: fake-trace-id +spanId: fake-span-id handled: null attributes: service.name: 'PHP application' diff --git a/tests/__snapshots__/FlareTest__it_can_report_an_exception__2.yml b/tests/__snapshots__/FlareTest__it_can_report_an_exception__2.yml index 9bf6140..9f6994e 100644 --- a/tests/__snapshots__/FlareTest__it_can_report_an_exception__2.yml +++ b/tests/__snapshots__/FlareTest__it_can_report_an_exception__2.yml @@ -6,6 +6,8 @@ previous: { } openFrameIndex: null applicationPath: null trackingUuid: fake-uuid +traceId: fake-trace-id +spanId: fake-span-id handled: null attributes: service.name: 'PHP application' diff --git a/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml index f833475..3102493 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_a_report__1.yml @@ -6,6 +6,8 @@ previous: { } openFrameIndex: null applicationPath: null trackingUuid: fake-uuid +traceId: fake-trace-id +spanId: fake-span-id handled: null attributes: service.name: 'PHP application' diff --git a/tests/__snapshots__/ReportTest__it_can_create_a_report__2.yml b/tests/__snapshots__/ReportTest__it_can_create_a_report__2.yml index f833475..3102493 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_a_report__2.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_a_report__2.yml @@ -6,6 +6,8 @@ previous: { } openFrameIndex: null applicationPath: null trackingUuid: fake-uuid +traceId: fake-trace-id +spanId: fake-span-id handled: null attributes: service.name: 'PHP application' diff --git a/tests/__snapshots__/ReportTest__it_can_create_an_error_exception_report__1.yml b/tests/__snapshots__/ReportTest__it_can_create_an_error_exception_report__1.yml index 46f4902..0cbbd2c 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_an_error_exception_report__1.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_an_error_exception_report__1.yml @@ -6,6 +6,8 @@ previous: { } openFrameIndex: null applicationPath: null trackingUuid: fake-uuid +traceId: fake-trace-id +spanId: fake-span-id handled: null attributes: service.name: 'PHP application' diff --git a/tests/__snapshots__/ReportTest__it_can_create_an_error_exception_report__2.yml b/tests/__snapshots__/ReportTest__it_can_create_an_error_exception_report__2.yml index 46f4902..0cbbd2c 100644 --- a/tests/__snapshots__/ReportTest__it_can_create_an_error_exception_report__2.yml +++ b/tests/__snapshots__/ReportTest__it_can_create_an_error_exception_report__2.yml @@ -6,6 +6,8 @@ previous: { } openFrameIndex: null applicationPath: null trackingUuid: fake-uuid +traceId: fake-trace-id +spanId: fake-span-id handled: null attributes: service.name: 'PHP application'