2022-08-25 18:47:59 +00:00
|
|
|
defmodule Tesla.Middleware.OpenTelemetry do
|
2022-10-04 22:32:36 +00:00
|
|
|
@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
|
2023-10-03 13:39:45 +00:00
|
|
|
standards and use just the method name, if not being overridden by opts.
|
2022-10-04 22:32:36 +00:00
|
|
|
|
|
|
|
NOTE: This middleware needs to come before `Tesla.Middleware.PathParams`
|
|
|
|
|
|
|
|
## Options
|
|
|
|
|
|
|
|
- `:span_name` - override span name. Can be a `String` for a static span name,
|
|
|
|
or a function that takes the `Tesla.Env` and returns a `String`
|
2023-05-29 10:53:21 +00:00
|
|
|
- `:propagator` - configures trace headers propagators. Setting it to `:none` disables propagation.
|
|
|
|
Any module that implements `:otel_propagator_text_map` can be used.
|
|
|
|
Defaults to calling `:otel_propagator_text_map.get_text_map_injector/0`
|
2023-04-21 16:07:21 +00:00
|
|
|
- `:mark_status_ok` - configures spans with a list of expected HTTP error codes to be marked as `ok`,
|
|
|
|
not as an error-containing spans
|
2022-10-04 22:32:36 +00:00
|
|
|
"""
|
2022-11-15 23:39:22 +00:00
|
|
|
|
|
|
|
alias OpenTelemetry.SemanticConventions.Trace
|
|
|
|
|
2022-08-25 18:47:59 +00:00
|
|
|
require OpenTelemetry.Tracer
|
2022-11-15 23:39:22 +00:00
|
|
|
require Trace
|
|
|
|
|
2022-08-25 18:47:59 +00:00
|
|
|
@behaviour Tesla.Middleware
|
|
|
|
|
2022-10-04 22:32:36 +00:00
|
|
|
def call(env, next, opts) do
|
|
|
|
span_name = get_span_name(env, Keyword.get(opts, :span_name))
|
2022-08-25 18:47:59 +00:00
|
|
|
|
|
|
|
OpenTelemetry.Tracer.with_span span_name, %{kind: :client} do
|
|
|
|
env
|
2023-04-21 16:07:21 +00:00
|
|
|
|> maybe_put_additional_ok_statuses(opts[:mark_status_ok])
|
2023-05-29 10:53:21 +00:00
|
|
|
|> maybe_propagate(Keyword.get(opts, :propagator, :opentelemetry.get_text_map_injector()))
|
2022-08-25 18:47:59 +00:00
|
|
|
|> Tesla.run(next)
|
|
|
|
|> set_span_attributes()
|
|
|
|
|> handle_result()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-04 22:32:36 +00:00
|
|
|
defp get_span_name(_env, span_name) when is_binary(span_name) do
|
|
|
|
span_name
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_span_name(env, span_name_fun) when is_function(span_name_fun, 1) do
|
|
|
|
span_name_fun.(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_span_name(env, _) do
|
2022-08-25 18:47:59 +00:00
|
|
|
case env.opts[:path_params] do
|
|
|
|
nil -> "HTTP #{http_method(env.method)}"
|
|
|
|
_ -> URI.parse(env.url).path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-29 10:53:21 +00:00
|
|
|
defp maybe_propagate(env, :none), do: env
|
|
|
|
|
|
|
|
defp maybe_propagate(env, propagator) do
|
|
|
|
:otel_propagator_text_map.inject(
|
|
|
|
propagator,
|
|
|
|
env,
|
|
|
|
fn key, value, env -> Tesla.put_header(env, key, value) end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-04-21 16:07:21 +00:00
|
|
|
defp maybe_put_additional_ok_statuses(env, [_ | _] = additional_ok_statuses) do
|
|
|
|
case env.opts[:additional_ok_statuses] do
|
|
|
|
nil -> Tesla.put_opt(env, :additional_ok_statuses, additional_ok_statuses)
|
|
|
|
_ -> env
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_put_additional_ok_statuses(env, _additional_ok_statuses), do: env
|
|
|
|
|
2022-08-25 18:47:59 +00:00
|
|
|
defp set_span_attributes({_, %Tesla.Env{} = env} = result) do
|
|
|
|
OpenTelemetry.Tracer.set_attributes(build_attrs(env))
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
defp set_span_attributes(result) do
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2023-04-21 16:07:21 +00:00
|
|
|
defp handle_result({:ok, %Tesla.Env{status: status, opts: opts} = env}) when status >= 400 do
|
|
|
|
span_status =
|
|
|
|
if status in Keyword.get(opts, :additional_ok_statuses, []), do: :ok, else: :error
|
|
|
|
|
|
|
|
span_status
|
|
|
|
|> OpenTelemetry.status("")
|
|
|
|
|> OpenTelemetry.Tracer.set_status()
|
2022-08-25 18:47:59 +00:00
|
|
|
|
|
|
|
{:ok, env}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_result({:error, {Tesla.Middleware.FollowRedirects, :too_many_redirects}} = result) do
|
|
|
|
OpenTelemetry.Tracer.set_status(OpenTelemetry.status(:error, ""))
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_result({:ok, env}) do
|
|
|
|
{:ok, env}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_result(result) do
|
|
|
|
OpenTelemetry.Tracer.set_status(OpenTelemetry.status(:error, ""))
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
defp build_attrs(%Tesla.Env{
|
|
|
|
method: method,
|
|
|
|
url: url,
|
|
|
|
status: status_code,
|
|
|
|
headers: headers,
|
|
|
|
query: query
|
|
|
|
}) do
|
|
|
|
url = Tesla.build_url(url, query)
|
|
|
|
uri = URI.parse(url)
|
|
|
|
|
|
|
|
attrs = %{
|
2022-11-15 23:39:22 +00:00
|
|
|
Trace.http_method() => http_method(method),
|
|
|
|
Trace.http_url() => url,
|
|
|
|
Trace.http_target() => uri.path,
|
|
|
|
Trace.net_host_name() => uri.host,
|
|
|
|
Trace.http_scheme() => uri.scheme,
|
|
|
|
Trace.http_status_code() => status_code
|
2022-08-25 18:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
maybe_append_content_length(attrs, headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_append_content_length(attrs, headers) do
|
|
|
|
case Enum.find(headers, fn {k, _v} -> k == "content-length" end) do
|
|
|
|
nil ->
|
|
|
|
attrs
|
|
|
|
|
|
|
|
{_key, content_length} ->
|
2022-11-15 23:39:22 +00:00
|
|
|
Map.put(attrs, Trace.http_response_content_length(), content_length)
|
2022-08-25 18:47:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp http_method(method) do
|
|
|
|
method
|
|
|
|
|> Atom.to_string()
|
|
|
|
|> String.upcase()
|
|
|
|
end
|
|
|
|
end
|