89 lines
3.1 KiB
C#
89 lines
3.1 KiB
C#
using System.Text.Json.Serialization;
|
|
|
|
namespace NATS.Server.Monitoring;
|
|
|
|
public sealed class JszHandler
|
|
{
|
|
private readonly NatsServer _server;
|
|
private readonly NatsOptions _options;
|
|
|
|
/// <summary>
|
|
/// Creates a JetStream monitoring response builder bound to server runtime state.
|
|
/// </summary>
|
|
/// <param name="server">Running server instance exposing JetStream counters and IDs.</param>
|
|
/// <param name="options">Server options containing JetStream capacity configuration.</param>
|
|
public JszHandler(NatsServer server, NatsOptions options)
|
|
{
|
|
_server = server;
|
|
_options = options;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Builds a point-in-time <c>/jsz</c> style response from current server state.
|
|
/// </summary>
|
|
public JszResponse Build()
|
|
{
|
|
return new JszResponse
|
|
{
|
|
ServerId = _server.ServerId,
|
|
Now = DateTime.UtcNow,
|
|
Enabled = _server.Stats.JetStreamEnabled,
|
|
Memory = 0,
|
|
Storage = 0,
|
|
Streams = _server.JetStreamStreams,
|
|
Consumers = _server.JetStreamConsumers,
|
|
ApiTotal = (ulong)Math.Max(Interlocked.Read(ref _server.Stats.JetStreamApiTotal), 0),
|
|
ApiErrors = (ulong)Math.Max(Interlocked.Read(ref _server.Stats.JetStreamApiErrors), 0),
|
|
Config = new JetStreamConfig
|
|
{
|
|
MaxMemory = _options.JetStream?.MaxMemoryStore ?? 0,
|
|
MaxStorage = _options.JetStream?.MaxFileStore ?? 0,
|
|
StoreDir = _options.JetStream?.StoreDir ?? string.Empty,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
public sealed class JszResponse
|
|
{
|
|
/// <summary>Server identifier for the node producing this response.</summary>
|
|
[JsonPropertyName("server_id")]
|
|
public string ServerId { get; set; } = string.Empty;
|
|
|
|
/// <summary>UTC timestamp when this monitoring snapshot was generated.</summary>
|
|
[JsonPropertyName("now")]
|
|
public DateTime Now { get; set; }
|
|
|
|
/// <summary>Whether JetStream is enabled on this server.</summary>
|
|
[JsonPropertyName("enabled")]
|
|
public bool Enabled { get; set; }
|
|
|
|
/// <summary>JetStream memory usage in bytes.</summary>
|
|
[JsonPropertyName("memory")]
|
|
public ulong Memory { get; set; }
|
|
|
|
/// <summary>JetStream file-storage usage in bytes.</summary>
|
|
[JsonPropertyName("storage")]
|
|
public ulong Storage { get; set; }
|
|
|
|
/// <summary>Number of JetStream streams currently hosted.</summary>
|
|
[JsonPropertyName("streams")]
|
|
public int Streams { get; set; }
|
|
|
|
/// <summary>Number of JetStream consumers currently hosted.</summary>
|
|
[JsonPropertyName("consumers")]
|
|
public int Consumers { get; set; }
|
|
|
|
/// <summary>Total number of JetStream API requests handled.</summary>
|
|
[JsonPropertyName("api_total")]
|
|
public ulong ApiTotal { get; set; }
|
|
|
|
/// <summary>Total number of JetStream API requests that returned errors.</summary>
|
|
[JsonPropertyName("api_errors")]
|
|
public ulong ApiErrors { get; set; }
|
|
|
|
/// <summary>Configured JetStream resource limits and storage directory.</summary>
|
|
[JsonPropertyName("config")]
|
|
public JetStreamConfig Config { get; set; } = new();
|
|
}
|