-
-
Save benbarber/4351237caab4e9212e331958c4c9dd89 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