Skip to content

Commit

Permalink
Return full URL and status info in Phoenix transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Sep 25, 2024
1 parent 875b6e1 commit 870ad1f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
41 changes: 37 additions & 4 deletions lib/sentry/opentelemetry/span_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
trace: build_trace_context(root_span)
},
request: %{
url: attributes["http.target"],
url: url_from_attributes(attributes),
method: attributes["http.method"],
headers: %{
"User-Agent" => attributes["http.user_agent"]
Expand Down Expand Up @@ -142,6 +142,7 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
parent_span_id: nil,
op: "http.server",
origin: root_span.origin,
status: status_from_attributes(attributes),
data: %{
"http.response.status_code" => attributes["http.status_code"]
}
Expand Down Expand Up @@ -176,10 +177,8 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
defp build_span(
%SpanRecord{origin: "opentelemetry_phoenix", attributes: attributes} = span_record
) do
op = "#{attributes["phoenix.plug"]}##{attributes["phoenix.action"]}"

%Span{
op: op,
op: "#{attributes["phoenix.plug"]}##{attributes["phoenix.action"]}",
start_timestamp: span_record.start_time,
timestamp: span_record.end_time,
trace_id: span_record.trace_id,
Expand Down Expand Up @@ -238,4 +237,38 @@ defmodule Sentry.Opentelemetry.SpanProcessor do
parent_span_id: span_record.parent_span_id
}
end

defp url_from_attributes(attributes) do
URI.to_string(%URI{
scheme: attributes["http.scheme"],
host: attributes["net.host.name"],
port: attributes["net.host.port"],
path: attributes["http.target"]
})
end

defp status_from_attributes(%{"http.status_code" => status_code}) do
cond do
status_code in 200..299 ->
"ok"

status_code in [400, 401, 403, 404, 409, 429, 499, 500, 501, 503, 504] ->
%{
400 => "invalid_argument",
401 => "unauthenticated",
403 => "permission_denied",
404 => "not_found",
409 => "already_exists",
429 => "resource_exhausted",
499 => "cancelled",
500 => "internal_error",
501 => "unimplemented",
503 => "unavailable",
504 => "deadline_exceeded"
}[status_code]

true ->
"unknown_error"
end
end
end
1 change: 1 addition & 0 deletions lib/sentry/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Sentry.Transaction do
:timestamp,
:transaction,
:transaction_info,
:status,
:contexts,
:request,
:measurements,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule PhoenixApp.Application do
config: %{metadata: [:file, :line]}
})

OpentelemetryBandit.setup()
# OpentelemetryBandit.setup()
OpentelemetryPhoenix.setup()
OpentelemetryEcto.setup([:phoenix_app, :repo], db_statement: :enabled)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ defmodule Sentry.Integrations.Phoenix.TransactionTest do
assert trace.origin == "opentelemetry_phoenix"
assert trace.op == "http.server"
assert trace.data == %{"http.response.status_code" => 200}
assert trace.status == "ok"

assert transaction.request.env == %{"SERVER_NAME" => "www.example.com", "SERVER_PORT" => 80}
assert transaction.request.url == "/transaction"
assert transaction.request.url == "http://www.example.com/transaction"
assert transaction.request.method == "GET"

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

assert transaction.request.env == %{"SERVER_NAME" => "www.example.com", "SERVER_PORT" => 80}
assert transaction.request.url == "/users"
assert transaction.request.url == "http://www.example.com/users"
assert transaction.request.method == "GET"

assert [span] = transaction.spans
Expand Down

0 comments on commit 870ad1f

Please sign in to comment.