Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the X Treblle Trace ID to the on Kernel Request #11

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
$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 Expand Up @@ -89,7 +89,7 @@
$this->httpRequest->getUri(),
$this->httpRequest->headers->get('USER-AGENT', 'unknown') ?: 'unknown',
$this->httpRequest->getMethod(),
$this->normalizeHeaders($this->httpRequest->headers->all()),

Check failure on line 92 in src/DataProvider.php

View workflow job for this annotation

GitHub Actions / Execute (highest, 7.4, ubuntu-latest)

Parameter #1 $allHeaders of method Treblle\Symfony\DataProvider::normalizeHeaders() expects array<string, array<string>>, array<int|string, array<int, string|null>|string|null> given.

Check failure on line 92 in src/DataProvider.php

View workflow job for this annotation

GitHub Actions / Execute (highest, 8.0, ubuntu-latest)

Parameter #1 $allHeaders of method Treblle\Symfony\DataProvider::normalizeHeaders() expects array<string, array<string>>, array<int|string, array<int, string|null>|string|null> given.
$this->payloadAnonymizer->annonymize($requestParams),
$requestBody
);
Expand All @@ -115,7 +115,7 @@
$time = (float) microtime(true) - $this->timestampStart;

return new Response(
$this->normalizeHeaders($this->httpResponse->headers->all()),

Check failure on line 118 in src/DataProvider.php

View workflow job for this annotation

GitHub Actions / Execute (highest, 7.4, ubuntu-latest)

Parameter #1 $allHeaders of method Treblle\Symfony\DataProvider::normalizeHeaders() expects array<string, array<string>>, array<int|string, array<int, string|null>|string|null> given.

Check failure on line 118 in src/DataProvider.php

View workflow job for this annotation

GitHub Actions / Execute (highest, 8.0, ubuntu-latest)

Parameter #1 $allHeaders of method Treblle\Symfony\DataProvider::normalizeHeaders() expects array<string, array<string>>, array<int|string, array<int, string|null>|string|null> given.
$this->httpResponse->getStatusCode(),
$responseSize,
$time,
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);
};
Loading