Skip to content

Commit d42f028

Browse files
committed
Use code attributes for plug/action
1 parent a13dd6d commit d42f028

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

instrumentation/opentelemetry_phoenix/lib/opentelemetry_phoenix.ex

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ defmodule OpentelemetryPhoenix do
5353
end
5454
5555
"""
56-
alias OpenTelemetry.SemConv.Incubating.URLAttributes
56+
alias OpenTelemetry.SemConv.Incubating.{CodeAttributes, URLAttributes}
5757

5858
alias OpenTelemetry.Tracer
5959

@@ -148,8 +148,8 @@ defmodule OpentelemetryPhoenix do
148148
@doc false
149149
def handle_router_dispatch_start(_event, _measurements, meta, _config) do
150150
attributes = %{
151-
:"phoenix.plug" => meta.plug,
152-
:"phoenix.action" => meta.plug_opts,
151+
unquote(CodeAttributes.code_namespace()) => meta.plug,
152+
unquote(CodeAttributes.code_function()) => meta.plug_opts,
153153
URLAttributes.url_template() => meta.route
154154
}
155155

@@ -167,7 +167,7 @@ defmodule OpentelemetryPhoenix do
167167
@tracer_id,
168168
"#{inspect(live_view)}.mount",
169169
meta,
170-
live_view_start_opts(meta)
170+
live_view_start_opts(meta, :mount)
171171
)
172172
end
173173

@@ -181,7 +181,7 @@ defmodule OpentelemetryPhoenix do
181181
@tracer_id,
182182
"#{inspect(live_view)}.handle_params",
183183
meta,
184-
live_view_start_opts(meta)
184+
live_view_start_opts(meta, :handle_params)
185185
)
186186
end
187187

@@ -195,7 +195,7 @@ defmodule OpentelemetryPhoenix do
195195
@tracer_id,
196196
"#{inspect(live_view)}.handle_event##{event}",
197197
meta,
198-
live_view_start_opts(meta)
198+
live_view_start_opts(meta, :handle_event)
199199
)
200200
end
201201

@@ -223,21 +223,28 @@ defmodule OpentelemetryPhoenix do
223223
OpentelemetryTelemetry.end_telemetry_span(@tracer_id, meta)
224224
end
225225

226-
defp live_view_start_opts(meta) do
226+
defp live_view_start_opts(meta, function) do
227227
%{
228228
kind: :server,
229-
attributes: url_attributes(meta)
229+
attributes: url_attributes(meta) ++ code_attributes(meta.socket.view, function)
230230
}
231231
end
232232

233233
defp url_attributes(%{uri: uri, socket: %{router: router}}) do
234234
uri = URI.parse(uri)
235235

236236
case Phoenix.Router.route_info(router, "GET", uri.path, uri.host) do
237-
:error -> %{}
238-
route_info -> %{URLAttributes.url_template() => route_info.route}
237+
:error -> []
238+
route_info -> [{URLAttributes.url_template(), route_info.route}]
239239
end
240240
end
241241

242242
defp url_attributes(_meta), do: %{}
243+
244+
defp code_attributes(module, function) do
245+
[
246+
{unquote(CodeAttributes.code_namespace()), module},
247+
{unquote(CodeAttributes.code_function()), function}
248+
]
249+
end
243250
end

instrumentation/opentelemetry_phoenix/test/integration_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ if otp_vsn >= 27 do
131131
alias OpenTelemetry.SemConv.NetworkAttributes
132132
alias OpenTelemetry.SemConv.ServerAttributes
133133
alias OpenTelemetry.SemConv.UserAgentAttributes
134+
alias OpenTelemetry.SemConv.Incubating.CodeAttributes
134135
alias OpenTelemetry.SemConv.Incubating.HTTPAttributes
135136
alias OpenTelemetry.SemConv.Incubating.URLAttributes
136137

@@ -239,8 +240,8 @@ if otp_vsn >= 27 do
239240
{URLAttributes.url_query(), "a=1&b=abc"},
240241
{URLAttributes.url_scheme(), :http},
241242
{URLAttributes.url_template(), "/users/:user_id"},
242-
{:"phoenix.action", :user},
243-
{:"phoenix.plug", OpentelemetryPhoenix.Integration.TracingTest.TestController}
243+
{CodeAttributes.code_function(), :user},
244+
{CodeAttributes.code_namespace(), OpentelemetryPhoenix.Integration.TracingTest.TestController}
244245
]
245246

246247
for {attr, val} <- expected_attrs do

instrumentation/opentelemetry_phoenix/test/opentelemetry_phoenix_test.exs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ defmodule OpentelemetryPhoenixTest do
122122
attributes: attributes
123123
)}
124124

125-
assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"}
125+
assert :otel_attributes.map(attributes) == %{
126+
:"url.template" => "/resources/:resource_id",
127+
:"code.function" => :mount,
128+
:"code.namespace" => NnnnnWeb.ResourceLive.Show
129+
}
126130
end
127131

128132
test "records spans for Phoenix LiveView handle_params" do
@@ -146,7 +150,11 @@ defmodule OpentelemetryPhoenixTest do
146150
attributes: attributes
147151
)}
148152

149-
assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"}
153+
assert :otel_attributes.map(attributes) == %{
154+
:"url.template" => "/resources/:resource_id",
155+
:"code.function" => :handle_params,
156+
:"code.namespace" => NnnnnWeb.ResourceLive.Show
157+
}
150158
end
151159

152160
test "records spans for Phoenix LiveView handle_event" do
@@ -170,7 +178,11 @@ defmodule OpentelemetryPhoenixTest do
170178
attributes: attributes
171179
)}
172180

173-
assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"}
181+
assert :otel_attributes.map(attributes) == %{
182+
:"url.template" => "/resources/:resource_id",
183+
:"code.function" => :handle_event,
184+
:"code.namespace" => NnnnnWeb.ResourceLive.Show
185+
}
174186
end
175187

176188
test "handles exception during Phoenix LiveView handle_params" do
@@ -206,7 +218,11 @@ defmodule OpentelemetryPhoenixTest do
206218
attributes: attributes
207219
)}
208220

209-
assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"}
221+
assert :otel_attributes.map(attributes) == %{
222+
:"url.template" => "/resources/:resource_id",
223+
:"code.function" => :mount,
224+
:"code.namespace" => NnnnnWeb.ResourceLive.Show
225+
}
210226

211227
assert_receive {:span,
212228
span(
@@ -215,7 +231,11 @@ defmodule OpentelemetryPhoenixTest do
215231
events: events
216232
)}
217233

218-
assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"}
234+
assert :otel_attributes.map(attributes) == %{
235+
:"url.template" => "/resources/:resource_id",
236+
:"code.function" => :handle_params,
237+
:"code.namespace" => NnnnnWeb.ResourceLive.Show
238+
}
219239

220240
[
221241
event(
@@ -254,7 +274,11 @@ defmodule OpentelemetryPhoenixTest do
254274
events: events
255275
)}
256276

257-
assert :otel_attributes.map(attributes) == %{:"url.template" => "/resources/:resource_id"}
277+
assert :otel_attributes.map(attributes) == %{
278+
:"url.template" => "/resources/:resource_id",
279+
:"code.function" => :handle_event,
280+
:"code.namespace" => NnnnnWeb.ResourceLive.Show
281+
}
258282

259283
[
260284
event(

0 commit comments

Comments
 (0)