Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand All @@ -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
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down