Skip to content

Commit

Permalink
Make task implementation for async configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
drtheuns committed Dec 8, 2024
1 parent c5fc04c commit f1cd148
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
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
```

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
{: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

0 comments on commit f1cd148

Please sign in to comment.