-
Notifications
You must be signed in to change notification settings - Fork 318
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
Allow configuring stacktrace for logging #658
base: master
Are you sure you want to change the base?
Conversation
A lot of times, more stack items can be helpful in identifying the source of a query. This commit demonstrates how we can support it. But the API has problems: 1. the `:stacktrace` option is used not only by logging but also for general telemetry events. Making it an integer doesn't make sense for telemetry (as the full stacktrace is published there anyway). 2. the `:stacktrace` option is overriden Ecto-side, so Ecto.SQL can't accept integers on a per-query basis, only on global configuration.
Left the alternative `last_non_ecto` implementation taking a parameter for length. Goal is to make it easier to just increase the length without needing to implement a new function or use a 3rd party package.
6ee7ee5
to
80e5eb8
Compare
Co-authored-by: José Valim <[email protected]>
@josevalim Pushed all your suggestions, thanks for the thorough review! Only one thing: EctoSQL integration CI is running on v1.11.4 (this is the oldest supported version as well). Edit: I decided to just bump CI elixir version :) |
07ecdf5
to
f833e9a
Compare
Ecto already requires ~> 1.14
@@ -1377,21 +1425,29 @@ defmodule Ecto.Adapters.SQL do | |||
|
|||
@repo_modules [Ecto.Repo.Queryable, Ecto.Repo.Schema, Ecto.Repo.Transaction] | |||
|
|||
defp last_non_ecto([{mod, _, _, _} | _stacktrace], repo, last) | |||
def first_non_ecto_stacktrace(stacktrace, repo, size) do |
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.
We need to document this function. But also document the new option in Ecto
. But this function could have a full example of how to configure it and use it :)
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.
One last comment and ship it!
0ea86d4
to
da99c79
Compare
with [_ | _] <- stacktrace, | ||
{module, function, arity, info} <- last_non_ecto(Enum.reverse(stacktrace), repo, nil) do | ||
defp log_stacktrace([_ | _] = stacktrace, repo, {module, function, args}) do | ||
entries = apply(module, function, [stacktrace, repo | args]) |
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.
entries = apply(module, function, [stacktrace, repo | args]) | |
entries = apply(module, function, [stacktrace, %{repo: repo} | args]) |
As per the Ecto issue.
|> Enum.reverse() | ||
|> last_non_ecto_entries(repo, []) | ||
|> Enum.take(size) |
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.
I'm not sure how big these stacktraces can get. Is it better to pass a decrementing counter to last_non_ecto_entries
and reverse at the end? Or it doesn't really matter?
Co-authored-by: Greg Rychlewski <[email protected]>
Click to expand old description
A lot of times, more stack items can be helpful in identifying the source of a query.This PR demonstrates how we can support it. But the API has problems:
the
:stacktrace
option is used not only by logging but also for general telemetry events. Making it an integer doesn't make sense for telemetry (as the full stacktrace is published there anyway).the
:stacktrace
option is overriden Ecto-side, so Ecto.SQL can't accept integers on a per-query basis, only on global configuration.Adds a new option
:log_stacktrace_mfa
that can be used at configuration level or query level (like:log
, or:stacktrace
). This config allows filtering/prepping the stacktrace-derived data displayed in the logs. For example, one may want to filter other modules other than those that Ecto.SQL filters by default, or maybe just include more stack items.Sample usage: