Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connection and disconnection telemetry events #423

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,21 @@ unnecessary, it can save a trip to the field or a full device reboot.

`VintageNet.info/1` shows the power management state for network interfaces that
are using this feature.

## Telemetry

VintageNet uses `:telemetry` library for instrumentation. The following events
are published by VintageNet with the following measurements and metadata:

All time units are in `:native` time. See `System.convert_time/3` to convert the
time into a different time unit.

* `[:vintage_net, :connection, :start]` - dispatched when `VintageNet`
determines the the interface is internet connected.
* Measurement: `%{system_time: system_time}`
* Metadata: `%{ifname: VintageNet.ifname()}`

* `[:vintage_net, :connection, :stop]` - dispatched when `VintageNet` determines
the interface has been disconnected.
* Measurement: `%{duration: duration}`
* Metadata: `%{ifname: VintageNet.ifname()}`
1 change: 1 addition & 0 deletions lib/vintage_net/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule VintageNet.Application do

children = [
{PropertyTable, properties: properties, name: VintageNet, tuple_events: true},
VintageNet.Telemetry,
{VintageNet.PredictableInterfaceName, hw_path_ifnames},
VintageNet.PowerManager.Supervisor,
{BEAMNotify,
Expand Down
63 changes: 63 additions & 0 deletions lib/vintage_net/telemetry.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
defmodule VintageNet.Telemetry do
@moduledoc false

# Telemetry server that provides the necessary state for us to determine
# the duration between an interface being internet connected and disconnected.

use GenServer

@doc """
Start the telemetry server
"""
@spec start_link(keyword()) :: GenServer.on_start()
def start_link(args) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end

@impl GenServer
def init(_args) do
VintageNet.subscribe(["interface", :_, "connection"])
{:ok, %{}}
end

@impl GenServer
def handle_info(
{VintageNet, ["interface", ifname, "connection"], _old, :internet,
%{new_timestamp: start_timestamp}},
state
) do
system_time = System.system_time()
state = Map.put(state, ifname, start_timestamp)

:telemetry.execute([:vintage_net, :connection, :start], %{system_time: system_time}, %{
ifname: ifname
})

{:noreply, state}
end

@impl GenServer
def handle_info(
{VintageNet, ["interface", ifname, "connection"], _old, :disconnected,
%{new_timestamp: disconnect_ts}},
state
) do
case Map.get(state, ifname) do
nil ->
{:noreply, state}

start_ts ->
duration = disconnect_ts - start_ts

:telemetry.execute([:vintage_net, :connection, :stop], %{duration: duration}, %{
ifname: ifname
})

{:noreply, Map.drop(state, [ifname])}
end
end

def handle_info(_msg, state) do
{:noreply, state}
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ defmodule VintageNet.MixProject do
{:gen_state_machine, "~> 2.0.0 or ~> 2.1.0 or ~> 3.0.0"},
{:muontrap, "~> 1.0 or ~> 0.5.1 or ~> 0.6.0"},
{:property_table, "~> 0.2.0"},
{:telemetry, "~> 1.1"},
# Build dependencies
{:credo, "~> 1.2", only: :test, runtime: false},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"property_table": {:hex, :property_table, "0.2.0", "d47549d9c75c4f8975ca126b48a21c879c76e73d7e7dd0cc65b7d1619b3a198f", [:mix], [], "hexpm", "00f89af46c1f45e572c8386744c773fbf506f731648538d350b9a96720021f0d"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"telemetry": {:hex, :telemetry, "1.1.0", "a589817034a27eab11144ad24d5c0f9fab1f58173274b1e9bae7074af9cbee51", [:rebar3], [], "hexpm", "b727b2a1f75614774cff2d7565b64d0dfa5bd52ba517f16543e6fc7efcc0df48"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
}