-
Notifications
You must be signed in to change notification settings - Fork 5
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
Dataloader integration #21
Changes from 5 commits
79023c3
0ab03fb
d50d91c
a3722b0
cfbac56
4665b89
f9be08e
a0e11fd
e47b52f
ba49b1d
f0522f2
d5b061b
7392dc0
79921bd
17c0cfa
1ed3410
0652d9e
d22663a
c28e4d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,14 @@ defmodule Opencensus.Absinthe do | |
|
||
Worst case, you'll need to copy the code from the current `pipeline` target and add a call to | ||
`Opencensus.Absinthe.add_phases/1` as above. | ||
|
||
If you're using [`Dataloader`][dataloader], you will want to use the provided | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is in a |
||
`Opencensus.Absinthe.Middleware.Dataloader` Absinthe plugin module in place of | ||
the default one for tracing batched resolutions. See the [module | ||
docs][internal_dataloader] for details. | ||
|
||
[dataloader]: https://github.com/absinthe-graphql/dataloader | ||
[internal_dataloader]: https://hexdocs.pm/opencensus_absinthe/Opencensus.Absinthe.Middleware.Dataloader.html | ||
""" | ||
|
||
alias Absinthe.Middleware | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,15 +12,20 @@ defmodule Opencensus.Absinthe.Middleware do | |
@impl true | ||
@spec call(Resolution.t(), term()) :: Resolution.t() | ||
def call(%Resolution{state: :unresolved} = resolution, field: field) do | ||
acc = Acc.get(resolution) | ||
case Acc.get(resolution) do | ||
# nil -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to guard against this with some unit tests rather than commented out warnings. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added better comments and removed the commented-out warning 👍 |
||
# Logger.error("Handling tracing for a field with no span metadata: #{inspect(field, pretty: true)}") | ||
# resolution | ||
|
||
span_options = %{ | ||
attributes: field |> extract_metadata() |> Enum.into(%{}, &stringify_keys/1) | ||
} | ||
acc -> | ||
span_options = %{ | ||
attributes: field |> extract_metadata() |> Enum.into(%{}, &stringify_keys/1) | ||
} | ||
|
||
span_ctx = :oc_trace.start_span(field |> repr(), acc.span_ctx, span_options) | ||
middleware = resolution.middleware ++ [{{__MODULE__, :on_complete}, span_ctx: span_ctx}] | ||
%{resolution | middleware: middleware} | ||
span_ctx = :oc_trace.start_span(field |> repr(), acc.span_ctx, span_options) | ||
middleware = resolution.middleware ++ [{{__MODULE__, :on_complete}, span_ctx: span_ctx}] | ||
%{resolution | middleware: middleware} | ||
end | ||
end | ||
|
||
@doc false | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
if Code.ensure_loaded?(Dataloader) do | ||
defmodule Opencensus.Absinthe.Middleware.Dataloader do | ||
@moduledoc """ | ||
This is a small extension on top of `Absinthe.Middleware.Dataloader` that | ||
will create spans for each resolution. | ||
|
||
## Usage | ||
|
||
In your Absinthe schema, simply override the `plugins/0` callback (if you're | ||
not already) and prepend this plugin to the list: | ||
|
||
def plugins do | ||
[Opencensus.Absinthe.Middleware.Dataloader | Absinthe.Plugin.defaults()] | ||
end | ||
""" | ||
|
||
@behaviour Absinthe.Middleware | ||
@behaviour Absinthe.Plugin | ||
|
||
@span_key :dataloader_resolution_span_ctx | ||
@counter_key :dataloader_resolution_counter | ||
|
||
alias Opencensus.Absinthe.Acc | ||
alias Absinthe.Middleware.Dataloader, as: DefaultDataloader | ||
|
||
@doc """ | ||
The `Absinthe.Plugin` callback. Starts the OpenCensus span. | ||
""" | ||
def before_resolution(exec) do | ||
span_options = %{attributes: %{}} | ||
acc = Acc.get(exec) | ||
|
||
{counter, new_acc} = | ||
Map.get_and_update(acc, @counter_key, fn cur -> | ||
case cur do | ||
nil -> {cur, 1} | ||
x -> {x, x + 1} | ||
end | ||
end) | ||
|
||
span_ctx = :oc_trace.start_span("resolution_#{counter || 0}", acc.span_ctx, span_options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love the counter! Can we make that part of the main accumulator so we can track how many times we looped the loop regardless of the mechanism? I'm OK with an extra span per overall resolution named Let's also put the counter to the resolution span attributes as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok - I think I got this working - if not, please be detailed on what I'm doing wrong. Again, I'm not too familiar with Absinthe. ='( Thank you very much! |
||
new_acc = Map.put(new_acc, @span_key, span_ctx) | ||
|
||
exec | ||
|> Acc.set(new_acc) | ||
|> DefaultDataloader.before_resolution() | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recently automated insertion of Turns out, it's a mess when tracing every field meets Strikes me if you finish your span right here, we'll know! Read also, #26, which is all about this. |
||
|
||
@doc """ | ||
The `Absinthe.Plugin` callback. Finishes the OpenCensus span. | ||
""" | ||
def after_resolution(exec) do | ||
acc = Acc.get(exec) | ||
|
||
acc | ||
|> Map.get(@span_key) | ||
|> :oc_trace.finish_span() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we wait until |
||
|
||
acc = | ||
exec | ||
|> Acc.get() | ||
|> Map.delete(@span_key) | ||
|
||
exec | ||
|> Acc.set(acc) | ||
|> DefaultDataloader.after_resolution() | ||
end | ||
|
||
def call(resolution, callback), do: DefaultDataloader.call(resolution, callback) | ||
def pipeline(pipeline, exec), do: DefaultDataloader.pipeline(pipeline, exec) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm just too much of an [RFC 2119 fan, but can we get the word SHOULD in here?