Skip to content

Commit

Permalink
Support Bandit 1.4 telemetry changes (#311)
Browse files Browse the repository at this point in the history
Co-authored-by: Bryan Naegele <[email protected]>
  • Loading branch information
elvanja and bryannaegele authored Jul 22, 2024
1 parent f8916bb commit 1f95229
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
23 changes: 12 additions & 11 deletions instrumentation/opentelemetry_bandit/lib/opentelemetry_bandit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defmodule OpentelemetryBandit do
duration = measurements.duration
end_time = :opentelemetry.timestamp()
start_time = end_time - duration
resp_body_bytes = Map.get(measurements, :resp_body_bytes, 0)

url = extract_url(meta, conn)
request_path = extract_request_path(meta, conn)
Expand All @@ -52,10 +53,10 @@ defmodule OpentelemetryBandit do
if Map.has_key?(meta, :error) do
%{
Trace.http_url() => url,
Trace.http_method() => meta.method,
Trace.http_method() => conn.method,
Trace.net_transport() => :"IP.TCP",
Trace.http_response_content_length() => measurements.resp_body_bytes,
Trace.http_status_code() => meta.status
Trace.http_response_content_length() => resp_body_bytes,
Trace.http_status_code() => conn.status
}
else
%{
Expand All @@ -65,17 +66,17 @@ defmodule OpentelemetryBandit do
Trace.net_peer_name() => conn.host,
Trace.net_peer_port() => conn.port,
Trace.http_target() => conn.request_path,
Trace.http_method() => meta.method,
Trace.http_status_code() => meta.status,
Trace.http_response_content_length() => measurements.resp_body_bytes,
Trace.http_method() => conn.method,
Trace.http_status_code() => conn.status,
Trace.http_response_content_length() => resp_body_bytes,
Trace.net_transport() => :"IP.TCP",
Trace.http_user_agent() => user_agent(conn)
}
end

span_kind = if Map.has_key?(meta, :error), do: :error, else: :server

span_id = "HTTP #{meta.method} #{request_path}" |> String.trim()
span_id = "HTTP #{conn.method} #{request_path}" |> String.trim()

OpenTelemetry.Tracer.start_span(span_id, %{
attributes: attributes,
Expand Down Expand Up @@ -132,8 +133,8 @@ defmodule OpentelemetryBandit do
span
end

defp extract_url(%{error: _} = meta, _conn) do
case Map.get(meta, :request_target) do
defp extract_url(%{error: _}, conn) do
case Map.get(conn, :request_target) do
nil -> ""
{scheme, host, port, path} -> build_url(scheme, host, port, path)
end
Expand All @@ -143,8 +144,8 @@ defmodule OpentelemetryBandit do
build_url(conn.scheme, conn.host, conn.port, conn.request_path)
end

defp extract_request_path(%{error: _} = meta, _conn) do
case Map.get(meta, :request_target) do
defp extract_request_path(%{error: _}, conn) do
case Map.get(conn, :request_target) do
nil -> ""
{_, _, _, path} -> path || ""
end
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/opentelemetry_bandit/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule OpentelemetryBandit.MixProject do
# dev dependencies
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:excoveralls, "~> 0.18", only: :test},
{:bandit, "~> 1.0", only: [:dev, :test], runtime: false},
{:bandit, "~> 1.4", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},
{:opentelemetry, "~> 1.0", only: [:dev, :test]},
{:opentelemetry_exporter, "~> 1.0", only: [:dev, :test]},
Expand Down
4 changes: 2 additions & 2 deletions instrumentation/opentelemetry_bandit/mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"acceptor_pool": {:hex, :acceptor_pool, "1.0.0", "43c20d2acae35f0c2bcd64f9d2bde267e459f0f3fd23dab26485bf518c281b21", [:rebar3], [], "hexpm", "0cbcd83fdc8b9ad2eee2067ef8b91a14858a5883cb7cd800e6fcd5803e158788"},
"bandit": {:hex, :bandit, "1.0.0", "2bd87bbf713d0eed0090f2fa162cd1676198122e6c2b68a201c706e354a6d5e5", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [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", "32acf6ac030fee1f99fd9c3fcf81671911ae8637e0a61c98111861b466efafdb"},
"bandit": {:hex, :bandit, "1.4.2", "a1475c8dcbffd1f43002797f99487a64c8444753ff2b282b52409e279488e1f5", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [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", "3db8bacea631bd926cc62ccad58edfee4252d1b4c5cccbbad9825df2722b884f"},
"castore": {:hex, :castore, "1.0.7", "b651241514e5f6956028147fe6637f7ac13802537e895a724f90bf3e36ddd1dd", [:mix], [], "hexpm", "da7785a4b0d2a021cd1292a60875a784b6caef71e76bf4917bdee1f390455cf5"},
"chatterbox": {:hex, :ts_chatterbox, "0.13.0", "6f059d97bcaa758b8ea6fffe2b3b81362bd06b639d3ea2bb088335511d691ebf", [:rebar3], [{:hpack, "~> 0.2.3", [hex: :hpack_erl, repo: "hexpm", optional: false]}], "hexpm", "b93d19104d86af0b3f2566c4cba2a57d2e06d103728246ba1ac6c3c0ff010aa7"},
"ctx": {:hex, :ctx, "0.6.0", "8ff88b70e6400c4df90142e7f130625b82086077a45364a78d208ed3ed53c7fe", [:rebar3], [], "hexpm", "a14ed2d1b67723dbebbe423b28d7615eb0bdcba6ff28f2d1f1b0a7e1d4aa5fc2"},
Expand Down Expand Up @@ -35,7 +35,7 @@
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"telemetry_registry": {:hex, :telemetry_registry, "0.3.1", "14a3319a7d9027bdbff7ebcacf1a438f5f5c903057b93aee484cca26f05bdcba", [:mix, :rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6d0ca77b691cf854ed074b459a93b87f4c7f5512f8f7743c635ca83da81f939e"},
"thousand_island": {:hex, :thousand_island, "1.0.0", "63fc8807d8607c9d74fa670996897c8c8a1f2022c8c68d024182e45249acd756", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "996320c72ba8f34d7be9b02900622e44341649f24359e0f67643e4dda8f23995"},
"thousand_island": {:hex, :thousand_island, "1.3.5", "6022b6338f1635b3d32406ff98d68b843ba73b3aa95cfc27154223244f3a6ca5", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2be6954916fdfe4756af3239fb6b6d75d0b8063b5df03ba76fd8a4c87849e180"},
"tls_certificate_check": {:hex, :tls_certificate_check, "1.20.0", "1ac0c53f95e201feb8d398ef9d764ae74175231289d89f166ba88a7f50cd8e73", [:rebar3], [{:ssl_verify_fun, "~> 1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "ab57b74b1a63dc5775650699a3ec032ec0065005eff1f020818742b7312a8426"},
"websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ defmodule OpentelemetryBanditTest do
[:bandit, :request, :stop],
%{duration: 444, resp_body_bytes: 10},
%{
conn: nil,
status: 500,
error: "Internal Server Error",
method: "GET",
request_target: {nil, nil, nil, "/not_existing_route"}
conn: %{
status: 500,
method: "GET",
request_target: {nil, nil, nil, "/not_existing_route"}
},
error: "Internal Server Error"
}
)

Expand All @@ -130,11 +131,8 @@ defmodule OpentelemetryBanditTest do
[:bandit, :request, :stop],
%{duration: 444, resp_body_bytes: 10},
%{
conn: nil,
status: 500,
error: "Internal Server Error",
method: "GET",
request_target: nil
conn: %{status: 500, method: "GET", request_target: nil},
error: "Internal Server Error"
}
)

Expand Down

0 comments on commit 1f95229

Please sign in to comment.