From a6122b877e9197fd1d30bcdf4fc45eb6753f247a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Sun, 8 Dec 2024 13:54:51 +0100 Subject: [PATCH] feat: add measurements in InfluxdbService --- .env | 11 +++++ config/services.yaml | 2 +- src/MessageHandler/OrderDomainHandler.php | 10 +++++ .../SendDomainEventNotifHandler.php | 8 ++++ src/Service/InfluxdbService.php | 42 +++++++++++++++++-- src/Service/RDAPService.php | 21 +++++----- 6 files changed, 79 insertions(+), 15 deletions(-) diff --git a/.env b/.env index c9be662..1ad33af 100644 --- a/.env +++ b/.env @@ -61,6 +61,8 @@ MAILER_SENDER_NAME="Domain Watchdog" MAILER_SENDER_EMAIL=notifications@example.com REGISTRATION_ENABLED=true REGISTRATION_VERIFY_EMAIL=false + +# AUTH OAUTH_CLIENT_ID= OAUTH_CLIENT_SECRET= OAUTH_AUTHORIZATION_URL= @@ -73,7 +75,16 @@ OAUTH_SCOPE= # outgoing IP address. OUTGOING_IP= + +# FEATURES LIMITED_FEATURES=false LIMIT_MAX_WATCHLIST=0 LIMIT_MAX_WATCHLIST_DOMAINS=0 LIMIT_MAX_WATCHLIST_WEBHOOKS=0 + +# STATISTICS +INFLUXDB_ENABLED=false +INFLUXDB_URL=http://localhost:8086 +INFLUXDB_TOKEN=TOKEN +INFLUXDB_BUCKET=domainwatchdog +INFLUXDB_ORG=domainwatchdog diff --git a/config/services.yaml b/config/services.yaml index e4ea61c..1214757 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -19,7 +19,7 @@ parameters: outgoing_ip: '%env(string:OUTGOING_IP)%' - influxdb_enable: '%env(bool:INFLUXDB_ENABLE)' + influxdb_enabled: '%env(bool:INFLUXDB_ENABLED)%' influxdb_url: '%env(string:INFLUXDB_URL)%' influxdb_token: '%env(string:INFLUXDB_TOKEN)%' influxdb_bucket: '%env(string:INFLUXDB_BUCKET)%' diff --git a/src/MessageHandler/OrderDomainHandler.php b/src/MessageHandler/OrderDomainHandler.php index 6aedc06..3cb1b5c 100644 --- a/src/MessageHandler/OrderDomainHandler.php +++ b/src/MessageHandler/OrderDomainHandler.php @@ -11,6 +11,7 @@ use App\Repository\DomainRepository; use App\Repository\WatchListRepository; use App\Service\ChatNotificationService; use App\Service\Connector\AbstractProvider; +use App\Service\InfluxdbService; use App\Service\StatService; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -37,8 +38,11 @@ final readonly class OrderDomainHandler private LoggerInterface $logger, private StatService $statService, private ChatNotificationService $chatNotificationService, + private InfluxdbService $influxdbService, #[Autowire(service: 'service_container')] private ContainerInterface $locator, + #[Autowire(param: 'influxdb_enabled')] + private bool $influxdbEnabled, ) { $this->sender = new Address($mailerSenderEmail, $mailerSenderName); } @@ -105,6 +109,9 @@ final readonly class OrderDomainHandler ]); $this->statService->incrementStat('stats.domain.purchased'); + if ($this->influxdbEnabled) { + $this->influxdbService->addDomainOrderPoint($connector, $domain, true); + } $notification = (new DomainOrderNotification($this->sender, $domain, $connector)); $this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage()); $this->chatNotificationService->sendChatNotification($watchList, $notification); @@ -118,6 +125,9 @@ final readonly class OrderDomainHandler ]); $this->statService->incrementStat('stats.domain.purchase.failed'); + if ($this->influxdbEnabled) { + $this->influxdbService->addDomainOrderPoint($connector, $domain, false); + } $notification = (new DomainOrderErrorNotification($this->sender, $domain)); $this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage()); $this->chatNotificationService->sendChatNotification($watchList, $notification); diff --git a/src/MessageHandler/SendDomainEventNotifHandler.php b/src/MessageHandler/SendDomainEventNotifHandler.php index cc4692e..4facc8e 100644 --- a/src/MessageHandler/SendDomainEventNotifHandler.php +++ b/src/MessageHandler/SendDomainEventNotifHandler.php @@ -12,8 +12,10 @@ use App\Notifier\DomainUpdateNotification; use App\Repository\DomainRepository; use App\Repository\WatchListRepository; use App\Service\ChatNotificationService; +use App\Service\InfluxdbService; use App\Service\StatService; use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Messenger\Attribute\AsMessageHandler; @@ -35,6 +37,9 @@ final readonly class SendDomainEventNotifHandler private DomainRepository $domainRepository, private WatchListRepository $watchListRepository, private ChatNotificationService $chatNotificationService, + #[Autowire(param: 'influxdb_enabled')] + private bool $influxdbEnabled, + private InfluxdbService $influxdbService, ) { $this->sender = new Address($mailerSenderEmail, $mailerSenderName); } @@ -82,6 +87,9 @@ final readonly class SendDomainEventNotifHandler } $this->statService->incrementStat('stats.alert.sent'); + if ($this->influxdbEnabled) { + $this->influxdbService->addDomainNotificationPoint($domain, $watchListTrigger->getAction(), true); + } } } } diff --git a/src/Service/InfluxdbService.php b/src/Service/InfluxdbService.php index e08bee1..d67b190 100644 --- a/src/Service/InfluxdbService.php +++ b/src/Service/InfluxdbService.php @@ -2,21 +2,28 @@ namespace App\Service; +use App\Config\TriggerAction; +use App\Entity\Connector; use App\Entity\Domain; use App\Entity\RdapServer; use InfluxDB2\Client; use InfluxDB2\Model\WritePrecision; use InfluxDB2\Point; use InfluxDB2\WriteType; +use Symfony\Component\DependencyInjection\Attribute\Autowire; readonly class InfluxdbService { private Client $client; public function __construct( + #[Autowire(param: 'influxdb_url')] private string $influxdbUrl = 'http://influxdb:8086', + #[Autowire(param: 'influxdb_token')] private string $influxdbToken = '', + #[Autowire(param: 'influxdb_bucket')] private string $influxdbBucket = 'domainwatchdog', + #[Autowire(param: 'influxdb_org')] private string $influxdbOrg = 'domainwatchdog', ) { $this->client = new Client([ @@ -28,18 +35,47 @@ readonly class InfluxdbService ]); } - public function addRdapRequest(RdapServer $rdapServer, Domain $domain, bool $success): void + public function addRdapQueryPoint(RdapServer $rdapServer, Domain $domain, array $info): void { - $this->writePoints(new Point('rdap_request', [ + $this->writePoints(new Point('rdap_query', [ 'domain' => $domain->getLdhName(), 'tld' => $domain->getTld()->getTld(), 'rdap_server' => $rdapServer->getUrl(), + 'primary_ip' => $info['primary_ip'], + ], [ + 'http_code' => $info['http_code'], + 'total_time_us' => $info['total_time_us'], + 'namelookup_time_us' => $info['namelookup_time_us'], + 'connect_time_us' => $info['connect_time_us'], + 'starttransfer_time_us' => $info['starttransfer_time_us'], + 'size_download' => $info['size_download'], + 'ssl_verify_result' => $info['ssl_verify_result'], + ])); + } + + public function addDomainOrderPoint(Connector $connector, Domain $domain, bool $success): void + { + $this->writePoints(new Point('domain_order', [ + 'domain' => $domain->getLdhName(), + 'tld' => $domain->getTld()->getTld(), + 'provider' => $connector->getProvider()->value, ], [ 'success' => $success, ])); } - public function writePoints(Point ...$points): void + public function addDomainNotificationPoint(Domain $domain, TriggerAction $triggerAction, bool $success): void + { + $this->writePoints(new Point('domain_notification', [ + 'domain' => $domain->getLdhName(), + 'tld' => $domain->getTld()->getTld(), + 'medium' => $triggerAction->value, + ], [ + 'success' => $success, + ])); + } + + private function writePoints(Point ...$points): void { $writeApi = $this->client->createWriteApi(['writeType' => WriteType::BATCHING, 'batchSize' => count($points)]); foreach ($points as $point) { diff --git a/src/Service/RDAPService.php b/src/Service/RDAPService.php index c4d78b3..8adebf1 100644 --- a/src/Service/RDAPService.php +++ b/src/Service/RDAPService.php @@ -25,6 +25,7 @@ use App\Repository\TldRepository; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Exception\ORMException; use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\HttpClient\Exception\ClientException; use Symfony\Component\HttpFoundation\Exception\BadRequestException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -88,7 +89,8 @@ readonly class RDAPService private LoggerInterface $logger, private StatService $statService, private InfluxdbService $influxService, - private bool $influxdbEnable = false, + #[Autowire(param: 'influxdb_enabled')] + private bool $influxdbEnabled, ) { } @@ -142,9 +144,10 @@ readonly class RDAPService try { $this->statService->incrementStat('stats.rdap_queries.count'); - $res = $this->client->request( + $req = $this->client->request( 'GET', $rdapServerUrl.'domain/'.$idnDomain - )->toArray(); + ); + $res = $req->toArray(); } catch (\Exception $e) { if ($e instanceof ClientException && 404 === $e->getResponse()->getStatusCode()) { if (null !== $domain) { @@ -158,14 +161,14 @@ readonly class RDAPService $this->em->flush(); } - if ($this->influxdbEnable) { - $this->influxService->addRdapRequest($rdapServer, $domain, false); - } - throw new NotFoundHttpException('The domain name is not present in the WHOIS database.'); } throw $e; + } finally { + if ($this->influxdbEnabled && isset($req)) { + $this->influxService->addRdapQueryPoint($rdapServer, $domain, $req->getInfo()); + } } if (null === $domain) { @@ -354,10 +357,6 @@ readonly class RDAPService $this->em->persist($domain); $this->em->flush(); - if ($this->influxdbEnable) { - $this->influxService->addRdapRequest($rdapServer, $domain, true); - } - return $domain; }