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 1 commit
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
8 changes: 8 additions & 0 deletions guides/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ 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

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
20 changes: 20 additions & 0 deletions lib/absinthe/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,24 @@ defmodule Absinthe.Utils do
#{types ++ directives}
"""
end

@spec async((-> any)) :: Task.t()
def async(fun) do
case Application.fetch_env(:absinthe, :task_module) do
drtheuns marked this conversation as resolved.
Show resolved Hide resolved
{:ok, module} when is_atom(module) ->
module.async(fun)

_ ->
async_default(fun)
end
end

# Optionally use `async/1` function from `opentelemetry_process_propagator` if available
if Code.ensure_loaded?(OpentelemetryProcessPropagator.Task) do
@spec async_default((-> any)) :: Task.t()
defdelegate async_default(fun), to: OpentelemetryProcessPropagator.Task, as: :async
else
@spec async_default((-> any)) :: Task.t()
defdelegate async_default(fun), to: Task, as: :async
end
end