fix: track HTTP request stats for all monitoring endpoints
This commit is contained in:
@@ -14,7 +14,7 @@ public sealed class MonitorServer : IAsyncDisposable
|
|||||||
private readonly WebApplication _app;
|
private readonly WebApplication _app;
|
||||||
private readonly ILogger<MonitorServer> _logger;
|
private readonly ILogger<MonitorServer> _logger;
|
||||||
|
|
||||||
public MonitorServer(NatsServer server, NatsOptions options, ILoggerFactory loggerFactory)
|
public MonitorServer(NatsServer server, NatsOptions options, ServerStats stats, ILoggerFactory loggerFactory)
|
||||||
{
|
{
|
||||||
_logger = loggerFactory.CreateLogger<MonitorServer>();
|
_logger = loggerFactory.CreateLogger<MonitorServer>();
|
||||||
|
|
||||||
@@ -27,26 +27,75 @@ public sealed class MonitorServer : IAsyncDisposable
|
|||||||
|
|
||||||
var varzHandler = new VarzHandler(server, options);
|
var varzHandler = new VarzHandler(server, options);
|
||||||
|
|
||||||
_app.MapGet(basePath + "/", () => Results.Ok(new
|
_app.MapGet(basePath + "/", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new
|
||||||
{
|
{
|
||||||
endpoints = new[]
|
endpoints = new[]
|
||||||
{
|
{
|
||||||
"/varz", "/connz", "/healthz", "/routez",
|
"/varz", "/connz", "/healthz", "/routez",
|
||||||
"/gatewayz", "/leafz", "/subz", "/accountz", "/jsz",
|
"/gatewayz", "/leafz", "/subz", "/accountz", "/jsz",
|
||||||
},
|
},
|
||||||
}));
|
});
|
||||||
_app.MapGet(basePath + "/healthz", () => Results.Ok("ok"));
|
});
|
||||||
_app.MapGet(basePath + "/varz", async () => Results.Ok(await varzHandler.HandleVarzAsync()));
|
_app.MapGet(basePath + "/healthz", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/healthz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok("ok");
|
||||||
|
});
|
||||||
|
_app.MapGet(basePath + "/varz", async () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/varz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(await varzHandler.HandleVarzAsync());
|
||||||
|
});
|
||||||
|
|
||||||
// Stubs for unimplemented endpoints
|
// Stubs for unimplemented endpoints
|
||||||
_app.MapGet(basePath + "/routez", () => Results.Ok(new { }));
|
_app.MapGet(basePath + "/connz", () =>
|
||||||
_app.MapGet(basePath + "/gatewayz", () => Results.Ok(new { }));
|
{
|
||||||
_app.MapGet(basePath + "/leafz", () => Results.Ok(new { }));
|
stats.HttpReqStats.AddOrUpdate("/connz", 1, (_, v) => v + 1);
|
||||||
_app.MapGet(basePath + "/subz", () => Results.Ok(new { }));
|
return Results.Ok(new { });
|
||||||
_app.MapGet(basePath + "/subscriptionsz", () => Results.Ok(new { }));
|
});
|
||||||
_app.MapGet(basePath + "/accountz", () => Results.Ok(new { }));
|
_app.MapGet(basePath + "/routez", () =>
|
||||||
_app.MapGet(basePath + "/accstatz", () => Results.Ok(new { }));
|
{
|
||||||
_app.MapGet(basePath + "/jsz", () => Results.Ok(new { }));
|
stats.HttpReqStats.AddOrUpdate("/routez", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new { });
|
||||||
|
});
|
||||||
|
_app.MapGet(basePath + "/gatewayz", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/gatewayz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new { });
|
||||||
|
});
|
||||||
|
_app.MapGet(basePath + "/leafz", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/leafz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new { });
|
||||||
|
});
|
||||||
|
_app.MapGet(basePath + "/subz", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/subz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new { });
|
||||||
|
});
|
||||||
|
_app.MapGet(basePath + "/subscriptionsz", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/subscriptionsz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new { });
|
||||||
|
});
|
||||||
|
_app.MapGet(basePath + "/accountz", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/accountz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new { });
|
||||||
|
});
|
||||||
|
_app.MapGet(basePath + "/accstatz", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/accstatz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new { });
|
||||||
|
});
|
||||||
|
_app.MapGet(basePath + "/jsz", () =>
|
||||||
|
{
|
||||||
|
stats.HttpReqStats.AddOrUpdate("/jsz", 1, (_, v) => v + 1);
|
||||||
|
return Results.Ok(new { });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task StartAsync(CancellationToken ct)
|
public async Task StartAsync(CancellationToken ct)
|
||||||
|
|||||||
@@ -47,9 +47,6 @@ public sealed class VarzHandler
|
|||||||
_lastCpuUsage = currentCpu;
|
_lastCpuUsage = currentCpu;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Track HTTP request count for /varz
|
|
||||||
stats.HttpReqStats.AddOrUpdate("/varz", 1, (_, v) => v + 1);
|
|
||||||
|
|
||||||
return new Varz
|
return new Varz
|
||||||
{
|
{
|
||||||
Id = _server.ServerId,
|
Id = _server.ServerId,
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public sealed class NatsServer : IMessageRouter, ISubListAccess, IDisposable
|
|||||||
|
|
||||||
if (_options.MonitorPort > 0)
|
if (_options.MonitorPort > 0)
|
||||||
{
|
{
|
||||||
_monitorServer = new MonitorServer(this, _options, _loggerFactory);
|
_monitorServer = new MonitorServer(this, _options, _stats, _loggerFactory);
|
||||||
await _monitorServer.StartAsync(ct);
|
await _monitorServer.StartAsync(ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user