Last active
July 9, 2023 09:26
-
-
Save joshnuss/28668c3a56eb6720644421c939488527 to your computer and use it in GitHub Desktop.
Output slow Ecto queries to logs
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
defmodule MyApp.Telemetry do | |
require Logger | |
def handle_event([:my_app, :repo, :query], measurements, metadata, _config) do | |
milliseconds = System.convert_time_unit(measurements.total_time, :native, :millisecond) | |
# did the query take longer than 100ms? | |
if milliseconds > 100 do | |
# log it as a warning | |
Logger.warn("SLOW QUERY: ms: #{milliseconds}, query: #{metadata.query}") | |
end | |
end | |
end | |
# in application.ex | |
:ok = :telemetry.attach("slow-query-handler", [:my_app, :repo, :query], &MyApp.Telemetry.handle_event/4, %{}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment