Skip to content

Instantly share code, notes, and snippets.

@benbarber
Forked from joshnuss/slow_query_handler.ex
Created July 9, 2023 09:26
Show Gist options
  • Save benbarber/4351237caab4e9212e331958c4c9dd89 to your computer and use it in GitHub Desktop.
Save benbarber/4351237caab4e9212e331958c4c9dd89 to your computer and use it in GitHub Desktop.
Output slow Ecto queries to logs
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