Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telemetry implementation for Ecto 3 #68

Merged
merged 3 commits into from Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/scout_apm/instruments/ecto_logger.ex
Expand Up @@ -22,7 +22,7 @@ defmodule ScoutApm.Instruments.EctoLogger do
entry
end

defp record(entry) do
def record(entry) do
case query_time(entry) do
{:ok, duration} ->
ScoutApm.TrackedRequest.track_layer(
Expand Down
21 changes: 21 additions & 0 deletions lib/scout_apm/instruments/ecto_telemetry.ex
@@ -0,0 +1,21 @@
defmodule ScoutApm.Instruments.EctoTelemetry do
@doc """
Attaches an event handler for Ecto queries.

Takes a fully namespaced Ecto.Repo module as the only argument. Example:

ScoutApm.Instruments.EctoTelemetry.attach(MyApp.Repo)
"""
def attach(repo_module) do
query_event = repo_module
|> Module.split()
|> Enum.map(& &1 |> Macro.underscore() |> String.to_atom())
|> Kernel.++([:query])

Telemetry.attach("scout-ecto-query-handler", query_event, ScoutApm.Instruments.EctoTelemetry, :handle_event, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that scout-ecto-query-handler string mean anything? Can we instrument multiple repos with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scout-ecto-query-handler does need to be unique. https://hexdocs.pm/telemetry/0.2.0/Telemetry.html#attach_many/5 is available, though we could also make the string ID unique by including the repo information in it which would allow multiple calls to EctoTelemetry.attach?

end

def handle_event([_app, _repo, :query], _value, metadata, _config) do
ScoutApm.Instruments.EctoLogger.record(metadata)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay code reuse.

end
end
1 change: 1 addition & 0 deletions mix.exs
Expand Up @@ -39,6 +39,7 @@ defmodule ScoutApm.Mixfile do
{:hackney, "~> 1.0"},

{:approximate_histogram, "~>0.1.1"},
{:telemetry, "~> 0.2.0"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this going to cause problems installing the agent on older versions of the Phoenix stack? For instance, we had to do the 0.0.0 thing for poison and just guard against functions not being present at runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added fd7b299 to handle that


#########################
# Dev & Testing Deps
Expand Down
5 changes: 3 additions & 2 deletions mix.lock
Expand Up @@ -6,8 +6,8 @@
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []},
"credo": {:hex, :credo, "0.8.10", "261862bb7363247762e1063713bb85df2bbd84af8d8610d1272cd9c1943bba63", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}]},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], []},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, optional: false]}]},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [repo: "hexpm", hex: :earmark, optional: false]}, {:makeup_elixir, "~> 0.7", [repo: "hexpm", hex: :makeup_elixir, optional: false]}], "hexpm"},
"hackney": {:hex, :hackney, "1.7.1", "e238c52c5df3c3b16ce613d3a51c7220a784d734879b1e231c9babd433ac1cb4", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []},
"makeup": {:hex, :makeup, "0.5.1", "966c5c2296da272d42f1de178c1d135e432662eca795d6dc12e5e8787514edf7", [:mix], [{:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, optional: false]}]},
Expand All @@ -26,4 +26,5 @@
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], []},
"slime": {:hex, :slime, "0.16.0", "4f9c677ca37b2817cd10422ecb42c524fe904d3630acf242b81dfe189900272a", [:mix], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []},
"telemetry": {:hex, :telemetry, "0.2.0", "5b40caa3efe4deb30fb12d7cd8ed4f556f6d6bd15c374c2366772161311ce377", [:mix], [], "hexpm"},
}