Skip to content

Commit 5be17c7

Browse files
committed
Make possible to include additional attributes to the span through setup
1 parent 6591b28 commit 5be17c7

File tree

2 files changed

+88
-15
lines changed

2 files changed

+88
-15
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+
* `: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+
config_attributes = Keyword.get(config, :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(config_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: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ defmodule OpentelemetryEctoTest do
6161
} = :otel_attributes.map(attributes)
6262
end
6363

64+
test "include config attributes" do
65+
attach_handler(
66+
attributes: %{"config.attribute": "special value", "db.instance": "my_instance_config"}
67+
)
68+
69+
Repo.all(User)
70+
71+
assert_receive {:span, span(attributes: attributes)}
72+
73+
assert %{"config.attribute": "special value", "db.instance": "my_instance_config"} =
74+
:otel_attributes.map(attributes)
75+
end
76+
6477
test "changes the time unit" do
6578
attach_handler(time_unit: :millisecond)
6679

@@ -111,7 +124,7 @@ defmodule OpentelemetryEctoTest do
111124
attach_handler()
112125

113126
try do
114-
Repo.all(from u in "users", select: u.non_existant_field)
127+
Repo.all(from(u in "users", select: u.non_existant_field))
115128
rescue
116129
_ -> :ok
117130
end
@@ -137,9 +150,24 @@ defmodule OpentelemetryEctoTest do
137150
end
138151

139152
assert_receive {:span, span(span_id: root_span_id, name: "parent span")}
140-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:users")}
141-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:posts")}
142-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:comments")}
153+
154+
assert_receive {:span,
155+
span(
156+
parent_span_id: ^root_span_id,
157+
name: "opentelemetry_ecto.test_repo.query:users"
158+
)}
159+
160+
assert_receive {:span,
161+
span(
162+
parent_span_id: ^root_span_id,
163+
name: "opentelemetry_ecto.test_repo.query:posts"
164+
)}
165+
166+
assert_receive {:span,
167+
span(
168+
parent_span_id: ^root_span_id,
169+
name: "opentelemetry_ecto.test_repo.query:comments"
170+
)}
143171
end
144172

145173
test "preloads in parallel are tied to the parent span" do
@@ -154,9 +182,24 @@ defmodule OpentelemetryEctoTest do
154182
end
155183

156184
assert_receive {:span, span(span_id: root_span_id, name: "parent span")}
157-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:users")}
158-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:posts")}
159-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:comments")}
185+
186+
assert_receive {:span,
187+
span(
188+
parent_span_id: ^root_span_id,
189+
name: "opentelemetry_ecto.test_repo.query:users"
190+
)}
191+
192+
assert_receive {:span,
193+
span(
194+
parent_span_id: ^root_span_id,
195+
name: "opentelemetry_ecto.test_repo.query:posts"
196+
)}
197+
198+
assert_receive {:span,
199+
span(
200+
parent_span_id: ^root_span_id,
201+
name: "opentelemetry_ecto.test_repo.query:comments"
202+
)}
160203
end
161204

162205
test "nested query preloads are tied to the parent span" do
@@ -167,21 +210,45 @@ defmodule OpentelemetryEctoTest do
167210
attach_handler()
168211

169212
Tracer.with_span "parent span" do
170-
users_query = from u in User, preload: [:posts, :comments]
171-
comments_query = from c in Comment, preload: [user: ^users_query]
213+
users_query = from(u in User, preload: [:posts, :comments])
214+
comments_query = from(c in Comment, preload: [user: ^users_query])
172215
Repo.all(Query.from(User, preload: [:posts, comments: ^comments_query]))
173216
end
174217

175218
assert_receive {:span, span(span_id: root_span_id, name: "parent span")}
176219
# root query
177-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:users")}
220+
assert_receive {:span,
221+
span(
222+
parent_span_id: ^root_span_id,
223+
name: "opentelemetry_ecto.test_repo.query:users"
224+
)}
225+
178226
# comments preload
179-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:comments")}
227+
assert_receive {:span,
228+
span(
229+
parent_span_id: ^root_span_id,
230+
name: "opentelemetry_ecto.test_repo.query:comments"
231+
)}
232+
180233
# users preload
181-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:users")}
234+
assert_receive {:span,
235+
span(
236+
parent_span_id: ^root_span_id,
237+
name: "opentelemetry_ecto.test_repo.query:users"
238+
)}
239+
182240
# preloads of user
183-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:posts")}
184-
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:comments")}
241+
assert_receive {:span,
242+
span(
243+
parent_span_id: ^root_span_id,
244+
name: "opentelemetry_ecto.test_repo.query:posts"
245+
)}
246+
247+
assert_receive {:span,
248+
span(
249+
parent_span_id: ^root_span_id,
250+
name: "opentelemetry_ecto.test_repo.query:comments"
251+
)}
185252
end
186253

187254
def attach_handler(config \\ []) do

0 commit comments

Comments
 (0)