From 3599f8ccbaa94b2414d0246d55703bcf5a2ecc80 Mon Sep 17 00:00:00 2001 From: Kim Date: Mon, 6 Jan 2025 16:57:32 -0500 Subject: [PATCH] PR feedback --- lib/monitoring/uptime.ex | 123 +++++++++++++------------------- test/monitoring/uptime_test.exs | 16 +++-- 2 files changed, 61 insertions(+), 78 deletions(-) diff --git a/lib/monitoring/uptime.ex b/lib/monitoring/uptime.ex index 545a70a8..86135354 100644 --- a/lib/monitoring/uptime.ex +++ b/lib/monitoring/uptime.ex @@ -6,51 +6,31 @@ defmodule Monitoring.Uptime do {:ok, date_time} = DateTime.from_unix(timestamp, :second) Enum.each(nodes, fn node -> - prefix = get_prefix(node) + case get_log_fields(node) do + {:ok, log_fields} -> + log([date_time: date_time] ++ log_fields) - log_fields = - case prefix do - "software_uptime" -> - get_live_pa_app_fields(node) - - "headend_server_stats" -> - get_headend_server_fields(node) - - "live_pa_connectivity" -> - get_live_pa_connectivity_fields(node) - - "device_uptime" -> - get_device_status_fields(node) - - "unknown" -> - Logger.warn("unknown_node: #{inspect(node)}") - [] - end - - log(prefix, date_time, log_fields) + :error -> + Logger.warn("unknown_node: #{inspect(node)}") + end end) end - defp get_prefix(%{"sw_component" => _}), do: "software_uptime" - defp get_prefix(%{"TotalMemory" => _}), do: "headend_server_stats" - defp get_prefix(%{"Location ID" => _}), do: "live_pa_connectivity" - defp get_prefix(%{"node_type" => _}), do: "device_uptime" - defp get_prefix(_), do: "unknown" - - defp get_live_pa_connectivity_fields(%{ + defp get_log_fields(%{ "Location ID" => station_code, "IP Address" => ip_address, "Status" => status }) do - [ - node_type: "live_pa_connectivity", - station_code: station_code, - ip_address: ip_address, - status: String.replace(status, " ", "_") - ] + {:ok, + [ + tag: "live_pa_connectivity", + station_code: station_code, + ip_address: ip_address, + status: String.replace(status, " ", "_") + ]} end - defp get_headend_server_fields(%{ + defp get_log_fields(%{ "IPAddress" => ip_address, "Uptime" => uptime, "TotalMemory" => total_memory, @@ -64,39 +44,43 @@ defmodule Monitoring.Uptime do String.split(uptime, ",") |> Enum.map(&(String.trim_leading(&1) |> String.split() |> List.first())) - [ - node_type: "headend_server", - ip_address: ip_address, - uptime_days: days, - uptime_hours: hours, - uptime_minutes: minutes, - total_memory: total_memory, - total_c_space: total_c_space, - total_d_space: total_d_space, - available_memory: available_memory, - available_c_space: available_c_space, - available_d_space: available_d_space - ] + {:ok, + [ + tag: "headend_server_stats", + ip_address: ip_address, + uptime_days: days, + uptime_hours: hours, + uptime_minutes: minutes, + total_memory: total_memory, + total_c_space: total_c_space, + total_d_space: total_d_space, + available_memory: available_memory, + available_c_space: available_c_space, + available_d_space: available_d_space + ]} end - defp get_live_pa_app_fields(%{ + defp get_log_fields(%{ "sw_component" => sw_component, "is_online" => is_online, "status" => status }) do - [ - node_type: String.replace(sw_component, " ", "_"), - is_online: is_online, - status: status - ] + {:ok, + [ + tag: "software_uptime", + node_type: String.replace(sw_component, " ", "_"), + is_online: is_online, + status: status + ]} end - defp get_device_status_fields(%{"description" => description, "is_online" => is_online} = node) do + defp get_log_fields(%{"description" => description, "is_online" => is_online} = node) do node_type = get_node_type(node) [line, station, device_id, maybe_sign_zone] = String.split(description, ":") log_fields = [ + tag: "device_uptime", node_type: node_type, line: line, station: String.replace(station, " ", "_"), @@ -104,11 +88,15 @@ defmodule Monitoring.Uptime do is_online: is_online ] - if node_type == "sign", - do: log_fields ++ [sign_zone: maybe_sign_zone], - else: log_fields + {:ok, + if(node_type == "sign", + do: log_fields ++ [sign_zone: maybe_sign_zone], + else: log_fields + )} end + defp get_log_fields(_), do: :error + defp get_node_type(%{"node_type" => node_type}) do case node_type do "SGN" -> @@ -134,22 +122,13 @@ defmodule Monitoring.Uptime do end end - def log(_, _, []) do - nil - end - - def log(prefix, date_time, fields) do - base = [ - "#{prefix}: ", - "date_time=", - DateTime.to_string(date_time) - ] - + defp log(fields) do formatted_fields = - Enum.flat_map(fields, fn {k, v} -> - [" #{Atom.to_string(k)}=", v] + Enum.map(fields, fn {k, v} -> + " #{k}=#{v}" end) + |> Enum.join() - Logger.info(base ++ formatted_fields) + Logger.info(formatted_fields) end end diff --git a/test/monitoring/uptime_test.exs b/test/monitoring/uptime_test.exs index 0afb414d..978d0e74 100644 --- a/test/monitoring/uptime_test.exs +++ b/test/monitoring/uptime_test.exs @@ -16,6 +16,8 @@ defmodule Monitoring.UptimeTest do Monitoring.Uptime.monitor_nodes(nodes, 1_234_567_890) end) + IO.inspect(log) + assert log =~ "node_type=scu line=line station=station device_id=UNITTESTSCU001 is_online=true" end @@ -38,20 +40,22 @@ defmodule Monitoring.UptimeTest do "node_type=sign line=line station=station device_id=UNITTESTSIGN001 is_online=true sign_zone=C" end - test "Test unspecified node type" do + test "Logs unknown node type" do nodes = [ %{ "description" => "line:station:UNITTESTSIGN001:C", - "is_online" => "true" + "is_online" => "true", + "node_type" => "unknown_node" } ] log = - capture_log([level: :warn], fn -> + capture_log([level: :info], fn -> Monitoring.Uptime.monitor_nodes(nodes, 1_234_567_890) end) - assert log =~ "unknown_node" + assert log =~ + " date_time=2009-02-13 23:31:30Z tag=device_uptime node_type=unknown line=line station=station device_id=UNITTESTSIGN001 is_online=true" end test "Processes live PA connectivity" do @@ -69,7 +73,7 @@ defmodule Monitoring.UptimeTest do end) assert log =~ - "node_type=live_pa_connectivity station_code=TEST ip_address=0.0.0.0 status=Timed_out" + "date_time=2009-02-13 23:31:30Z tag=live_pa_connectivity station_code=TEST ip_address=0.0.0.0 status=Timed_out" end test "Processes live PA application status" do @@ -109,6 +113,6 @@ defmodule Monitoring.UptimeTest do end) assert log =~ - "node_type=headend_server ip_address=0.0.0.0 uptime_days=864 uptime_hours=21 uptime_minutes=55 total_memory=127.69 total_c_space=60 total_d_space=60 available_memory=71.27 available_c_space=29.78 available_d_space=29.78" + "date_time=2009-02-13 23:31:30Z tag=headend_server_stats ip_address=0.0.0.0 uptime_days=864 uptime_hours=21 uptime_minutes=55 total_memory=127.69 total_c_space=60 total_d_space=60 available_memory=71.27 available_c_space=29.78 available_d_space=29.78" end end