lthn.io/app/Mod/Names/Controllers/MetricsController.php
Claude 3f294340b2
feat: UpdateDnsRecords Action, Prometheus metrics, JSON validation
- UpdateDnsRecords Action: controller method now one-liner, all DNS
  logic in Action with activity logging and edit lock.
- Prometheus metrics at /v1/metrics: chain_height, alias_count,
  claims_pending, dns_tickets, gateways_live. Grafana-ready.
- ValidateJsonRequest middleware: enforces application/json on POST,
  64KB body size limit. Applied to all /v1/* API routes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 12:12:07 +01:00

63 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Mod\Names\Controllers;
use Illuminate\Routing\Controller;
use Mod\Chain\Services\DaemonRpc;
use Mod\Gateway\Services\GatewayRegistry;
use Mod\Names\Models\DnsTicket;
use Mod\Names\Models\NameClaim;
/**
* Prometheus metrics endpoint.
*
* GET /v1/metrics
*
* Returns Prometheus text exposition format for Grafana scraping.
*/
class MetricsController extends Controller
{
public function __invoke(DaemonRpc $rpc, GatewayRegistry $registry): \Illuminate\Http\Response
{
$info = $rpc->getInfo();
$offline = isset($info['_offline']);
$lines = [];
$lines[] = '# HELP lthn_chain_height Current blockchain height';
$lines[] = '# TYPE lthn_chain_height gauge';
$lines[] = 'lthn_chain_height ' . ($info['height'] ?? 0);
$lines[] = '# HELP lthn_alias_count Total registered aliases';
$lines[] = '# TYPE lthn_alias_count gauge';
$lines[] = 'lthn_alias_count ' . ($info['alias_count'] ?? 0);
$lines[] = '# HELP lthn_tx_pool_size Pending transactions';
$lines[] = '# TYPE lthn_tx_pool_size gauge';
$lines[] = 'lthn_tx_pool_size ' . ($info['tx_pool_size'] ?? 0);
$lines[] = '# HELP lthn_daemon_online Daemon reachability';
$lines[] = '# TYPE lthn_daemon_online gauge';
$lines[] = 'lthn_daemon_online ' . ($offline ? 0 : 1);
$lines[] = '# HELP lthn_claims_pending Pending name claims';
$lines[] = '# TYPE lthn_claims_pending gauge';
$lines[] = 'lthn_claims_pending ' . NameClaim::pending()->count();
$lines[] = '# HELP lthn_claims_total Total name claims';
$lines[] = '# TYPE lthn_claims_total counter';
$lines[] = 'lthn_claims_total ' . NameClaim::count();
$lines[] = '# HELP lthn_dns_tickets_pending Pending DNS tickets';
$lines[] = '# TYPE lthn_dns_tickets_pending gauge';
$lines[] = 'lthn_dns_tickets_pending ' . DnsTicket::pending()->count();
$lines[] = '# HELP lthn_gateways_live Live paired gateways';
$lines[] = '# TYPE lthn_gateways_live gauge';
$lines[] = 'lthn_gateways_live ' . count($registry->liveGateways());
return response(implode("\n", $lines) . "\n", 200)
->header('Content-Type', 'text/plain; charset=utf-8');
}
}