Skip to content

Commit

Permalink
HTTPoision use atoms for span keys
Browse files Browse the repository at this point in the history
The other http client implementations use atoms for span keys. Update
the HTTPoison integration for consistency.

This shouldn't have any impact other than in testing.
  • Loading branch information
jeffutter committed Jan 24, 2024
1 parent 3a9ed42 commit a24fd4e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ defmodule OpentelemetryHTTPoison do
alias OpenTelemetry.Tracer
alias OpentelemetryHTTPoison.Configuration

@http_url Atom.to_string(Conventions.http_url())
@http_method Atom.to_string(Conventions.http_method())
@http_route Atom.to_string(Conventions.http_route())
@http_status_code Atom.to_string(Conventions.http_status_code())
@net_peer_name Atom.to_string(Conventions.net_peer_name())
@http_url Conventions.http_url()
@http_method Conventions.http_method()
@http_route Conventions.http_route()
@http_status_code Conventions.http_status_code()
@net_peer_name Conventions.net_peer_name()

@doc ~S"""
Configures OpentelemetryHTTPoison using the provided `opts` `Keyword list`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ defmodule OpentelemetryHTTPoisonTest do
assert_receive {:span, span(attributes: attributes_record, name: "GET")}
attributes = elem(attributes_record, 4)

assert ["http.method", "http.status_code", "http.url", "net.peer.name"] ==
assert [:"http.method", :"http.status_code", :"http.url", :"net.peer.name"] ==
attributes |> Map.keys() |> Enum.sort()

assert {"http.method", "GET"} in attributes
assert {"net.peer.name", "localhost"} in attributes
assert {:"http.method", "GET"} in attributes
assert {:"net.peer.name", "localhost"} in attributes
end

test "traceparent header is injected when no headers" do
Expand Down Expand Up @@ -64,36 +64,44 @@ defmodule OpentelemetryHTTPoisonTest do
OpentelemetryHTTPoison.get!("http://user:pass@localhost:8000/user/edit/24")

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_attributes(attributes, {"http.url", "http://localhost:8000/user/edit/24"})
assert confirm_attributes(attributes, {:"http.url", "http://localhost:8000/user/edit/24"})
end
end

describe "OpentelemetryHTTPoison calls with additional options" do
test "additional span attributes can be passed to OpentelemetryHTTPoison invocation" do
OpentelemetryHTTPoison.get!("http://localhost:8000", [], ot_attributes: [{"app.callname", "mariorossi"}])
OpentelemetryHTTPoison.get!("http://localhost:8000", [],
ot_attributes: [{"app.callname", "mariorossi"}]
)

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_attributes(attributes, {"app.callname", "mariorossi"})
end

test "resource route can be explicitly passed to OpentelemetryHTTPoison invocation as a string" do
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: "/user/edit")
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: "/user/edit"
)

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_attributes(attributes, {"http.route", "/user/edit"})
assert confirm_attributes(attributes, {:"http.route", "/user/edit"})
end

test "resource route can be explicitly passed to OpentelemetryHTTPoison invocation as a function" do
infer_fn = fn request -> URI.parse(request.url).path end

OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: infer_fn)
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: infer_fn
)

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_attributes(attributes, {"http.route", "/user/edit/24"})
assert confirm_attributes(attributes, {:"http.route", "/user/edit/24"})
end

test "resource route inference can be explicitly ignored" do
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: :ignore)
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: :ignore
)

assert_receive {:span, span(attributes: attributes)}, 1000
refute confirm_http_route_attribute(attributes)
Expand All @@ -108,11 +116,15 @@ defmodule OpentelemetryHTTPoisonTest do

test "resource route inference fails if an incorrect value is passed to the OpentelemetryHTTPoison invocation" do
assert_raise(ArgumentError, fn ->
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: nil)
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: nil
)
end)

assert_raise(ArgumentError, fn ->
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: 1)
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: 1
)
end)
end

Expand All @@ -123,7 +135,7 @@ defmodule OpentelemetryHTTPoisonTest do
)

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_attributes(attributes, {"http.route", "/user/edit"})
assert confirm_attributes(attributes, {:"http.route", "/user/edit"})
assert confirm_attributes(attributes, {"app.callname", "mariorossi"})
end
end
Expand Down Expand Up @@ -234,7 +246,9 @@ defmodule OpentelemetryHTTPoisonTest do
end

test "resource route can be implicitly inferred by OpentelemetryHTTPoison invocation using a default function" do
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: :infer)
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: :infer
)

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_http_route_attribute(attributes, "/user/:subpath")
Expand All @@ -247,7 +261,9 @@ defmodule OpentelemetryHTTPoisonTest do

set_env(:infer_route, infer_fn)

OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: :infer)
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: :infer
)

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_http_route_attribute(attributes, "/user/edit/24")
Expand All @@ -262,7 +278,9 @@ defmodule OpentelemetryHTTPoisonTest do

set_env(:infer_route, infer_fn)

OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: invocation_infer_fn)
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: invocation_infer_fn
)

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_http_route_attribute(attributes, "test")
Expand All @@ -275,7 +293,9 @@ defmodule OpentelemetryHTTPoisonTest do

set_env(:infer_route, infer_fn)

OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [], ot_resource_route: "test")
OpentelemetryHTTPoison.get!("http://localhost:8000/user/edit/24", [],
ot_resource_route: "test"
)

assert_receive {:span, span(attributes: attributes)}, 1000
assert confirm_http_route_attribute(attributes, "test")
Expand Down Expand Up @@ -310,7 +330,7 @@ defmodule OpentelemetryHTTPoisonTest do
end

defp confirm_http_route_attribute(attributes, value) do
confirm_attributes(attributes, {"http.route", value})
confirm_attributes(attributes, {:"http.route", value})
end

defp confirm_http_route_attribute(attributes) do
Expand Down

0 comments on commit a24fd4e

Please sign in to comment.