feat(introspection): add lines and version to info

This commit is contained in:
dualfroz committed 2026-09-07 21:33:03 +02:00
1 parent 8757d4b0c8
commit ca34ee8744
1 file changed
+97
+97
View File
@@ -161,3 +161,100 @@ defmodule Excache.Command.Introspection.Info do
]
end
defp lines(:persistence, _context) do
snapshotPath = Persistence.snapshot_path()
[
{"loading", 0},
{"rdb_last_save_time", fileMtime(snapshotPath)},
{"rdb_snapshot_size", fileSize(snapshotPath)},
{"aof_enabled", 1},
{"aof_rewrite_in_progress", 0},
{"aof_fsync_policy", Aof.fsync_policy()},
{"aof_current_size", fileSize(Aof.path())}
]
end
defp lines(:stats, context) do
snapshot = context.clients
runner = Runner.stats()
[
{"total_connections_received", snapshot.connectionsReceived},
{"total_net_input_bytes", snapshot.netIn},
{"total_net_output_bytes", snapshot.netOut},
{"evicted_keys", runner.evictedKeys},
{"oom_rejections", runner.oomRejections},
{"pubsub_channels", Clients.subscribedChannels()},
{"pubsub_patterns", 0}
]
end
defp lines(:replication, _context) do
[
{"role", "master"},
{"connected_slaves", 0},
{"master_failover_state", "no-failover"},
{"master_repl_offset", 0},
{"second_repl_offset", -1},
{"repl_backlog_active", 0},
{"repl_backlog_size", 0},
{"repl_backlog_first_byte_offset", 0},
{"repl_backlog_histlen", 0}
]
end
defp lines(:keyspace, context) do
case Store.dbsize() do
0 ->
[]
keys ->
[{"db0", "keys=#{keys},expires=#{context.volatile},avg_ttl=#{context.avgTtl}"}]
end
end
defp lines(:cpu, _context) do
{runtimeMillis, _sinceLast} = :erlang.statistics(:runtime)
{wallMillis, _sinceLast} = :erlang.statistics(:wall_clock)
[
{"used_cpu_total", seconds(runtimeMillis)},
{"wall_clock_seconds", seconds(wallMillis)},
{"schedulers", :erlang.system_info(:schedulers)},
{"schedulers_online", :erlang.system_info(:schedulers_online)},
{"run_queue", :erlang.statistics(:total_run_queue_lengths)}
]
end
defp version, do: :excache |> Application.spec(:vsn) |> List.to_string()
defp tcpPort do
{:ok, port} = Config.get("port")
port || 0
end
defp seconds(millis), do: :erlang.float_to_binary(millis / 1000, decimals: 2)
defp fileSize(path) do
case File.stat(path) do
{:ok, stat} -> stat.size
{:error, _reason} -> 0
end
end
defp fileMtime(path) do
case File.stat(path, time: :posix) do
{:ok, stat} -> stat.mtime
{:error, _reason} -> 0
end
end
defp humanBytes(bytes) when bytes < 1024, do: "#{bytes}B"
defp humanBytes(bytes) when bytes < 1_048_576, do: scaled(bytes, 1024, "K")
defp humanBytes(bytes) when bytes < 1_073_741_824, do: scaled(bytes, 1_048_576, "M")
defp humanBytes(bytes), do: scaled(bytes, 1_073_741_824, "G")
defp scaled(bytes, divisor, suffix),
do: :erlang.float_to_binary(bytes / divisor, decimals: 2) <> suffix
end