tesla icon indicating copy to clipboard operation
tesla copied to clipboard

Allow `update_env/2` function as the option for `Tesla.Middleware.Retry`

Open smt116 opened this issue 2 years ago • 2 comments

The function takes the result and env from the previous attempt and returns a new env for the retry request. It allows supporting "retry with new authorization token" cases.

smt116 avatar Jul 04 '22 12:07 smt116

@yordis, no options were renamed. I've just sorted the list alphabetically for better readability.

smt116 avatar Jul 04 '22 19:07 smt116

🤦🏻‍♂️ .... nevermind ... 🤦🏻‍♂️

yordis avatar Jul 04 '22 20:07 yordis

What’s the use case for updating the env on retry? If feels a bit weird to add it as an option.

In case of authentication I’d probably write a custom middleware with something like:

case Tesla.run(env, next) do
  {:ok, %{status: 401}} -> 
    env 
    |> Tesla.put_header(…)
    |> Tesla.run(next)
  other -> 
    other
end

teamon avatar May 15 '23 18:05 teamon

@teamon beside that use case, I am trying to find who created an issue or comment or something where the person wanted to log the retries because it was important to him to know how many retries for a given endpoint were happening type of thing ...

For the use 401 use case, I would propose adding a middleware before the retry.

yordis avatar May 15 '23 19:05 yordis

Also this, https://hexdocs.pm/opentelemetry_semantic_conventions/OpenTelemetry.SemanticConventions.Trace.html#http_retry_count/0

I intended to use this PR to try to change the http_retry_count in https://github.com/open-telemetry/opentelemetry-erlang-contrib/blob/main/instrumentation/opentelemetry_tesla/lib/middleware/opentelemetry_tesla_middleware.ex, and teach people how to connect them.

If you expose the OTEL context, you may also want such information.


So from my end, two use cases thus far,

  1. Somebody asked to be able to log retries so they can collect metrics about it
  2. Be able to add http_retry_count to a request.

yordis avatar May 15 '23 19:05 yordis

Maybe Retry middleware should just add retry_attempt counter to env.opts?

teamon avatar May 16 '23 14:05 teamon

Tricky situation, even adding that the OTEL Middleware would be the one concerning OTEL stuff.

  • Would that require to fallback to the OTEL middleware?
  • Or Would you suggest adding another middleware in front of Retry?

Same for the logger

The tricky bit is the direction of dependencies and the runtime workflow. I am unsure how to accomplish the tasks without introducing coupled middleware to the Retry middleware and the potential complexity and ordering nightmare or opening up the API a bit.

Not sure.

yordis avatar May 16 '23 16:05 yordis

Found the issue https://github.com/elixir-tesla/tesla/issues/578 @egeersoz; maybe you want to participate over here!

yordis avatar May 16 '23 16:05 yordis

Hey there, thank you so much for taking the time to contribute, as @teamon said https://github.com/elixir-tesla/tesla/pull/537#issuecomment-1548363180 I recommend you write a custom middleware related to your authentication needs that reinject the new token.

yordis avatar Sep 10 '23 10:09 yordis