EctoQueryExplorer
Collect and analyze Ecto query telemetry. Find which code produces which queries, how often, and how long they take.
Warning
Experimental. Use at your own risk.
Installation
-
Add dependency:
{:ecto_query_explorer, "~> 0.4"} -
Add a SQLite repo:
defmodule MyApp.EctoQueryExplorerRepo do use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.SQLite3 end -
Configure:
# Enable stacktraces on your repo config :my_app, MyApp.Repo, stacktrace: true # Configure the SQLite repo config :my_app, MyApp.EctoQueryExplorerRepo, database: "/tmp/ecto-query-explorer.sqlite3" # Configure EctoQueryExplorer config :ecto_query_explorer, otp_app: :my_app, repo: MyApp.EctoQueryExplorerRepo, ets_table_name: :ecto_query_explorer_data, samples_to_keep: 5, source_ecto_repos: [MyApp.Repo] -
Add to supervision tree:
children = [ MyApp.EctoQueryExplorerRepo, EctoQueryExplorer ] -
Run migrations:
# In a migration file def up do EctoQueryExplorer.Migration0.up() EctoQueryExplorer.Migration1.up() end def down do EctoQueryExplorer.Migration1.down() EctoQueryExplorer.Migration0.down() end
Usage
Collected data is written to SQLite every 60 seconds and on shutdown. On
several connected nodes sharing one SQLite file, one node does the writing;
see the EctoQueryExplorer module docs.
# Write collected data to SQLite now
EctoQueryExplorer.Data.dump2sqlite()
# Find queries by pattern
EctoQueryExplorer.Queries.filter_by_query("SELECT%users%")
# Find queries by origin
EctoQueryExplorer.Queries.filter_by_mfa(MyApp.Accounts, :get_user, 1)
# Get top queries by execution count
EctoQueryExplorer.Queries.top_queries(10)
See module docs for more: EctoQueryExplorer, EctoQueryExplorer.Queries, EctoQueryExplorer.Data.
Development
Enter the shell, then use mix as usual:
nix develop
mix compile
mix dialyzer
mix docs
Format the Nix files:
nix fmt
Running tests
Run everything CI runs, which is the tests, the docs and dialyzer:
nix flake check
One test needs PostgreSQL and is left out of a plain mix test. On Linux the
check above starts its own. On macOS, and to run that test yourself,
PostgreSQL must be running on localhost:5432 with a postgres user:
mix test --include integration
Releasing
From a clean main that matches origin/main, with major, minor or patch:
nix run .#release -- minor
It does what you would by hand:
- Bump
VERSIONand rename the[Unreleased]section ofCHANGELOG.mdto[vX.Y.Z] - YYYY-MM-DD, with a new empty[Unreleased]above it. - Commit.
- Push to
main.
Once the tests pass, CI publishes the package and its docs to Hex and creates
the GitHub release. A release that failed before its tag was created can be
retried by running the CI workflow on main.
License
MIT. See LICENSE.