Attach telemetry to multiple Ecto applications started as umbrella

I have multiple ecto applications as umbrella.
I want to attach prometheus metrics to all of them. In each applicaition.ex from each umbrella I have.

  :ok =
      :telemetry.attach(
        "prometheus-ecto",
        [:restaurants_domain, :repo, :query],
        &Restaurants.RepoInstrumenter.handle_event/4,
        %{}
      )
:ok =
      :telemetry.attach(
        "prometheus-ecto",
        [:users_domain, :repo, :query],
        &Users.RepoInstrumenter.handle_event/4,
        %{}
      )

This raises error at runtime though at the second attach:

   ** (MatchError) no match of right hand side value: {:error, :already_exists}

How can I attach telemetry to multiple ecto repos and delegate the handle event to each one?

You need to use unique handler_id, but see that it do not need to be string, it is defined as term(), so if you want to do it programatically you can do:

for domain <- ~w[restaurants_domain users_domain]a do
  :ok = :telemetry.attach({:ecto, domain}, [domain, :repo, :query], handler, %{})
end
2 Likes