Files
appwrite/app/init/realtime/span.php
T
Prem Palanisamy 5e7d88c703 refactor(registers): dedupe logger/realtimeLogger via shared $createLogger
The realtimeLogger registry was a ~40-line copy of logger; extract the
DSN -> adapter -> Logger construction into one $createLogger closure used by
both. realtimeLogger now just resolves _APP_LOGGING_CONFIG_REALTIME (falling
back to _APP_LOGGING_CONFIG), skips Sentry — those errors go out as spans via
app/init/realtime/span.php — and delegates the rest. Cross-referenced the two
"sentry" conditions in both files. No behavior change (legacy ;-delimited
config no longer crashes the realtime registry; it disables logging like the
main one).
2026-05-12 16:30:34 +01:00

44 lines
1.8 KiB
PHP

<?php
use Utopia\Console;
use Utopia\DSN\DSN;
use Utopia\Span\Exporter;
use Utopia\Span\Span;
use Utopia\System\System;
/**
* Export Realtime error spans to a dedicated Sentry project, configured via
* `_APP_LOGGING_CONFIG_REALTIME` (falls back to `_APP_LOGGING_CONFIG`). Registered only for the
* Realtime server so the HTTP / worker / CLI servers keep reporting to the default Sentry project.
*
* Paired with the `realtimeLogger` registry in app/init/registers.php: it skips building the Sentry
* logger for the same `sentry` DSN condition, so each Realtime error is reported once — here as a
* span, or there via utopia/logger for non-Sentry providers. Keep the two conditions in sync.
*/
$loggingConfig = System::getEnv('_APP_LOGGING_CONFIG_REALTIME', '') ?: System::getEnv('_APP_LOGGING_CONFIG', '');
if (empty($loggingConfig)) {
return;
}
try {
$dsn = new DSN($loggingConfig);
if ($dsn->getScheme() === 'sentry') {
// utopia/logger DSN (`sentry://PROJECT_ID:KEY@HOST/`) -> standard Sentry DSN (`https://KEY@HOST/PROJECT_ID`).
$sentryDsn = 'https://' . ($dsn->getPassword() ?? '') . '@' . $dsn->getHost() . '/' . ($dsn->getUser() ?? '');
$isProduction = System::getEnv('_APP_ENV', 'development') === 'production';
Span::addExporter(
new Exporter\Sentry(
dsn: $sentryDsn,
environment: $isProduction ? 'production' : 'staging',
release: System::getEnv('_APP_VERSION', 'UNKNOWN'),
serverName: System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname()),
),
sampler: static fn (Span $span): bool => $span->getError() !== null,
);
}
} catch (Throwable $error) {
Console::warning('Failed to register Realtime Sentry span exporter: ' . $error->getMessage());
}