Skip to content

Instantly share code, notes, and snippets.

@joshnuss
Last active July 9, 2023 09:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joshnuss/28668c3a56eb6720644421c939488527 to your computer and use it in GitHub Desktop.
Save joshnuss/28668c3a56eb6720644421c939488527 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