Ecto

Integrate Timber with the Ecto library

The Timber Ecto integration requires installation of the base timber Hex package first.

Timber integrates with Ecto through the timber_ecto Hex package which automatically captures useful context and metadata, turning your Ecto logs into rich structured events.

Installation

When integrating Timber with Ecto for an umbrella application, the :timber_ecto library needs to be a dependency for every application that defines an Ecto Repo.

  1. Ensure that you have both :timber (version 3.1.0 or later) and :timber_ecto listed as dependencies in mix.exs:

    mix.exs
    def deps do
    [
    {:timber, "~> 3.1"},
    {:timber_ecto, "~> 2.0"}
    ]
    end
  2. Run mix deps.get to get the dependencies.

  3. Add a configuration line for every Ecto Repo. For example, if you have the application :my_app and the Ecto Repo MyApp.Repo, the configuration in config/config.exs would look like this:

    config/config.exs
    use Mix.Config
    ‚Äč
    config :my_app, MyApp.Repo,
    log: false
  4. Attach Timber's Telemetry event handler in your Application's start callback:

    lib/my_app/application.ex
    def start(_type, _args) do
    # ...
    :ok = :telemetry.attach(
    "timber-ecto-query-handler",
    [:my_app, :repo, :query],
    &Timber.Ecto.handle_event/4,
    []
    )
    # ...
    Supervisor.start_link(children, opts)
    end

For more information, see the documentation for the Timber.Ecto module.

Configuration

query_time_ms_threshold

Only log query that exceed a specified duration (in milliseconds):

lib/my_app/application.ex
:ok = Telemetry.attach(
"timber-ecto-query-handler",
[:my_app, :repo, :query],
Timber.Ecto,
:handle_event,
[query_time_ms_threshold: 2_000]
)

Events

sql_query_executed

When Ecto logs a query a sql_query_execute event will be logged:

{
// ...
"sql_query_executed": {
"query": "SELECT * FROM users",
"duration_ms": 54.2
}
// ...
}

Name

Type

Description

sql_query_executed.query

string

SQL query

sql_query_executed.duration_ms

float

Execution duration of the query