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

Middleware configuration #516

Open
gordalina opened this issue Jan 27, 2022 · 2 comments
Open

Middleware configuration #516

gordalina opened this issue Jan 27, 2022 · 2 comments

Comments

@gordalina
Copy link

First of all, let me thank you for such a great piece of software, it's really a fun library to work with.

In this issue, I want to talk about the use case where I want the ability to add logging and telemetry to every single tesla request in my application.

As a user of libraries that depend on tesla (1), or libraries that depend on other libraries that depend on tesla (2), it's cumbersome if not impossible to customize middleware.

In this example, we want to configure all Tesla clients with a Logger and Telemetry middleware so we can instrument our application both in development as well as in production.

In the first (1) example, we can look at elixir-google-api, where to customize middleware we have to create our client as seen below. It's a bit cumbersome but doable.

  defp client() do
    scope = "https://www.googleapis.com/auth/bigquery"
    {:ok, %Token{token: token}} = Token.for_scope(scope)

    middleware = [
      {Tesla.Middleware.Logger, debug: false},
      {Tesla.Middleware.Telemetry, metadata: %{client: :bigquery}}
    ]

    token
    |> Connection.new()
    |> Client.middleware()
    |> Kernel.++(middleware)
    |> Tesla.client()
  end

In the second (2) example, if we look into waffle and waffle_gcs which uses elixir-google-api we are completely unable to configure the tesla client (unless I'm missing something).

An argument could be made that library authors need to change their libraries to support configuring of the tesla client, which what I think @josevalim is trying to get to in googleapis/elixir-google-api#98. In that case, there should be more/better documentation around it.

Even in that scenario that some/many library authors allow the end-user to configure the tesla client, there would still be some that don't which invalidate the original use case - the ability to log and instrument every tesla client.

One solution that comes to mind is global and per client middleware configuration as envisioned in #167 and #403.

Any thoughts as to how we could solve for the original use-case?

@gordalina
Copy link
Author

@teamon is there any interest in solving this?

@teamon
Copy link
Member

teamon commented May 8, 2022

All things considered I think this would be a good opt-in feature. We'd need to think of all the corner cases, like e.g. what happens when you put Telemetry into global middlewares but there is one already defined locally etc, but I still think it's worth pursuing.

@teamon teamon added this to Roadmap May 8, 2022
@teamon teamon moved this to Idea in Roadmap May 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Idea
Development

No branches or pull requests

2 participants