Skip to content

Commit

Permalink
✨ add configurable options for sentry tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti committed Aug 11, 2023
1 parent 2a87978 commit 9a24141
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 17 deletions.
30 changes: 30 additions & 0 deletions Classes/Service/ConfigService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Kanti\ServerTiming\Service;

final class ConfigService
{
public function stopWatchLimit(): int
{
return (int)($this->getConfig('stop_watch_limit') ?? 100_000);
}

public function tracesSampleRate(): ?float
{
$tracesSampleRate = $this->getConfig('sentry_sample_rate');
return $tracesSampleRate === null ? null : (float)$tracesSampleRate;
}

public function enableTracing(): ?bool
{
$tracesSampleRate = $this->tracesSampleRate();
return $tracesSampleRate === null ? null : (bool)$tracesSampleRate;
}

private function getConfig(string $path): ?string
{
return $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['server_timing'][$path] ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

declare(strict_types=1);

namespace Kanti\ServerTiming\Utility;
namespace Kanti\ServerTiming\Service;

use Kanti\ServerTiming\Dto\ScriptResult;
use Kanti\ServerTiming\Dto\StopWatch;
use Pluswerk\Sentry\Service\Sentry;
use Psr\Http\Message\ServerRequestInterface;
use Sentry\ClientBuilder;
use Sentry\SentrySdk;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\SpanStatus;
use Sentry\Tracing\TransactionContext;

final class SentryUtility
final class SentryService
{
public function __construct(private readonly ConfigService $configService)
{
}

/**
* @param StopWatch[] $stopWatches
*/
Expand All @@ -38,12 +41,8 @@ public function sendSentryTrace(ScriptResult $result, array $stopWatches): void

$options = $client->getOptions();

$options->setProfilesSampleRate(1.0); // TODO do not hard code!!!
$options->setTracesSampleRate(1.0); // TODO do not hard code!!!
$options->setEnableTracing(true); // TODO do not hard code!!!

$client = (new ClientBuilder($options))->getClient();
$hub->bindClient($client);
$options->setTracesSampleRate($this->configService->tracesSampleRate() ?? $options->getTracesSampleRate());
$options->setEnableTracing($this->configService->enableTracing() ?? $options->getEnableTracing());

$transactionContext = new TransactionContext();
if ($result->isCli()) {
Expand Down Expand Up @@ -77,7 +76,10 @@ public function sendSentryTrace(ScriptResult $result, array $stopWatches): void

$hub->setSpan($transaction);

$should = $options->shouldAttachStacktrace();
$options->setAttachStacktrace(false);
$transaction->finish($stopWatches[0]->stopTime);
$options->setAttachStacktrace($should);
}

private function setContextFromRequest(TransactionContext $transactionContext, ScriptResult $result): void
Expand Down
4 changes: 2 additions & 2 deletions Classes/Utility/GuzzleUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ final class GuzzleUtility
{
public static function getHandler(): ?Closure
{
if (ServerRequestFactory::fromGlobals()->getUri()->getPath() === '/typo3/module/system/config') {
// fix bug in Configuration module
if (str_starts_with($_SERVER['REQUEST_URI'] ?? '', '/typo3/module/system/config')) {
// fix bug in Configuration Backend module
return null;
}

Expand Down
14 changes: 10 additions & 4 deletions Classes/Utility/TimingUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Exception;
use Kanti\ServerTiming\Dto\ScriptResult;
use Kanti\ServerTiming\Dto\StopWatch;
use Kanti\ServerTiming\Service\SentryService;
use Kanti\ServerTiming\Service\ConfigService;
use Psr\Http\Message\ResponseInterface;
use SplObjectStorage;
use TYPO3\CMS\Core\Context\Context;
Expand All @@ -27,9 +29,12 @@ final class TimingUtility

private readonly Closure $registerShutdownFunction;

public function __construct(Closure $registerShutdownFunction = null)
private readonly ConfigService $configService;

public function __construct(Closure $registerShutdownFunction = null, ConfigService $configService = null)
{
$this->registerShutdownFunction = $registerShutdownFunction ?? register_shutdown_function(...);
$this->configService = $configService ?? new ConfigService();
}

public static function getInstance(Closure $registerShutdownFunction = null): TimingUtility
Expand Down Expand Up @@ -105,8 +110,9 @@ public function stopWatchInternal(string $key, string $info = ''): StopWatch
$this->order[] = $phpStopWatch;
}

// TODO limit to x watches
$this->order[] = $stopWatch;
if (count($this->order) < $this->configService->stopWatchLimit()) {
$this->order[] = $stopWatch;
}

if (!$this->registered) {
$x = $this->registerShutdownFunction;
Expand All @@ -131,7 +137,7 @@ public function shutdown(ScriptResult $result): ?ResponseInterface
$stopWatch->stopIfNot();
}

GeneralUtility::makeInstance(SentryUtility::class)->sendSentryTrace($result, $this->order);
GeneralUtility::makeInstance(SentryService::class)->sendSentryTrace($result, $this->order);

if (!$this->shouldAddHeader()) {
return $result->response;
Expand Down
5 changes: 3 additions & 2 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ services:
_defaults:
autowire: true
autoconfigure: true
public: false
public: true

Kanti\ServerTiming\:
resource: '../Classes/*'
exclude: '../Classes/SqlLogging/*'
# SqlLogging can be removed from exclude if only TYPO3 12 and above are required
exclude: '../Classes/{Dto,SqlLogging}/*'

Kanti\ServerTiming\EventListener\ConsoleCommandEventListener:
tags:
Expand Down
2 changes: 2 additions & 0 deletions Tests/TimingUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kanti\ServerTiming\Tests;

use Generator;
use Kanti\ServerTiming\Service\ConfigService;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -15,6 +16,7 @@

#[CoversClass(TimingUtility::class)]
#[CoversClass(StopWatch::class)]
#[CoversClass(ConfigService::class)]
final class TimingUtilityTest extends TestCase
{
protected function setUp(): void
Expand Down
8 changes: 8 additions & 0 deletions ext_conf_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# cat=sentry; type=integer; label=StopWatch Limit
stop_watch_limit = 100000

# cat=sentry; type=string; label=Sentry Sample Rate between 0.0 and 1.0 (empty: keep default)
sentry_sample_rate =

# cat=sentry; type=string; label=Sentry CLI Sample Rate between 0.0 and 1.0 (empty: keep default)
sentry_cli_sample_rate =

0 comments on commit 9a24141

Please sign in to comment.