mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
feat: add measurements in InfluxdbService
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user