2026-02-13 13:29:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Utopia\Span\Exporter;
|
|
|
|
|
use Utopia\Span\Span;
|
|
|
|
|
use Utopia\Span\Storage;
|
2026-05-13 09:29:15 +00:00
|
|
|
use Utopia\System\System;
|
2026-02-13 13:29:54 +00:00
|
|
|
|
|
|
|
|
Span::setStorage(new Storage\Coroutine());
|
2026-05-13 09:29:15 +00:00
|
|
|
|
|
|
|
|
// Resolve trace filters once at boot to avoid repeated env lookups per span.
|
|
|
|
|
$traceProjectId = System::getEnv('_APP_TRACE_PROJECT_ID', '');
|
|
|
|
|
$traceFunctionId = System::getEnv('_APP_TRACE_FUNCTION_ID', '');
|
|
|
|
|
$traceEnabled = $traceProjectId !== '' || $traceFunctionId !== '';
|
|
|
|
|
|
|
|
|
|
Span::addExporter(new Exporter\Pretty(), function (Span $span) use ($traceEnabled, $traceProjectId, $traceFunctionId): bool {
|
2026-03-11 23:20:51 +00:00
|
|
|
if (\str_starts_with($span->getAction(), 'listener.')) {
|
|
|
|
|
return $span->getError() !== null;
|
|
|
|
|
}
|
2026-05-13 09:29:15 +00:00
|
|
|
|
|
|
|
|
// Selective tracing: when _APP_TRACE_PROJECT_ID / _APP_TRACE_FUNCTION_ID are set,
|
|
|
|
|
// only export spans tagged with matching project.id / function.id.
|
|
|
|
|
if ($traceEnabled) {
|
|
|
|
|
if ($traceProjectId !== '' && $span->get('project.id') !== $traceProjectId) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ($traceFunctionId !== '' && $span->get('function.id') !== $traceFunctionId) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 23:20:51 +00:00
|
|
|
return true;
|
|
|
|
|
});
|