Skip to content

Commit

Permalink
fix: query count and network usage metrics (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored Aug 9, 2023
1 parent dde33aa commit 5037bc2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.25
0.2.26
9 changes: 6 additions & 3 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ defmodule Supavisor.ClientHandler do
ssl: false,
auth_secrets: nil,
proxy_type: nil,
def_mode_type: opts.def_mode_type
def_mode_type: opts.def_mode_type,
stats: %{}
}

:gen_statem.enter_loop(__MODULE__, [hibernate_after: 5_000], :exchange, data)
Expand Down Expand Up @@ -317,9 +318,11 @@ defmodule Supavisor.ClientHandler do

db_pid = handle_db_pid(data.mode, data.pool, data.db_pid)

Telem.network_usage(:client, data.sock, data.tenant, data.user_alias)
{_, stats} =
Telem.network_usage(:client, data.sock, data.tenant, data.user_alias, data.stats)

Telem.client_query_time(data.query_start, data.tenant, data.user_alias)
{:next_state, :idle, %{data | db_pid: db_pid}, reply}
{:next_state, :idle, %{data | db_pid: db_pid, stats: stats}, reply}
else
Logger.debug("Client is not ready")
{:keep_state_and_data, reply}
Expand Down
10 changes: 6 additions & 4 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ defmodule Supavisor.DbHandler do
parameter_status: %{},
nonce: nil,
messages: "",
server_proof: nil
server_proof: nil,
stats: %{}
}

{:ok, :connect, data, {:next_event, :internal, :connect}}
Expand Down Expand Up @@ -240,10 +241,11 @@ defmodule Supavisor.DbHandler do
:ok = Client.client_call(data.caller, bin, ready)

if ready do
Telem.network_usage(:db, data.sock, data.tenant, data.user_alias)
{_, stats} = Telem.network_usage(:db, data.sock, data.tenant, data.user_alias, data.stats)
{:keep_state, %{data | stats: stats}}
else
:keep_state_and_data
end

:keep_state_and_data
end

def handle_event({:call, {pid, _} = from}, {:db_call, bin}, :idle, %{sock: sock} = data) do
Expand Down
15 changes: 11 additions & 4 deletions lib/supavisor/monitoring/telem.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@ defmodule Supavisor.Monitoring.Telem do
atom(),
{:gen_tcp, :gen_tcp.socket()} | {:ssl, :ssl.socket()},
String.t(),
String.t()
) :: :ok
def network_usage(type, {mod, socket}, tenant, user_alias) do
String.t(),
map()
) :: {:ok | :error, map()}
def network_usage(type, {mod, socket}, tenant, user_alias, stats) do
mod = if mod == :ssl, do: :ssl, else: :inet

case mod.getstat(socket) do
{:ok, values} ->
values = Map.new(values)
diff = Map.merge(values, stats, fn _, v1, v2 -> v1 - v2 end)

:telemetry.execute(
[:supavisor, type, :network, :stat],
Map.new(values),
diff,
%{tenant: tenant, user_alias: user_alias}
)

{:ok, values}

{:error, reason} ->
Logger.error("Failed to get socket stats: #{inspect(reason)}")
{:error, stats}
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/supavisor/monitoring/tenant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ defmodule Supavisor.PromEx.Plugins.Tenant do
),
counter(
[:supavisor, :client, :queries, :count],
event_name: [:supavisor, :pool, :checkout, :stop],
event_name: [:supavisor, :client, :query, :stop],
description: "The total number of queries received by clients.",
tags: [:tenant, :user_alias]
)
Expand Down

0 comments on commit 5037bc2

Please sign in to comment.