feat: add MonitorServer with /healthz and /varz endpoints

This commit is contained in:
Joseph Doherty
2026-02-22 22:20:44 -05:00
parent f6b38df291
commit f2badc3488
4 changed files with 289 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace NATS.Server.Monitoring;
/// <summary>
/// HTTP monitoring server providing /healthz, /varz, and other monitoring endpoints.
/// Corresponds to Go server/monitor.go HTTP server setup.
/// </summary>
public sealed class MonitorServer : IAsyncDisposable
{
private readonly WebApplication _app;
private readonly ILogger<MonitorServer> _logger;
public MonitorServer(NatsServer server, NatsOptions options, ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<MonitorServer>();
var builder = WebApplication.CreateSlimBuilder();
builder.WebHost.UseUrls($"http://{options.MonitorHost}:{options.MonitorPort}");
builder.Logging.ClearProviders();
_app = builder.Build();
var basePath = options.MonitorBasePath ?? "";
var varzHandler = new VarzHandler(server, options);
_app.MapGet(basePath + "/", () => Results.Ok(new
{
endpoints = new[]
{
"/varz", "/connz", "/healthz", "/routez",
"/gatewayz", "/leafz", "/subz", "/accountz", "/jsz",
},
}));
_app.MapGet(basePath + "/healthz", () => Results.Ok("ok"));
_app.MapGet(basePath + "/varz", async () => Results.Ok(await varzHandler.HandleVarzAsync()));
// Stubs for unimplemented endpoints
_app.MapGet(basePath + "/routez", () => Results.Ok(new { }));
_app.MapGet(basePath + "/gatewayz", () => Results.Ok(new { }));
_app.MapGet(basePath + "/leafz", () => Results.Ok(new { }));
_app.MapGet(basePath + "/subz", () => Results.Ok(new { }));
_app.MapGet(basePath + "/subscriptionsz", () => Results.Ok(new { }));
_app.MapGet(basePath + "/accountz", () => Results.Ok(new { }));
_app.MapGet(basePath + "/accstatz", () => Results.Ok(new { }));
_app.MapGet(basePath + "/jsz", () => Results.Ok(new { }));
}
public async Task StartAsync(CancellationToken ct)
{
await _app.StartAsync(ct);
_logger.LogInformation("Monitoring listening on {Urls}", string.Join(", ", _app.Urls));
}
public async ValueTask DisposeAsync()
{
await _app.DisposeAsync();
}
}