Skip to content

Commit 203b2df

Browse files
committed
Return full URL and status info in Phoenix transactions
1 parent 8cbad35 commit 203b2df

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

lib/sentry/opentelemetry/span_processor.ex

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
7979
trace: build_trace_context(root_span)
8080
},
8181
request: %{
82-
url: attributes["http.target"],
82+
url: url_from_attributes(attributes),
8383
method: attributes["http.method"],
8484
headers: %{
8585
"User-Agent" => attributes["http.user_agent"]
@@ -142,6 +142,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
142142
parent_span_id: nil,
143143
op: "http.server",
144144
origin: root_span.origin,
145+
status: status_from_attributes(attributes),
145146
data: %{
146147
"http.response.status_code" => attributes["http.status_code"]
147148
}
@@ -176,10 +177,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
176177
defp build_span(
177178
%SpanRecord{origin: "opentelemetry_phoenix", attributes: attributes} = span_record
178179
) do
179-
op = "#{attributes["phoenix.plug"]}##{attributes["phoenix.action"]}"
180-
181180
%Span{
182-
op: op,
181+
op: "#{attributes["phoenix.plug"]}##{attributes["phoenix.action"]}",
183182
start_timestamp: span_record.start_time,
184183
timestamp: span_record.end_time,
185184
trace_id: span_record.trace_id,
@@ -238,4 +237,38 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
238237
parent_span_id: span_record.parent_span_id
239238
}
240239
end
240+
241+
defp url_from_attributes(attributes) do
242+
URI.to_string(%URI{
243+
scheme: attributes["http.scheme"],
244+
host: attributes["net.host.name"],
245+
port: attributes["net.host.port"],
246+
path: attributes["http.target"]
247+
})
248+
end
249+
250+
defp status_from_attributes(%{"http.status_code" => status_code}) do
251+
cond do
252+
status_code in 200..299 ->
253+
"ok"
254+
255+
status_code in [400, 401, 403, 404, 409, 429, 499, 500, 501, 503, 504] ->
256+
%{
257+
400 => "invalid_argument",
258+
401 => "unauthenticated",
259+
403 => "permission_denied",
260+
404 => "not_found",
261+
409 => "already_exists",
262+
429 => "resource_exhausted",
263+
499 => "cancelled",
264+
500 => "internal_error",
265+
501 => "unimplemented",
266+
503 => "unavailable",
267+
504 => "deadline_exceeded"
268+
}[status_code]
269+
270+
true ->
271+
"unknown_error"
272+
end
273+
end
241274
end

lib/sentry/transaction.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Sentry.Transaction do
99
:timestamp,
1010
:transaction,
1111
:transaction_info,
12+
:status,
1213
:contexts,
1314
:request,
1415
:measurements,

test_integrations/phoenix_app/lib/phoenix_app/application.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule PhoenixApp.Application do
1313
config: %{metadata: [:file, :line]}
1414
})
1515

16-
OpentelemetryBandit.setup()
16+
# OpentelemetryBandit.setup()
1717
OpentelemetryPhoenix.setup()
1818
OpentelemetryEcto.setup([:phoenix_app, :repo], db_statement: :enabled)
1919

test_integrations/phoenix_app/test/phoenix_app_web/controllers/transaction_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
2525
assert trace.origin == "opentelemetry_phoenix"
2626
assert trace.op == "http.server"
2727
assert trace.data == %{"http.response.status_code" => 200}
28+
assert trace.status == "ok"
2829

2930
assert transaction.request.env == %{"SERVER_NAME" => "www.example.com", "SERVER_PORT" => 80}
30-
assert transaction.request.url == "/transaction"
31+
assert transaction.request.url == "http://www.example.com/transaction"
3132
assert transaction.request.method == "GET"
3233

3334
assert [span] = transaction.spans
@@ -53,9 +54,10 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
5354
assert trace.origin == "opentelemetry_phoenix"
5455
assert trace.op == "http.server"
5556
assert trace.data == %{"http.response.status_code" => 200}
57+
assert trace.status == "ok"
5658

5759
assert transaction.request.env == %{"SERVER_NAME" => "www.example.com", "SERVER_PORT" => 80}
58-
assert transaction.request.url == "/users"
60+
assert transaction.request.url == "http://www.example.com/users"
5961
assert transaction.request.method == "GET"
6062

6163
assert [span] = transaction.spans

0 commit comments

Comments
 (0)