Skip to content
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

Ability to format logged queries #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danielytics
Copy link

Adds an optional key :log-query-formatter to the component, which takes a function (fn [query] ...) that gets passed each query before it gets logged, for allowing custom formatting of the logged query. This does not impact the execution of the query, only how it gets logged.

As briefly discussed in #3

I'm unsure of the key name and whether it should accept a vector of all queries, or each individual query like now. Feedback welcome.

@atomist
Copy link

atomist bot commented Sep 29, 2020

Commit messages of this repository should follow the seven rules of a great Git commit message, as mentioned in the project's contributing guidelines. It looks like there's a few issues with the commit messages in this pull request:

  • The commit message subject is over 50 characters.

@danielytics danielytics force-pushed the query-formatting branch 2 times, most recently from 707975c to adb73a5 Compare September 29, 2020 01:57
src/duct/database/sql/hikaricp.clj Outdated Show resolved Hide resolved
@danielytics danielytics force-pushed the query-formatting branch 3 times, most recently from 0a8c6c1 to ffb5213 Compare October 21, 2020 15:20
Copy link
Contributor

@weavejester weavejester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it too so long to get round to reviewing this. There are a few changes necessary that I've commented on.

@@ -18,32 +18,34 @@
(defn- logged-query [^QueryInfo query-info]
(let [query (.getQuery query-info)
params (query-parameter-lists query-info)]
(into [query] (if (= (count params) 1) (first params) params))))
(into [query] (if (= (count params) 1) (first params) params))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a space was accidentally deleted here, misaligning the indentation.

(is (empty? @logs))))

(defn log-query-formatter [[query & params]]
(into
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nonstandard indentation, and also short enough to put on one line. Could you format as:

  (into [(str (re-find #"^\w+" query) "...")] params)

(deftest formatted-logging-test
(let [logs (atom [])
logger (->AtomLogger logs)
hikaricp (ig/init-key ::sql/hikaricp {:jdbc-url "jdbc:sqlite:" :logger logger :log-query-formatter log-query-formatter})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line longer than 80 characters.

(hikari-cp/make-datasource)
(cond-> logger (wrap-logger logger)))}))
(hikari-cp/make-datasource))]
(if logger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use an if here, instead of tacking the extra assoc onto the end of the cond->?

Copy link
Author

@danielytics danielytics Aug 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason was so that it only runs:

(assoc boundary :unlogged-spec {:datasource datasource})

when a logger is set. Perhaps just always set unlogged-spec, regardless of if a logger is specified or not?

Something like this:

(let [datasource (-> (dissoc options :logger)
                     (assoc :jdbc-url (or jdbc-url connection-uri))
                     (hikari-cp/make-datasource))]
    (-> (sql/->Boundary {:datasource (cond-> datasource logger (wrap-logger logger log-query-formatter))})
        (assoc :unlogged-spec {:datasource datasource})))

PS: I'm not sure how you want it formatted, I'm having a hard time the wrap-logger line under 80 characters without introducing another binding in the let. Perhaps this:

(defmethod ig/init-key :duct.database.sql/hikaricp
  [_ {:keys [logger connection-uri jdbc-url log-query-formatter]
      :or {log-query-formatter identity} :as options}]
  (let [datasource (-> (dissoc options :logger)
                       (assoc :jdbc-url (or jdbc-url connection-uri))
                       (hikari-cp/make-datasource))
        logged-datasource (cond-> datasource
                            logger (wrap-logger logger log-query-formatter))]
    (assoc (sql/->Boundary {:datasource logged-datasource})
           :unlogged-spec {:datasource datasource})))

Copy link
Contributor

@weavejester weavejester Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now. Then perhaps let's factor out some of this into a separate function for clarity:

(defn- logged-boundary [datasource {:keys [logger log-query-formatter]}]
  (let [logged-source (wrap-logger datasource logger log-query-formatter)]
    (-> (sql/->Boundary {:datasource logged-source})
        (assoc :unlogged-spec {:datasource datasource}))))

(defmethod ig/init-key :duct.database.sql/hikaricp
  [_ {:keys [logger connection-uri jdbc-url logger] :as options}]
  (let [datasource (-> (dissoc options :logger)
                       (assoc :jdbc-url (or jdbc-url connection-uri))
                       (hikari-cp/make-datasource))]
    (if logger
      (logged-boundary data-source options)
      (sql/->Boundary {:datasource datasource}))))

What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good to me. I'll push this and the other changes soon.

[_ {:keys [logger connection-uri jdbc-url] :as options}]
(sql/->Boundary {:datasource
(-> (dissoc options :logger)
[_ {:keys [logger connection-uri jdbc-url log-query-formatter] :as options}]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a default value for log-query-formatter as identity here would prevent a need for the (or query-formatter identity) later on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants