Skip to content

Commit

Permalink
Merge pull request #11 from Treblle/feature/trace-header
Browse files Browse the repository at this point in the history
Adding the X Treblle Trace ID to the on Kernel Request
  • Loading branch information
JustSteveKing authored Dec 5, 2023
2 parents b69ff11 + f4bce9c commit 644622e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function onKernelRequest(RequestEvent $event): void
$this->httpRequest = $event->getRequest();
$this->timestampStart = microtime(true);
}

return;
}

if (method_exists($event, 'isMainRequest')) { // Symfony < 5.3
if ($event->isMainRequest()) {
$this->httpRequest = $event->getRequest();
Expand Down
30 changes: 15 additions & 15 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ public function getConfigTreeBuilder()
// @phpstan-ignore-next-line
$treeBuilder->getRootNode()
->children()
->scalarNode('endpoint_url')
->defaultValue('https://rocknrolla.treblle.com')
->end()
->scalarNode('project_id')
->end()
->scalarNode('api_key')
->end()
->booleanNode('debug')
->end()
->arrayNode('masked')
->scalarPrototype()->end()
->end()
->arrayNode('ignore')
->scalarPrototype()->end()
->end()
->scalarNode('endpoint_url')
->defaultValue('https://rocknrolla.treblle.com')
->end()
->scalarNode('project_id')
->end()
->scalarNode('api_key')
->end()
->booleanNode('debug')
->end()
->arrayNode('masked')
->scalarPrototype()->end()
->end()
->arrayNode('ignore')
->scalarPrototype()->end()
->end()
->end();

return $treeBuilder;
Expand Down
24 changes: 2 additions & 22 deletions src/DependencyInjection/TreblleConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,22 @@

class TreblleConfiguration
{
/** @var string $apiKey */
private string $apiKey;

/** @var string $projectId */
private string $projectId;

/** @var string $endpointUrl */
private string $endpointUrl;

/** @var list<string> $masked */
/** @var list<string> */
private array $masked;

/** @var list<string> $ignore */
/** @var list<string> */
private array $ignore;

/** @var bool $debug */
private bool $debug;

/**
* @param string $apiKey
* @param string $projectId
* @param string $endpointUrl
* @param array<int,string> $masked
* @param bool $debug
* @param array<int,string> $ignore
*/
public function __construct(string $apiKey, string $projectId, string $endpointUrl, array $masked, bool $debug, array $ignore = [])
Expand All @@ -42,25 +34,16 @@ public function __construct(string $apiKey, string $projectId, string $endpointU
$this->debug = $debug;
}

/**
* @return string
*/
public function getApiKey(): string
{
return $this->apiKey;
}

/**
* @return string
*/
public function getProjectId(): string
{
return $this->projectId;
}

/**
* @return string
*/
public function getEndpointUrl(): string
{
return $this->endpointUrl;
Expand All @@ -74,9 +57,6 @@ public function getMasked(): array
return $this->masked;
}

/**
* @return bool
*/
public function isDebug(): bool
{
return $this->debug;
Expand Down
10 changes: 9 additions & 1 deletion src/EventSubscriber/TreblleEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ public function __construct(Treblle $treblle, LoggerInterface $logger)
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
KernelEvents::TERMINATE => 'onKernelTerminate',
KernelEvents::EXCEPTION => 'onException',
];
}

public function onKernelRequest(KernelEvent $event): void
{
$request = $event->getRequest();
$requestId = $request->headers->get('X-TREBLLE-TRACE-ID', uniqid('req_', true));
$request->attributes->set('requestId', $requestId);
}

public function onKernelTerminate(KernelEvent $event): void
{
if (in_array($event->getRequest()->getRequestUri(), $this->treblle->ignoredUris(), true)) {
if (\in_array($event->getRequest()->getRequestUri(), $this->treblle->ignoredUris(), true)) {
return;
}

Expand Down
4 changes: 1 addition & 3 deletions src/TreblleBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use Symfony\Component\HttpKernel\Bundle\Bundle;

final class TreblleBundle extends Bundle
{
}
final class TreblleBundle extends Bundle {}
1 change: 1 addition & 0 deletions tests/DataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

/**
* @internal
*
* @coversNothing
*
* @small
Expand Down
3 changes: 2 additions & 1 deletion tests/TreblleEventSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

/**
* @internal
*
* @coversNothing
*
* @small
Expand All @@ -26,7 +27,7 @@ final class TreblleEventSubscriberTest extends TestCase
{
private TreblleEventSubscriber $subjectUnderTest;

/** @var Treblle&MockObject */
/** @var MockObject&Treblle */
private Treblle $treblle;

protected function setUp(): void
Expand Down
7 changes: 4 additions & 3 deletions tests/TreblleIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

/**
* @internal
*
* @coversNothing
*
* @small
Expand All @@ -46,7 +47,7 @@ final class TreblleIntegrationTest extends TestCase

private array $container = [];

/** @var ServerDataProvider&MockObject */
/** @var MockObject&ServerDataProvider */
private ServerDataProvider $serverDataProvider;

/** @var LanguageDataProvider&MockObject */
Expand Down Expand Up @@ -93,7 +94,7 @@ protected function setUp(): void
$this->eventSubscriber = new TreblleEventSubscriber($this->treblle, new NullLogger());
}

public function provideTestData(): iterable
public function provideIt_correctly_serializes_request_data_on_shutdownCases(): iterable
{
$server = new Server(
'1.1.1.1',
Expand Down Expand Up @@ -324,7 +325,7 @@ public function provideTestData(): iterable
}

/**
* @dataProvider provideTestData
* @dataProvider provideIt_correctly_serializes_request_data_on_shutdownCases
*/
public function test_it_correctly_serializes_request_data_on_shutdown(
Server $server,
Expand Down
4 changes: 2 additions & 2 deletions tools/rector/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$parameters->set(Option::PATHS, [
$basePath.'src',
]);
// $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80);
// $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_80);
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_74);
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__.'/tools/phpstan/config.neon');
$parameters->set(Option::SKIP, [
Expand All @@ -24,6 +24,6 @@
]);
$containerConfigurator->import(SetList::CODE_QUALITY);
$containerConfigurator->import(SetList::PHP_74);
// $containerConfigurator->import(SetList::PHP_80);
// $containerConfigurator->import(SetList::PHP_80);
$containerConfigurator->import(SetList::TYPE_DECLARATION_STRICT);
};

0 comments on commit 644622e

Please sign in to comment.