Skip to content

Commit a9e6fa0

Browse files
authored
opentelemetry_ecto - add possibility to setup additional attributes. (#94)
* Make possible to include additional attributes to the span through setup * Adjust code review changes
1 parent d5bae4d commit a9e6fa0

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

instrumentation/opentelemetry_ecto/lib/opentelemetry_ecto.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ defmodule OpentelemetryEcto do
3636
defaults to the concatenation of the event name with periods, e.g.
3737
`"blog.repo.query"`. This will always be followed with a colon and the
3838
source (the table name for SQL adapters).
39+
* `:additional_attributes` - additional attributes to include in the span. If there
40+
are conflits with default provided attributes, the ones provided with
41+
this config will have precedence.
3942
"""
4043
def setup(event_prefix, config \\ []) do
4144
event = event_prefix ++ [:query]
@@ -74,6 +77,7 @@ defmodule OpentelemetryEcto do
7477
end <> if source != nil, do: ":#{source}", else: ""
7578

7679
time_unit = Keyword.get(config, :time_unit, :microsecond)
80+
additional_attributes = Keyword.get(config, :additional_attributes, %{})
7781

7882
db_type =
7983
case type do
@@ -101,6 +105,8 @@ defmodule OpentelemetryEcto do
101105
_, acc ->
102106
acc
103107
end)
108+
|> Map.merge(base_attributes)
109+
|> Map.merge(additional_attributes)
104110

105111
parent_context = OpentelemetryProcessPropagator.fetch_parent_ctx(1, :"$callers")
106112

@@ -111,7 +117,7 @@ defmodule OpentelemetryEcto do
111117
s =
112118
OpenTelemetry.Tracer.start_span(span_name, %{
113119
start_time: start_time,
114-
attributes: Map.merge(attributes, base_attributes),
120+
attributes: attributes,
115121
kind: :client
116122
})
117123

instrumentation/opentelemetry_ecto/test/opentelemetry_ecto_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ defmodule OpentelemetryEctoTest do
6161
} = :otel_attributes.map(attributes)
6262
end
6363

64+
test "include additionaL_attributes" do
65+
attach_handler(additional_attributes: %{"config.attribute": "special value", "db.instance": "my_instance"})
66+
Repo.all(User)
67+
68+
assert_receive {:span, span(attributes: attributes)}
69+
assert %{"config.attribute": "special value", "db.instance": "my_instance"} = :otel_attributes.map(attributes)
70+
end
71+
6472
test "changes the time unit" do
6573
attach_handler(time_unit: :millisecond)
6674

0 commit comments

Comments
 (0)