feat: add measurements in InfluxdbService

This commit is contained in:
Maël Gangloff
2024-12-08 13:54:51 +01:00
parent c7569b9081
commit a6122b877e
6 changed files with 79 additions and 15 deletions

11
.env
View File

@@ -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

View File

@@ -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)%'

View File

@@ -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);

View File

@@ -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);
}
}
}
}

View File

@@ -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) {

View File

@@ -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;
}