chenfei-io

chenfei-io

How to label all the db queries related to a http request in log

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!

Most Liked

idi527

idi527

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)

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement