Skip to content

Commit

Permalink
fixup! Add telemetry support
Browse files Browse the repository at this point in the history
  • Loading branch information
kmakiela committed Feb 25, 2020
1 parent 872b642 commit 5828e3f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 47 deletions.
47 changes: 24 additions & 23 deletions lib/mongoose_push.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,7 @@ defmodule MongoosePush do
:timer.tc(module, :push, [notification, device_id, pool, opts])
end

:telemetry.execute(
[:mpush, :push],
%{time: time},
case push_result do
{:error, {type, reason}} ->
%{
result: :error,
type: type,
reason: reason
}

{:error, reason} ->
%{
result: :error,
reason: reason
}

other ->
%{
result: other
}
end
)
emit_telemetry_event(time, push_result)

push_result
|> Metrics.update(:spiral, [:push, service, mode])
Expand All @@ -132,4 +110,27 @@ defmodule MongoosePush do
Logger.warn(~s"Unable to complete push request due to #{inspect(reason)}")
return_value
end

defp emit_telemetry_event(time, {:error, {type, reason}}) do
:telemetry.execute(
[:mongoose_push, :push, :error, type],
%{time: time},
%{reason: reason}
)
end

defp emit_telemetry_event(time, {:error, reason}) do
:telemetry.execute(
[:mongoose_push, :push, :error],
%{time: time},
%{reason: reason}
)
end

defp emit_telemetry_event(time, :ok) do
:telemetry.execute(
[:mongoose_push, :push, :ok],
%{time: time}
)
end
end
22 changes: 12 additions & 10 deletions lib/mongoose_push/service/apns/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ defmodule MongoosePush.Service.APNS.State do
:apns_state = :ets.new(:apns_state, [:set, :public, :named_table])
true = :ets.insert(:apns_state, default_topics)

:telemetry.execute([:mpush, :apns_state, :init],
%{},
%{
default_topics: default_topics
}
:telemetry.execute(
[:mongoose_push, :apns_state, :init],
%{},
%{
default_topics: default_topics
}
)

{:ok, %{}}
Expand All @@ -35,11 +36,12 @@ defmodule MongoosePush.Service.APNS.State do
def get_default_topic(pool_name) do
[{_name, topic}] = :ets.lookup(:apns_state, pool_name)

:telemetry.execute([:mpush, :apns_state, :get_default_topic],
%{},
%{
topic: topic
}
:telemetry.execute(
[:mongoose_push, :apns_state, :get_default_topic],
%{},
%{
topic: topic
}
)

topic
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoose_push/service/apns/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ defmodule MongoosePush.Service.APNS.Supervisor do
]

:telemetry.execute(
[:mpush, :apns_supervisor, :init],
[:mongoose_push, :apns_supervisor, :init],
%{},
%{}
)
)

Supervisor.init(children, strategy: :one_for_one)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoose_push/service/fcm/pool/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ defmodule MongoosePush.Service.FCM.Pool.Supervisor do
]

:telemetry.execute(
[:mpush, :fcm_supervisor, :init],
[:mongoose_push, :fcm_supervisor, :init],
%{},
%{}
)
)

Supervisor.init(children, strategy: :one_for_one)
end
Expand Down
10 changes: 5 additions & 5 deletions lib/mongoose_push/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ defmodule MongoosePush.Telemetry do
:telemetry.attach_many(
"handler",
[
[:mpush, :push],
[:mpush, :apns_state, :init],
[:mpush, :apns_state, :get_default_topic],
[:mpush, :apns_supervisor, :init],
[:mpush, :fcm_supervisor, :init]
[:mongoose_push, :push],
[:mongoose_push, :apns_state, :init],
[:mongoose_push, :apns_state, :get_default_topic],
[:mongoose_push, :apns_supervisor, :init],
[:mongoose_push, :fcm_supervisor, :init]
],
&MongoosePush.Telemetry.handle_event/4,
nil
Expand Down
13 changes: 8 additions & 5 deletions lib/mongoose_push/telemetry/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@ defmodule MongoosePush.Telemetry.Handler do
{:ok, %{}}
end

def handle_cast({[:mpush, :push], _measurements, _metadata, _}, state) do
def handle_cast({[:mongoose_push, :push], _measurements, _metadata, _}, state) do
{:noreply, state}
end

def handle_cast({[:mpush, :apns_state, :init], _measurements, _metadata, _}, state) do
def handle_cast({[:mongoose_push, :apns_state, :init], _measurements, _metadata, _}, state) do
{:noreply, state}
end

def handle_cast({[:mpush, :apns_state, :get_default_topic], _measurements, _metadata, _}, state) do
def handle_cast(
{[:mongoose_push, :apns_state, :get_default_topic], _measurements, _metadata, _},
state
) do
{:noreply, state}
end

def handle_cast({[:mpush, :apns_supervisor, :init], _measurements, _metadata, _}, state) do
def handle_cast({[:mongoose_push, :apns_supervisor, :init], _measurements, _metadata, _}, state) do
{:noreply, state}
end

def handle_cast({[:mpush, :fcm_supervisor, :init], _measurements, _metadata, _}, state) do
def handle_cast({[:mongoose_push, :fcm_supervisor, :init], _measurements, _metadata, _}, state) do
{:noreply, state}
end
end

0 comments on commit 5828e3f

Please sign in to comment.