diff --git a/lib/vintage_net_qmi/connectivity.ex b/lib/vintage_net_qmi/connectivity.ex index 78f063f..3c4d315 100644 --- a/lib/vintage_net_qmi/connectivity.ex +++ b/lib/vintage_net_qmi/connectivity.ex @@ -52,7 +52,8 @@ defmodule VintageNetQMI.Connectivity do # true for the modem to have internet access lan?: connection_status == :lan or connection_status == :internet, ip_address?: has_ipv4_address?(addresses), - serving_system?: true + serving_system?: true, + cached_status_timestamp: :erlang.monotonic_time() } |> update_connection_status() @@ -97,17 +98,45 @@ defmodule VintageNetQMI.Connectivity do def handle_info(_message, state), do: {:noreply, state} defp update_connection_status(%{lan?: true, serving_system?: true, ip_address?: true} = state) do - RouteManager.set_connection_status(state.ifname, :internet) + new_connection = :internet + + RouteManager.set_connection_status(state.ifname, new_connection) + state = execute_telemetry_events(new_connection, state) + %{state | cached_status: :internet} end defp update_connection_status(%{cached_status: :internet} = state) do - RouteManager.set_connection_status(state.ifname, :disconnected) - %{state | cached_status: :disconnected} + new_connection = :disconnected + + RouteManager.set_connection_status(state.ifname, new_connection) + state = execute_telemetry_events(new_connection, state) + + %{state | cached_status: new_connection} end defp update_connection_status(state), do: state + defp execute_telemetry_events(connection, %{cached_status: connection} = state), do: state + + defp execute_telemetry_events(new_connection, state) do + cached_status_timestamp = :erlang.monotonic_time() + + :telemetry.execute( + [:vintage_net_qmi, :connection, :end], + %{duration: :erlang.monotonic_time() - state.cached_status_timestamp}, + %{ifname: state.ifname, status: state.cached_status} + ) + + :telemetry.execute( + [:vintage_net_qmi, :connection], + %{}, + %{ifname: state.ifname, status: new_connection} + ) + + %{state | cached_status_timestamp: cached_status_timestamp} + end + defp has_ipv4_address?(addresses) do Enum.any?(addresses, &ipv4?/1) end diff --git a/mix.exs b/mix.exs index dd1cd4d..0e7fa9a 100644 --- a/mix.exs +++ b/mix.exs @@ -38,7 +38,8 @@ defmodule VintageNetQMI.MixProject do {:credo, "~> 1.5", only: [:dev, :test], runtime: false}, {:dialyxir, "~> 1.1.0", only: [:dev, :test], runtime: false}, {:ex_doc, "~> 0.23", only: :docs, runtime: false}, - {:excoveralls, "~> 0.14", only: :test, runtime: false} + {:excoveralls, "~> 0.14", only: :test, runtime: false}, + {:telemetry, "~> 0.4.3"} ] end diff --git a/mix.lock b/mix.lock index b2a153f..97f3e81 100644 --- a/mix.lock +++ b/mix.lock @@ -24,6 +24,7 @@ "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, "qmi": {:hex, :qmi, "0.6.0", "6326197fdb6312a945b0375bbc78283d4b2afcf9de5c1dba49c1e2abbe78dbb9", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "ff37c071cd749179920c1c78db36c607066a89ae2ecfe590adf1677bd3fc7821"}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, + "telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, "vintage_net": {:hex, :vintage_net, "0.10.5", "0441e5c76338ca071c97503390fcfc484c2f352b7573453e8da1255e65345c96", [:make, :mix], [{:beam_notify, "~> 0.2.0", [hex: :beam_notify, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:gen_state_machine, "~> 2.0.0 or ~> 2.1.0 or ~> 3.0.0", [hex: :gen_state_machine, repo: "hexpm", optional: false]}, {:muontrap, "~> 0.5.1 or ~> 0.6.0", [hex: :muontrap, repo: "hexpm", optional: false]}], "hexpm", "69b599dff8e018d562a7dbdb1c192b663aad6ae7ced5690b3c0a1337f9f1f65c"}, }