Skip to content

Middleware configuration #516

@gordalina

Description

@gordalina

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Idea

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions