Tighten span logging validation

This commit is contained in:
Chirag Aggarwal
2026-04-30 11:26:54 +05:30
parent 7175c3b7c9
commit 9e9ca86a3c
2 changed files with 62 additions and 1 deletions
+9 -1
View File
@@ -22,7 +22,15 @@ $addSentryExporter = function (string $loggingConfig, ?callable $sampler = null)
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Logging provider not supported. Logging is disabled');
}
$dsn = 'https://' . $loggingProvider->getPassword() . '@' . $loggingProvider->getHost() . '/' . ($loggingProvider->getUser() ?? '');
$projectId = $loggingProvider->getUser();
$apiKey = $loggingProvider->getPassword();
$host = $loggingProvider->getHost();
if (empty($projectId) || empty($apiKey) || empty($host)) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Invalid Sentry DSN. Logging is disabled');
}
$dsn = 'https://' . $apiKey . '@' . $host . '/' . $projectId;
Span::addExporter(
new Exporter\Sentry(
+53
View File
@@ -40,4 +40,57 @@ class SpanTest extends TestCase
$this->assertSame('worker.error', $exporter->exported[0]->getAction());
$this->assertSame($error, $exporter->exported[0]->getError());
}
public function testSamplerSkipsUnmatchedSpan(): void
{
$exporter = new class () implements Exporter {
/**
* @var array<Span>
*/
public array $exported = [];
public function export(Span $span): void
{
$this->exported[] = $span;
}
};
Span::addExporter(
exporter: $exporter,
sampler: fn (Span $span): bool => $span->get('appwrite.error.publish') === true,
);
$span = new Span('worker.error');
$span->set('appwrite.error.publish', false);
$span->finish();
$this->assertCount(0, $exporter->exported);
}
public function testSamplerExportsMatchedSpan(): void
{
$exporter = new class () implements Exporter {
/**
* @var array<Span>
*/
public array $exported = [];
public function export(Span $span): void
{
$this->exported[] = $span;
}
};
Span::addExporter(
exporter: $exporter,
sampler: fn (Span $span): bool => $span->get('appwrite.error.publish') === true,
);
$span = new Span('worker.error');
$span->set('appwrite.error.publish', true);
$span->finish();
$this->assertCount(1, $exporter->exported);
$this->assertSame('worker.error', $exporter->exported[0]->getAction());
}
}