Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ defmodule OpentelemetryBandit do

name =
if request_method == HTTPAttributes.http_request_method_values().other,
do: :HTTP,
else: request_method
do: "HTTP",
else: conn.method

if public_endpoint?(conn, config) do
propagated_ctx =
Expand All @@ -264,7 +264,7 @@ defmodule OpentelemetryBandit do

# error with no conn
def handle_request_start(_meta, _config) do
Tracer.start_span(:HTTP, %{kind: :server})
Tracer.start_span("HTTP", %{kind: :server})
Comment thread
lunks marked this conversation as resolved.
Outdated
|> Tracer.set_current_span()
end

Expand Down Expand Up @@ -468,10 +468,29 @@ defmodule OpentelemetryBandit do
|> Tracer.set_attributes()
end

maybe_update_span_name(conn)
Comment thread
lunks marked this conversation as resolved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This couldn't be moved to the start event?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. We only have the URL path, not the route name. call_plug! sets it, but the span has already been started:
https://github.com/mtrudel/bandit/blob/1.10.3/lib/bandit/pipeline.ex#L38-L42

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsloughter I am gonna defer to you, I am not sure if changing midway the span name is a major concern or not

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you folks think there's a better approach to accomplish a similar result, let me know.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. This is fine.


Tracer.end_span()
Ctx.clear()
end

defp maybe_update_span_name(conn) do
case conn.private do
%{plug_route: {route, _fun}} ->
Comment thread
lunks marked this conversation as resolved.
method = sanitize_method(conn.method)
Tracer.update_name("#{method} #{route}")
Comment thread
lunks marked this conversation as resolved.

_ ->
:ok
end
end

defp sanitize_method(method) do
if parse_method(method) == HTTPAttributes.http_request_method_values().other,
do: "HTTP",
else: method
end

@doc false
def handle_request_exception(meta, config) do
Tracer.set_status(OpenTelemetry.status(:error, ""))
Expand Down
6 changes: 3 additions & 3 deletions instrumentation/opentelemetry_bandit/mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
%{
"acceptor_pool": {:hex, :acceptor_pool, "1.0.1", "d88c2e8a0be9216cf513fbcd3e5a4beb36bee3ff4168e85d6152c6f899359cdb", [], [], "hexpm", "f172f3d74513e8edd445c257d596fc84dbdd56d2c6fa287434269648ae5a421e"},
"acceptor_pool": {:hex, :acceptor_pool, "1.0.1", "d88c2e8a0be9216cf513fbcd3e5a4beb36bee3ff4168e85d6152c6f899359cdb", [:rebar3], [], "hexpm", "f172f3d74513e8edd445c257d596fc84dbdd56d2c6fa287434269648ae5a421e"},
"bandit": {:hex, :bandit, "1.10.3", "1e5d168fa79ec8de2860d1b4d878d97d4fbbe2fdbe7b0a7d9315a4359d1d4bb9", [:mix], [{:hpax, "~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.18", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "99a52d909c48db65ca598e1962797659e3c0f1d06e825a50c3d75b74a5e2db18"},
"chatterbox": {:hex, :ts_chatterbox, "0.15.1", "5cac4d15dd7ad61fc3c4415ce4826fc563d4643dee897a558ec4ea0b1c835c9c", [:rebar3], [{:hpack, "~> 0.3.0", [hex: :hpack_erl, repo: "hexpm", optional: false]}], "hexpm", "4f75b91451338bc0da5f52f3480fa6ef6e3a2aeecfc33686d6b3d0a0948f31aa"},
"ctx": {:hex, :ctx, "0.6.0", "8ff88b70e6400c4df90142e7f130625b82086077a45364a78d208ed3ed53c7fe", [:rebar3], [], "hexpm", "a14ed2d1b67723dbebbe423b28d7615eb0bdcba6ff28f2d1f1b0a7e1d4aa5fc2"},
Expand All @@ -18,9 +18,9 @@
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},
"makeup_erlang": {:hex, :makeup_erlang, "1.0.3", "4252d5d4098da7415c390e847c814bad3764c94a814a0b4245176215615e1035", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "953297c02582a33411ac6208f2c6e55f0e870df7f80da724ed613f10e6706afd"},
"mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"},
"mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"},
"mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"},
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
"nimble_pool": {:hex, :nimble_pool, "1.1.0", "bf9c29fbdcba3564a8b800d1eeb5a3c58f36e1e11d7b7fb2e084a643f645f06b", [:mix], [], "hexpm", "af2e4e6b34197db81f7aad230c1118eac993acc0dae6bc83bac0126d4ae0813a"},
"opentelemetry": {:hex, :opentelemetry, "1.7.0", "20d0f12d3d1c398d3670fd44fd1a7c495dd748ab3e5b692a7906662e2fb1a38a", [:rebar3], [{:opentelemetry_api, "~> 1.5.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "a9173b058c4549bf824cbc2f1d2fa2adc5cdedc22aa3f0f826951187bbd53131"},
"opentelemetry_api": {:hex, :opentelemetry_api, "1.5.0", "1a676f3e3340cab81c763e939a42e11a70c22863f645aa06aafefc689b5550cf", [:mix, :rebar3], [], "hexpm", "f53ec8a1337ae4a487d43ac89da4bd3a3c99ddf576655d071deed8b56a2d5dda"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule OpentelemetryBanditTest do

assert_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
attributes: span_attrs,
parent_span_id: 13_235_353_014_750_950_193
Expand Down Expand Up @@ -112,14 +112,14 @@ defmodule OpentelemetryBanditTest do

refute_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
parent_span_id: 13_235_353_014_750_950_193
)}

assert_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
links: links,
parent_span_id: :undefined
Expand Down Expand Up @@ -147,14 +147,14 @@ defmodule OpentelemetryBanditTest do

refute_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
parent_span_id: 13_235_353_014_750_950_193
)}

assert_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
links: links,
parent_span_id: :undefined
Expand All @@ -173,14 +173,14 @@ defmodule OpentelemetryBanditTest do

assert_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
parent_span_id: 13_235_353_014_750_950_193
)}

refute_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
parent_span_id: :undefined
)}
Expand Down Expand Up @@ -210,7 +210,7 @@ defmodule OpentelemetryBanditTest do

assert_receive {:span,
span(
name: :GET,
name: "GET",
attributes: span_attrs
)}

Expand Down Expand Up @@ -334,7 +334,7 @@ defmodule OpentelemetryBanditTest do

assert_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
attributes: span_attrs
)}
Expand Down Expand Up @@ -385,7 +385,7 @@ defmodule OpentelemetryBanditTest do

assert_receive {:span,
span(
name: :GET,
name: "GET",
attributes: span_attrs,
events: events,
status: ^expected_status
Expand Down Expand Up @@ -434,7 +434,7 @@ defmodule OpentelemetryBanditTest do

assert_receive {:span,
span(
name: :GET,
name: "GET",
attributes: span_attributes,
events: events,
status: ^expected_status
Expand Down Expand Up @@ -467,7 +467,7 @@ defmodule OpentelemetryBanditTest do

assert_receive {:span,
span(
name: :GET,
name: "GET",
attributes: span_attributes,
events: events,
status: ^expected_status
Expand All @@ -488,6 +488,19 @@ defmodule OpentelemetryBanditTest do
assert [] = :otel_events.list(events)
end

test "updates span name with route from conn.private[:plug_route]" do
OpentelemetryBandit.setup()
port = start_server()

Req.get("http://localhost:#{port}/with_route")

assert_receive {:span,
span(
name: "GET /items/:id",
kind: :server
)}
end

test "with halted request" do
OpentelemetryBandit.setup()
port = start_server()
Expand All @@ -498,7 +511,7 @@ defmodule OpentelemetryBanditTest do

assert_receive {:span,
span(
name: :GET,
name: "GET",
kind: :server,
attributes: span_attrs,
status: ^expected_status
Expand All @@ -522,6 +535,12 @@ defmodule OpentelemetryBanditTest do
conn |> send_resp(200, "OK")
end

def with_route(conn) do
conn
|> put_private(:plug_route, {"/items/:id", fn -> nil end})
|> send_resp(200, "OK")
end

def with_body(conn) do
conn
|> put_resp_content_type("application/json")
Expand Down