diff --git a/instrumentation/opentelemetry_ecto/lib/opentelemetry_ecto.ex b/instrumentation/opentelemetry_ecto/lib/opentelemetry_ecto.ex index d745ad4..44949b7 100644 --- a/instrumentation/opentelemetry_ecto/lib/opentelemetry_ecto.ex +++ b/instrumentation/opentelemetry_ecto/lib/opentelemetry_ecto.ex @@ -36,6 +36,9 @@ defmodule OpentelemetryEcto do defaults to the concatenation of the event name with periods, e.g. `"blog.repo.query"`. This will always be followed with a colon and the source (the table name for SQL adapters). + * `:additional_attributes` - additional attributes to include in the span. If there + are conflits with default provided attributes, the ones provided with + this config will have precedence. """ def setup(event_prefix, config \\ []) do event = event_prefix ++ [:query] @@ -74,6 +77,7 @@ defmodule OpentelemetryEcto do end <> if source != nil, do: ":#{source}", else: "" time_unit = Keyword.get(config, :time_unit, :microsecond) + additional_attributes = Keyword.get(config, :additional_attributes, %{}) db_type = case type do @@ -101,6 +105,8 @@ defmodule OpentelemetryEcto do _, acc -> acc end) + |> Map.merge(base_attributes) + |> Map.merge(additional_attributes) parent_context = OpentelemetryProcessPropagator.fetch_parent_ctx(1, :"$callers") @@ -111,7 +117,7 @@ defmodule OpentelemetryEcto do s = OpenTelemetry.Tracer.start_span(span_name, %{ start_time: start_time, - attributes: Map.merge(attributes, base_attributes), + attributes: attributes, kind: :client }) diff --git a/instrumentation/opentelemetry_ecto/test/opentelemetry_ecto_test.exs b/instrumentation/opentelemetry_ecto/test/opentelemetry_ecto_test.exs index 2068bcd..9f6fdec 100644 --- a/instrumentation/opentelemetry_ecto/test/opentelemetry_ecto_test.exs +++ b/instrumentation/opentelemetry_ecto/test/opentelemetry_ecto_test.exs @@ -61,6 +61,14 @@ defmodule OpentelemetryEctoTest do } = :otel_attributes.map(attributes) end + test "include additionaL_attributes" do + attach_handler(additional_attributes: %{"config.attribute": "special value", "db.instance": "my_instance"}) + Repo.all(User) + + assert_receive {:span, span(attributes: attributes)} + assert %{"config.attribute": "special value", "db.instance": "my_instance"} = :otel_attributes.map(attributes) + end + test "changes the time unit" do attach_handler(time_unit: :millisecond)