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

Fix Ecto parallel preloads #915

Merged
merged 2 commits into from
Jan 24, 2024
Merged

Fix Ecto parallel preloads #915

merged 2 commits into from
Jan 24, 2024

Conversation

unflxw
Copy link
Contributor

@unflxw unflxw commented Jan 22, 2024

Fixes #603. See also https://github.com/appsignal/appsignal-docs/pull/1715. Use appsignal/test-setups#184 to test it.

Add the Appsignal.Ecto.Repo module to fix the Ecto parallel preload issue, making it easy to apply the workaround that was already found by Elixir Forums users.

After this change, customers can replace use Ecto.Repo in their Ecto repositories with use Appsignal.Ecto.Repo. This will configure Ecto so that it passes through the current span at invocation time as the telemetry options for the query, fixing the issue where parallel preload queries were lost because they took place in parallel processes where there was no current span.

For customers who wish to customise their Ecto repo default options, which would override the default_options/1 function implemented by Appsignal.Ecto.Repo, a workaround is provided by also exposing this function as Appsignal.Ecto.Repo.default_options, so customers can call it manually and merge its output with their own default options.

@backlog-helper
Copy link

backlog-helper bot commented Jan 22, 2024

✔️ All good!

New issue guide | Backlog management | Rules | Feedback

@unflxw
Copy link
Contributor Author

unflxw commented Jan 22, 2024

I was wondering if there would be an easy way for Appsignal.Ecto.Repo to automatically register the telemetry handlers for the Ecto repo it is used in, providing a slightly easier way to register additional repos than Appsignal.Ecto.attach/2 and lessening the reliance on the OTP app auto-detection.

(This is not a blocker, just a thought)

@unflxw unflxw changed the title Add Appsignal.Ecto.Repo module Fix Ecto parallel preloads Jan 22, 2024
Add the `Appsignal.Ecto.Repo` module to fix the Ecto parallel preload
issue, making it easy to apply the workaround that was already found
by [Elixir Forums][solution] users.

After this change, customers can replace `use Ecto.Repo` in their Ecto
repositories with `use Appsignal.Ecto.Repo`. This will configure Ecto
so that it passes through the current span at invocation time as the
telemetry options for the query, fixing the issue where parallel
preload queries were lost because they took place in parallel
processes where there was no current span.

For customers who wish to customise their Ecto repo default options,
which would override the `default_options/1` function implemented by
`Appsignal.Ecto.Repo`, a workaround is provided by also exposing this
function as `Appsignal.Ecto.Repo.default_options`, so customers can
call it manually and merge its output with their own default options.

[solution]: https://elixirforum.com/t/help-understanding-ecto-telemetry-events-according-to-appsignal/35957/3
@unflxw unflxw force-pushed the ecto-preload-instrumentation branch from cdb305d to ae09d17 Compare January 22, 2024 17:48
Copy link
Member

@jeffkreeftmeijer jeffkreeftmeijer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a nice patch! I'm very excited that this is an add-on instead of a completely new implementation. 👍

Co-authored-by: Tom de Bruijn <tom@tomdebruijn.com>
@unflxw unflxw merged commit 61d9c1c into main Jan 24, 2024
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for parallel Ecto preloads
4 participants