Tesla.Middleware.OpenTelemetry: handle 400 as error (#153)

This commit is contained in:
Erick Dennis 2023-03-06 18:51:05 +01:00 committed by GitHub
parent 8897400d4e
commit bc11851360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 23 deletions

View File

@ -59,7 +59,7 @@ defmodule Tesla.Middleware.OpenTelemetry do
result result
end end
defp handle_result({:ok, %Tesla.Env{status: status} = env}) when status > 400 do defp handle_result({:ok, %Tesla.Env{status: status} = env}) when status >= 400 do
OpenTelemetry.Tracer.set_status(OpenTelemetry.status(:error, "")) OpenTelemetry.Tracer.set_status(OpenTelemetry.status(:error, ""))
{:ok, env} {:ok, env}

View File

@ -191,7 +191,39 @@ defmodule Tesla.Middleware.OpenTelemetryTest do
assert_receive {:span, span(name: "HTTP GET", attributes: _attributes)} assert_receive {:span, span(name: "HTTP GET", attributes: _attributes)}
end end
test "Marks Span status as :error when HTTP request fails", %{bypass: bypass} do @error_codes [
400,
401,
402,
403,
404,
405,
406,
407,
408,
409,
410,
411,
412,
413,
414,
415,
416,
417,
418,
500,
501,
502,
503,
504,
505,
506,
507,
508
]
for code <- @error_codes do
test "Marks Span status as :error when HTTP request fails with #{code}", %{bypass: bypass} do
defmodule TestClient do defmodule TestClient do
def get(client) do def get(client) do
Tesla.get(client, "/users/") Tesla.get(client, "/users/")
@ -208,7 +240,7 @@ defmodule Tesla.Middleware.OpenTelemetryTest do
end end
Bypass.expect_once(bypass, "GET", "/users", fn conn -> Bypass.expect_once(bypass, "GET", "/users", fn conn ->
Plug.Conn.resp(conn, 500, "") Plug.Conn.resp(conn, unquote(code), "")
end) end)
bypass.port bypass.port
@ -218,6 +250,7 @@ defmodule Tesla.Middleware.OpenTelemetryTest do
assert_receive {:span, span(status: {:status, :error, ""})} assert_receive {:span, span(status: {:status, :error, ""})}
end end
end
test "Marks Span status as :errors when max redirects are exceeded", %{bypass: bypass} do test "Marks Span status as :errors when max redirects are exceeded", %{bypass: bypass} do
defmodule TestClient do defmodule TestClient do