Skip to content

Commit

Permalink
Appeasing the dialyzer
Browse files Browse the repository at this point in the history
* [Refactored] Bonny.Telemetry.emit/N to remove unused signatures and
correct per dialyzer errors
  • Loading branch information
coryodaniel committed Mar 12, 2019
1 parent d914916 commit fd892b6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 42 deletions.
2 changes: 0 additions & 2 deletions lib/bonny/application.ex
Expand Up @@ -4,8 +4,6 @@ defmodule Bonny.Application do
use Application

def start(_type, _args) do
Bonny.Telemetry.attach()

children =
Bonny.Config.controllers()
|> Enum.map(fn controller ->
Expand Down
52 changes: 23 additions & 29 deletions lib/bonny/telemetry.ex
Expand Up @@ -2,6 +2,7 @@ defmodule Bonny.Telemetry do
@moduledoc """
List of telemetry events.
"""

@spec events() :: list(list(atom))
def events() do
[
Expand All @@ -12,27 +13,17 @@ defmodule Bonny.Telemetry do
end

@doc """
Emits telemetry.
Wrapper around `:telemetry.execute/3`.
**Note:** This method switches the order of `measurements` and `metadata`
Prepends `:bonnny` to all atom lists.
"""
@spec emit(atom) :: no_return
@spec emit(atom, fun) :: no_return
@spec emit(atom, map) :: no_return
@spec emit(atom, map, map) :: no_return
@spec emit(atom, map, fun) :: no_return
@spec emit(list(atom), map, fun) :: no_return
def emit(name), do: emit(name, %{}, %{})
def emit(name, metadata = %{}), do: emit(name, %{}, metadata)
def emit(name, func) when is_function(func), do: emit(name, %{}, func)

def emit(name, metadata = %{}, func) when is_function(func),
do: emit(name, metadata, %{duration: measure(func)})
@spec emit(list(atom)) :: :ok
@spec emit(list(atom), map) :: :ok
@spec emit(list(atom), map, map) :: :ok
def emit(names), do: emit(names, %{}, %{})
def emit(names, measurements = %{}), do: emit(names, measurements, %{})

def emit(name, metadata = %{}, measurements = %{}) when is_atom(name),
do: emit([name], measurements, metadata)

def emit(names, metadata = %{}, measurements = %{}) when is_list(names),
def emit(names, measurements = %{}, metadata = %{}),
do: :telemetry.execute([:bonny | names], measurements, metadata)

@doc """
Expand All @@ -53,20 +44,12 @@ defmodule Bonny.Telemetry do

{seconds, retval}
end

@spec attach() :: no_return
@doc false
def attach() do
:telemetry.attach_many(
"debug-logger",
events(),
&Bonny.Telemetry.DebugLogger.handle_event/4,
nil
)
end
end

defmodule Bonny.Telemetry.DebugLogger do
@moduledoc """
A telemetry logger for debugging.
"""
require Logger

def handle_event(event, measurements, metadata, _config) do
Expand All @@ -75,4 +58,15 @@ defmodule Bonny.Telemetry.DebugLogger do
"[#{event_name}] #{inspect(measurements)} #{inspect(metadata)}"
end)
end

@doc false
@spec attach() :: no_return
def attach() do
:telemetry.attach_many(
"debug-logger",
Bonny.Telemetry.events(),
&Bonny.Telemetry.DebugLogger.handle_event/4,
nil
)
end
end
6 changes: 1 addition & 5 deletions lib/bonny/watcher.ex
Expand Up @@ -14,12 +14,8 @@ defmodule Bonny.Watcher do
@impl GenServer
def init(controller) do
state = Impl.new(controller)
schedule_watcher()
{:ok, state}
end

defp schedule_watcher() do
Process.send_after(self(), :watch, 5000)
{:ok, state}
end

def handle_info(:watch, state) do
Expand Down
16 changes: 10 additions & 6 deletions lib/bonny/watcher/impl.ex
Expand Up @@ -16,9 +16,13 @@ defmodule Bonny.Watcher.Impl do

defstruct [:spec, :controller, :resource_version]

@doc """
Initialize a `Bonny.Watcher` state
"""
@spec new(module()) :: Impl.t()
def new(controller) do
spec = apply(controller, :crd_spec, [])
Bonny.Telemetry.emit([:watcher, :initialized], telemetry_metadata(spec))
Bonny.Telemetry.emit([:watcher, :initialized], %{}, telemetry_metadata(spec))

%__MODULE__{
controller: controller,
Expand All @@ -32,9 +36,9 @@ defmodule Bonny.Watcher.Impl do
Streams HTTPoison response to `Bonny.Watcher`
"""
@spec watch_for_changes(Impl.t(), pid()) :: nil
@spec watch_for_changes(Impl.t(), pid()) :: no_return
def watch_for_changes(state = %Impl{}, watcher) do
Bonny.Telemetry.emit([:watcher, :started], telemetry_metadata(state.spec))
Bonny.Telemetry.emit([:watcher, :started], %{}, telemetry_metadata(state.spec))

operation = list_operation(state)
rv = get_resource_version(state)
Expand Down Expand Up @@ -97,7 +101,7 @@ defmodule Bonny.Watcher.Impl do
def dispatch(%{"type" => "DELETED", "object" => object}, controller),
do: do_dispatch(controller, :delete, object)

@spec do_dispatch(atom, atom, map) :: nil
@spec do_dispatch(atom, atom, map) :: no_return
defp do_dispatch(controller, event, object) do
Task.start(fn ->
{time, result} = Bonny.Telemetry.measure(fn -> apply(controller, event, [object]) end)
Expand All @@ -118,7 +122,7 @@ defmodule Bonny.Watcher.Impl do
measurements = %{duration: time}
metadata = telemetry_metadata(controller.crd_spec, %{event: event, success: was_successful})

Bonny.Telemetry.emit([:watcher, :dispatched], metadata, measurements)
Bonny.Telemetry.emit([:watcher, :dispatched], measurements, metadata)
end)
end

Expand Down Expand Up @@ -157,7 +161,7 @@ defmodule Bonny.Watcher.Impl do

@doc false
@spec telemetry_metadata(Bonny.CRD.t(), map | nil) :: map
def telemetry_metadata(spec = %Bonny.CRD{}, extra \\ %{}) do
defp telemetry_metadata(spec = %Bonny.CRD{}, extra \\ %{}) do
base = %{
api_version: Bonny.CRD.api_version(spec),
kind: Bonny.CRD.kind(spec)
Expand Down
4 changes: 4 additions & 0 deletions test/support/k8s_mock_client.ex
@@ -1,4 +1,8 @@
defmodule Bonny.K8sMockClient do
@moduledoc """
Mock `K8s.Client`
"""

def list(api_version, kind, path_params) do
K8s.Operation.build(:list, api_version, kind, path_params)
end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
@@ -1 +1,2 @@
Bonny.Telemetry.DebugLogger.attach()
ExUnit.start()

0 comments on commit fd892b6

Please sign in to comment.