Nothing more than following up on something I think I asked about years ago. Adding tracing to our Postgres logs:
(honey/register-clause! :trace (fn [_ trace-id]
[(str "/* trace-id:" trace-id " */")]) :alter-table))
and
(defn trace [m trace-id]
(util/ensure! trace-id
#(clojure.core/or (specs/uuid? %)
(= core/unknown-trace-id %))
(str "trace-id must be a uuid or " core/unknown-trace-id " got "
(log/quote trace-id)))
(hh/generic-helper-unary :trace [m trace-id]))
How does that tracing work?
Or is it simply something you trace in your own code and PostgreSQL doesn't care about that trace-id at all?
The last bit, so we track our postgres logs in ELK, and now we have a correlation point between our own code and the postgres logs
So any request that comes in to our api gets a trace-id which is a uuid and is set on the logging context, so that all logs for the same request can be correlated. Now we extend that correlation to the postgres logs as well.
Interesting: to include a uuid into sql queries as a comment!
Just an idea: there might be a global dynamic var like *sql-trace-id* and a macro
(with-trace-id
...)
which binds it with a fresh uuid; when you render sql queries, you add a commented part from the dynamic var:
(let [sql (str sql "-- trace id: " *sql-trace-id*)])Yeah, there are many ways to skin a 🐈 , the main trick is inserting the comments.
thank you for sharing this, now I'm thinking about the same approach to tag SQL queries for further mapping b/w HTTP request and DB calls that it has produced
One other thing worth mentioning - PostgreSQL allows for arbitrary transaction configuration variables.
So e.g. you can SET the_project.trace_id = 7; somewhere and that value will be available within the same transaction. Useful when you need to e.g. also trace queries made by triggers.