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,32 +191,65 @@ 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 [
defmodule TestClient do 400,
def get(client) do 401,
Tesla.get(client, "/users/") 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
def get(client) do
Tesla.get(client, "/users/")
end
def client(url) do
middleware = [
{Tesla.Middleware.BaseUrl, url},
Tesla.Middleware.OpenTelemetry
]
Tesla.client(middleware)
end
end end
def client(url) do Bypass.expect_once(bypass, "GET", "/users", fn conn ->
middleware = [ Plug.Conn.resp(conn, unquote(code), "")
{Tesla.Middleware.BaseUrl, url}, end)
Tesla.Middleware.OpenTelemetry
]
Tesla.client(middleware) bypass.port
end |> endpoint_url()
|> TestClient.client()
|> TestClient.get()
assert_receive {:span, span(status: {:status, :error, ""})}
end end
Bypass.expect_once(bypass, "GET", "/users", fn conn ->
Plug.Conn.resp(conn, 500, "")
end)
bypass.port
|> endpoint_url()
|> TestClient.client()
|> TestClient.get()
assert_receive {:span, span(status: {:status, :error, ""})}
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