Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tesla): allows to read from keep request req_url #408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -2,11 +2,37 @@ defmodule Tesla.Middleware.OpenTelemetry do
@moduledoc """
Creates OpenTelemetry spans and injects tracing headers into HTTP requests

When used with `Tesla.Middleware.PathParams`, the span name will be created
based on the provided path. Without it, the span name follow OpenTelemetry
standards and use just the method name, if not being overridden by opts.
## Span Name

NOTE: This middleware needs to come before `Tesla.Middleware.PathParams`
If `span_name` is provided, the span name will be created based on the
provided value.

(Recommended Setup) If `Tesla.Middleware.KeepRequest` is used, the span name
will be created based on the request URL stored in `:req_url`.

What that means in practice is that you should set the `Tesla.Middleware.PathParams`
after `Tesla.Middleware.KeepRequest` and before `Tesla.Middleware.OpenTelemetry`
to use the path params before it has been processed by `Tesla.Middleware.PathParams`.

iex> client = Tesla.client([
...> Tesla.Middleware.KeepRequest,
...> Tesla.Middleware.PathParams,
...> # ... other middleware
...> Tesla.Middleware.OpenTelemetry
...> ])

If `Tesla.Middleware.PathParams` is used without
`Tesla.Middleware.KeepRequest`, and `:path_params` is provided, the span name
will be created based on the provided path.

What that means in practice is that you should set the `Tesla.Middleware.PathParams`
after `Tesla.Middleware.OpenTelemetry` to use the path params before it has
been processed by `Tesla.Middleware.PathParams`.

iex> client = Tesla.client([
...> Tesla.Middleware.OpenTelemetry,
...> Tesla.Middleware.PathParams
...> ])

## Options

Expand Down Expand Up @@ -48,9 +74,15 @@ defmodule Tesla.Middleware.OpenTelemetry do
end

defp get_span_name(env, _) do
case env.opts[:path_params] do
nil -> "HTTP #{http_method(env.method)}"
_ -> URI.parse(env.url).path
cond do
env.opts[:req_url] ->
URI.parse(env.opts[:req_url]).path

env.opts[:path_params] ->
URI.parse(env.url).path

true ->
"HTTP #{http_method(env.method)}"
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ defmodule Tesla.Middleware.OpenTelemetryTest do
end

describe "span name" do
test "uses generic route name when opentelemetry middleware is configured with path params and keep request middleware",
%{bypass: bypass} do
Bypass.expect_once(bypass, "GET", "/users/3", fn conn ->
Plug.Conn.resp(conn, 204, "")
end)

client =
Tesla.client([
{Tesla.Middleware.BaseUrl, endpoint_url(bypass.port)},
Tesla.Middleware.KeepRequest,
Tesla.Middleware.PathParams,
Tesla.Middleware.OpenTelemetry
])

Tesla.get(client, "/users/:id", opts: [path_params: [id: "3"]])

assert_receive {:span, span(name: "/users/:id", attributes: _attributes)}
end

test "uses generic route name when opentelemetry middleware is configured before path params middleware",
%{
bypass: bypass
Expand Down
Loading