Ignore Oban-related traces in OpenTelemetry for Ecto

All requests are made with Oban.Repo, which wraps calls with custom options as seen here: oban/lib/oban/repo.ex at main · oban-bg/oban · GitHub. Notice there is an oban: true field that you can detect in functions like prepare_query/3, as well as oban_conf in telemetry options which could theoretically be used to ignore Oban emitted events.

Unfortunately, it looks like OpenTelemetryEcto doesn’t have a way to ignore certain events (yet).

You could add a small wrapper module to your application, like this:

defmodule MyApp.OpenTelemetryEcto do
  @moduledoc """
  A wrapper around OpenTelemetryEcto that filters out queries generated by Oban.
  """

  def setup(event_prefix, config \\ []) do
    event = event_prefix ++ [:query]

    :telemetry.attach({OpenTelemetryEcto, event}, event, &__MODULE__.handle_event/4, config)
  end

  @doc false
  def handle_event(_event, _measure, %{oban_config: _}, _config) do
    :ok
  end

  def handle_event(event, measure, meta, config) do
    OpenTelemetryEcto.handle_event(event, measure, meta, config)
  end
end