I am working on a Phoenix project with Ecto and Postgres.
I want to know all the db queries related to an incoming request.
what I did:
I change the logger setting to add metadata so that it can log pid, and request_id.
but some queries are preload queries and it is triggered in different process. (because Ecto’s optimisation mechanism). How can I track them?
Thanks!
but some queries are preload queries and it is triggered in different process. (because Ecto’s optimisation mechanism)
I suppose you refer to ecto checking out a new connection for additional queries, even when they are causally related to some other queries that your app issues (preloads are dependent on the other resources that these preloads are for).
Sorry for being off-topic, but is it really an optimization? Wouldn’t it be more straightforward to run preloads on the same connection?
As for your question, maybe you can put the phoenix’s request_id
into the process dictionary for the cowboy’s process handing the http request, and pass it to all other processes (like ecto) as well?
Something like
# in your controller action
request_id = conn.private.phoenix.request_id # or wherever it is stored
:erlang.put(:request_id, request_id)
# then to let ecto know about the request_id as well
Repo.transaction(fn ->
# I suppose it's a different process, so we need to put the request id here as well
:erlang.put(:request_id, request_id)
# run your queries
end)
You can probably replace :erlang.put/2
with Logger.metadata
, but I’m not sure.
# in your controller action
request_id = conn.private.phoenix.request_id # or wherever it is stored
Logger.metadata(request_id: request_id)
# then to let ecto know about the request_id as well
Repo.transaction(fn ->
# I suppose it's a different process, so we need to put the request id here as well
Logger.metadata(request_id: request_id)
# run your queries
# ...
# and then
Logger.info("asfd") # should use request_id from before
end)
1 Like