Created
January 22, 2019 17:50
-
-
Save take-five/f62ba41adfb5c81d12f6c090283427f1 to your computer and use it in GitHub Desktop.
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 Friends.Application do | |
# See https://hexdocs.pm/elixir/Application.html | |
# for more information on OTP Applications | |
@moduledoc false | |
use Application | |
def start(_type, _args) do | |
# List all child processes to be supervised | |
children = [ | |
Friends.Repo, | |
{Friends.Instrumentation, []} | |
# Starts a worker by calling: Friends.Worker.start_link(arg) | |
# {Friends.Worker, arg}, | |
] | |
# See https://hexdocs.pm/elixir/Supervisor.html | |
# for other strategies and supported options | |
opts = [strategy: :one_for_one, name: Friends.Supervisor] | |
Supervisor.start_link(children, opts) | |
end | |
end |
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 Friends.Instrumentation do | |
require Logger | |
@event_subscriptions [ | |
[:friends, :repo, :query] | |
] | |
@handler_id "instrumentation" | |
@threshold 10 | |
@doc false | |
def start_link do | |
:ok = :telemetry.attach_many(@handler_id, @event_subscriptions, &handle_event/4, []) | |
:ignore | |
end | |
def child_spec(_opts) do | |
%{ | |
id: __MODULE__, | |
start: {__MODULE__, :start_link, []}, | |
type: :worker, | |
restart: :permanent, | |
shutdown: 500 | |
} | |
end | |
defp handle_event([:friends, :repo, :query], total_time, metadata, _config) do | |
if to_milliseconds(total_time) >= @threshold do | |
%{query_time: query_time} = metadata | |
Logger.warn("query_time=#{to_milliseconds(query_time)}") | |
end | |
end | |
## Helpers | |
defp to_milliseconds(time_native) do | |
System.convert_time_unit(time_native, :native, :milliseconds) | |
end | |
end |
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
friends_repo=# select * from people; | |
id | first_name | last_name | age | |
----+------------+-----------+----- | |
1 | Alice | Cooper | 70 | |
2 | Bob | Marley | 10 | |
3 | Charlie | Sheen | 55 | |
(3 rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment