honeysql

slipset 2024-07-03T07:48:08.873969Z

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]))

p-himik 2024-07-03T07:50:37.938939Z

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?

slipset 2024-07-03T07:51:25.934369Z

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

slipset 2024-07-03T07:52:40.760199Z

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.

👍 1
igrishaev 2024-07-03T09:47:03.288529Z

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*)])

slipset 2024-07-03T09:52:32.993969Z

Yeah, there are many ways to skin a 🐈 , the main trick is inserting the comments.

igrishaev 2024-07-03T10:05:12.645189Z

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

p-himik 2024-07-03T10:40:22.190509Z

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.