-
-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Prometheus metrics for active ws conn and topic sub totals
- Loading branch information
Showing
12 changed files
with
168 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule Realtime.Metrics.PromEx do | ||
use PromEx, otp_app: :realtime | ||
|
||
@impl true | ||
def plugins do | ||
[Realtime.Metrics.PromEx.Plugins.Realtime] | ||
end | ||
end |
29 changes: 29 additions & 0 deletions
29
server/lib/realtime/metrics/prom_ex_plugins/realtime_plugin.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule Realtime.Metrics.PromEx.Plugins.Realtime do | ||
use PromEx.Plugin | ||
|
||
@event_prefix [:realtime, :prom_ex, :plugin, :socket] | ||
|
||
@impl true | ||
def event_metrics(_opts) do | ||
Event.build( | ||
:realtime_socket_event_metrics, | ||
[ | ||
last_value( | ||
[:realtime, :active, :websocket, :connection, :total], | ||
event_name: socket_event(), | ||
measurement: :active_socket_total, | ||
description: "The total number of active websocket connections." | ||
), | ||
last_value( | ||
[:realtime, :active, :websocket, :topic, :total], | ||
event_name: channel_event(), | ||
measurement: :active_channel_total, | ||
description: "The total number of active topic subscriptions." | ||
) | ||
] | ||
) | ||
end | ||
|
||
def socket_event, do: @event_prefix ++ [:connection] | ||
def channel_event, do: @event_prefix ++ [:channel] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# This file draws from https://github.com/pushex-project/pushex | ||
# License: https://github.com/pushex-project/pushex/blob/master/LICENSE | ||
|
||
defmodule Realtime.Metrics.SocketMonitor do | ||
use GenServer | ||
alias Realtime.Metrics.PromEx.Plugins.Realtime | ||
|
||
def start_link(_) do | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
def init(_) do | ||
{:ok, %{transport_pids: %{}, channel_pids: %{}}} | ||
end | ||
|
||
def track_socket(socket = %Phoenix.Socket{}) do | ||
GenServer.call(__MODULE__, {:track_socket, socket}) | ||
end | ||
|
||
def track_channel(socket = %Phoenix.Socket{}) do | ||
GenServer.call(__MODULE__, {:track_channel, socket}) | ||
end | ||
|
||
## Callbacks | ||
|
||
def handle_call( | ||
{:track_socket, %Phoenix.Socket{transport_pid: transport_pid}}, | ||
_from, | ||
state = %{transport_pids: transport_pids} | ||
) do | ||
Process.monitor(transport_pid) | ||
|
||
new_transport_pids = | ||
Map.put(transport_pids, transport_pid, %{ | ||
online_at: unix_ms_now() | ||
}) | ||
|
||
execute_socket_telemetry(new_transport_pids) | ||
|
||
{:reply, :ok, %{state | transport_pids: new_transport_pids}} | ||
end | ||
|
||
def handle_call( | ||
{:track_channel, %Phoenix.Socket{channel_pid: channel_pid, topic: topic}}, | ||
_from, | ||
state = %{channel_pids: channel_pids} | ||
) do | ||
Process.monitor(channel_pid) | ||
|
||
new_channel_pids = | ||
Map.put(channel_pids, channel_pid, %{ | ||
channel: topic, | ||
online_at: unix_ms_now() | ||
}) | ||
|
||
execute_channel_telemetry(new_channel_pids) | ||
|
||
{:reply, :ok, %{state | channel_pids: new_channel_pids}} | ||
end | ||
|
||
def handle_info( | ||
{:DOWN, _ref, :process, pid, _reason}, | ||
state = %{transport_pids: transport_pids, channel_pids: channel_pids} | ||
) do | ||
new_transport_pids = Map.delete(transport_pids, pid) | ||
new_channel_pids = Map.delete(channel_pids, pid) | ||
|
||
execute_socket_telemetry(new_transport_pids) | ||
execute_channel_telemetry(new_channel_pids) | ||
|
||
{:noreply, %{state | transport_pids: new_transport_pids, channel_pids: new_channel_pids}} | ||
end | ||
|
||
defp execute_socket_telemetry(pids), | ||
do: :telemetry.execute(Realtime.socket_event(), %{active_socket_total: map_size(pids)}) | ||
|
||
defp execute_channel_telemetry(pids), | ||
do: :telemetry.execute(Realtime.channel_event(), %{active_channel_total: map_size(pids)}) | ||
|
||
defp unix_ms_now(), do: :erlang.system_time(:millisecond) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters