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

Make task implementation for async configurable #1349

Open
wants to merge 3 commits 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
10 changes: 10 additions & 0 deletions guides/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ Instead, you can add the `:opentelemetry_process_propagator` package to your
dependencies, which has a `Task.async/1` wrapper that will attach the context
automatically. If the package is installed, the middleware will use it in place
of the default `Task.async/1`.

Alternatively, you can configure the `Task` implementation to use:

```elixir
config :absinthe, task_module: MyTaskModule
```
drtheuns marked this conversation as resolved.
Show resolved Hide resolved

This is a compile-time option, so it cannot be configured in `runtime.exs`.
Instead, place it in `config.exs` or build-specific configs (`dev.exs`,
`prod.exs`, etc.). The provided module must support the `async/1` function.
11 changes: 2 additions & 9 deletions lib/absinthe/middleware/async.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ defmodule Absinthe.Middleware.Async do
@behaviour Absinthe.Middleware
@behaviour Absinthe.Plugin

import Absinthe.Utils, only: [async: 1]

# A function has handed resolution off to this middleware. The first argument
# is the current resolution struct. The second argument is the function to
# execute asynchronously, and opts we'll want to use when it is time to await
Expand Down Expand Up @@ -110,13 +112,4 @@ defmodule Absinthe.Middleware.Async do
pipeline
end
end

# Optionally use `async/1` function from `opentelemetry_process_propagator` if available
if Code.ensure_loaded?(OpentelemetryProcessPropagator.Task) do
@spec async((-> any)) :: Task.t()
defdelegate async(fun), to: OpentelemetryProcessPropagator.Task
else
@spec async((-> any)) :: Task.t()
defdelegate async(fun), to: Task
end
end
11 changes: 2 additions & 9 deletions lib/absinthe/middleware/batch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ defmodule Absinthe.Middleware.Batch do

require Logger

import Absinthe.Utils, only: [async: 1]

@typedoc """
The function to be called with the aggregate batch information.

Expand Down Expand Up @@ -226,13 +228,4 @@ defmodule Absinthe.Middleware.Batch do
pipeline
end
end

# Optionally use `async/1` function from `opentelemetry_process_propagator` if available
if Code.ensure_loaded?(OpentelemetryProcessPropagator.Task) do
@spec async((-> any)) :: Task.t()
defdelegate async(fun), to: OpentelemetryProcessPropagator.Task
else
@spec async((-> any)) :: Task.t()
defdelegate async(fun), to: Task
end
end
15 changes: 15 additions & 0 deletions lib/absinthe/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,19 @@ defmodule Absinthe.Utils do
#{types ++ directives}
"""
end

task_module = Application.compile_env(:absinthe, :task_module)

@spec async((-> any)) :: Task.t()
cond do
is_atom(task_module) and task_module != nil ->
def async(fun), do: unquote(task_module).async(fun)

# Optionally use `async/1` function from `opentelemetry_process_propagator` if available
Code.ensure_loaded?(OpentelemetryProcessPropagator.Task) ->
defdelegate async(fun), to: OpentelemetryProcessPropagator.Task

true ->
defdelegate async(fun), to: Task
end
end