-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Log db performance (query / query time) (#2954)
* feat: Log db performance (query / query time) --------- Co-authored-by: Kayla Firestack <firestack@users.noreply.github.com>
- Loading branch information
1 parent
0869609
commit 9601599
Showing
5 changed files
with
81 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Skate.Telemetry do | ||
@moduledoc """ | ||
Telemetry listeners for Skate business logic. | ||
""" | ||
require Logger | ||
|
||
def setup_telemetry do | ||
:telemetry.attach( | ||
"skate", | ||
[:skate, :repo, :query], | ||
&Skate.Telemetry.repo_query_telemetry/4, | ||
%{} | ||
) | ||
end | ||
|
||
def repo_query_telemetry( | ||
[:skate, :repo, :query], | ||
%{decode_time: decode_time, query_time: query_time, total_time: total_time}, | ||
%{ | ||
source: source, | ||
result: {:ok, %{connection_id: connection_id, num_rows: num_rows}}, | ||
query: query | ||
}, | ||
_config | ||
) | ||
when source in ["detours"] do | ||
Logger.info(fn -> | ||
"Telemetry for db query, source=#{source} connection_id=#{connection_id} num_rows=#{num_rows} decode_time=#{decode_time} query_time=#{query_time} total_time=#{total_time} query='#{query}'" | ||
end) | ||
end | ||
|
||
def repo_query_telemetry(_event, _measurements, _metadata, _config), do: nil | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Skate.TelemetryTest do | ||
@moduledoc false | ||
use Skate.DataCase | ||
import Skate.Factory | ||
import ExUnit.CaptureLog, only: [capture_log: 2] | ||
import Test.Support.Helpers, only: [set_log_level: 1] | ||
|
||
alias Skate.Detours.Detours | ||
|
||
setup tags do | ||
if events = tags[:telemetry_listen] do | ||
ref = :telemetry_test.attach_event_handlers(self(), events) | ||
|
||
on_exit(fn -> :telemetry.detach(ref) end) | ||
|
||
%{telemetry_ref: ref} | ||
end | ||
end | ||
|
||
defp detour_fixture do | ||
insert(:detour) | ||
end | ||
|
||
@tag telemetry_listen: [[:skate, :repo, :query]] | ||
test "logs exception info" do | ||
set_log_level(:info) | ||
detour = detour_fixture() | ||
|
||
log = | ||
capture_log([level: :info], fn -> | ||
assert Skate.Repo.preload(Detours.list_detours(), :author) == [detour] | ||
end) | ||
|
||
assert_receive {[:skate, :repo, :query], _ref, | ||
%{decode_time: _, query_time: _, total_time: _}, | ||
%{ | ||
query: _, | ||
result: {:ok, %{connection_id: _, num_rows: _}}, | ||
source: "detours" | ||
}} | ||
|
||
assert log =~ "Telemetry for db query, source=detours" | ||
end | ||
end |