Skip to content

Instantly share code, notes, and snippets.

@nesizer
Forked from joshnuss/slow_query_handler.ex
Created January 16, 2022 20:37
Show Gist options
  • Save nesizer/29473c42fd1ff633a1bb0363016df78a to your computer and use it in GitHub Desktop.
Save nesizer/29473c42fd1ff633a1bb0363016df78a 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