Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ambient-exception-traces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog-php": minor
---

Add the active OpenTelemetry trace and span IDs to captured exception events when the application provides OpenTelemetry.
7 changes: 7 additions & 0 deletions api/public-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,13 @@
}
]
},
"getTraceProperties": {
"static": true,
"abstract": false,
"final": false,
"returnType": "array",
"parameters": []
},
"overridePrimaryMechanism": {
"static": true,
"abstract": false,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"require-dev": {
"phpunit/phpunit": "^11.0",
"squizlabs/php_codesniffer": "^4.0",
"ext-zlib": "*"
"ext-zlib": "*",
"open-telemetry/api": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
261 changes: 260 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,12 @@ public function captureException(
return false;
}

$traceProperties = array_diff_key(
ExceptionPayloadBuilder::getTraceProperties(),
Comment thread
pauldambra marked this conversation as resolved.
$this->getContext()['properties'] ?? []
);
$properties = array_merge(
$traceProperties,
$additionalProperties,
[
'$exception_list' => $exceptionList,
Expand Down
6 changes: 5 additions & 1 deletion lib/ExceptionCapture.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,11 @@ private static function sendExceptionEvent(
$properties['$php_error_severity'] = $severity;
}

$properties = array_merge($providerContext['properties'], $properties);
$properties = array_merge(
ExceptionPayloadBuilder::getTraceProperties(),
$providerContext['properties'],
$properties
);

$distinctId = $providerContext['distinctId'];
if ($distinctId === null) {
Expand Down
29 changes: 29 additions & 0 deletions lib/ExceptionPayloadBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PostHog;

use OpenTelemetry\API\Trace\Span;

/**
* Builds PostHog error tracking exception payloads.
*
Expand All @@ -12,6 +14,33 @@ class ExceptionPayloadBuilder
private const CONTEXT_LINES = 5;
private const DEFAULT_MAX_FRAMES = 20;

public static function getTraceProperties(): array
{
try {
if (!class_exists(Span::class)) {
return [];
}

$context = Span::getCurrent()->getContext();
if (!$context->isValid()) {
return [];
}

$traceId = $context->getTraceId();
$spanId = $context->getSpanId();
if (
!preg_match('/^[0-9a-f]{32}$/D', $traceId) || $traceId === str_repeat('0', 32)
|| !preg_match('/^[0-9a-f]{16}$/D', $spanId) || $spanId === str_repeat('0', 16)
) {
return [];
}

return ['$trace_id' => $traceId, '$span_id' => $spanId];
} catch (\Throwable $error) {
return [];
}
}

/**
* Build a parsed exception list from a Throwable or string.
*
Expand Down
Loading
Loading