Skip to content

Commit

Permalink
feat: use context on logs
Browse files Browse the repository at this point in the history
  • Loading branch information
J0sueTM committed Jun 21, 2024
1 parent 5da4cbb commit 5590377
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
19 changes: 10 additions & 9 deletions src/com/moclojer/components/database.clj
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
(ns com.moclojer.components.database
(:require
[com.stuartsierra.component :as component]
[com.moclojer.components.db-utils :refer [to-jdbc-uri]]
[com.moclojer.components.logs :as logs]
[com.stuartsierra.component :as component]
[next.jdbc :as jdbc]
[next.jdbc.connection :as connection])
(:import
(com.zaxxer.hikari HikariDataSource)))

;; https://github.com/seancorfield/next-jdbc/blob/develop/doc/getting-started.md#logging-sql-and-parameters
(defn db-logger
"Simple logger for debugging purposes."
[operation query]
(logs/log :info (str "executing database operation " operation)
:ctx {:query query}))
(defn build-db-logger
"Builds a simple logger for debugging purposes."
[ctx]
(fn [operation query]
(logs/log :info operation
:ctx (assoc ctx :query query))))

(defprotocol DatabaseProvider
(execute [self command]
(execute [self command ctx]
"Low-level API to execute a command in the database"))

(defrecord Database [config ^HikariDataSource datasource]
Expand All @@ -36,7 +37,7 @@
this))

DatabaseProvider
(execute [this commands]
(execute [this commands ctx]
(let [ds (:datasource this)
log-ds (jdbc/with-logging ds db-logger)]
log-ds (jdbc/with-logging ds (build-db-logger ctx))]
(jdbc/execute! log-ds commands))))
64 changes: 32 additions & 32 deletions src/com/moclojer/components/http.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(:require
[clj-http.client :as http]
[clj-http.util :as http-util]
[com.stuartsierra.component :as component]
[com.moclojer.components.logs :as logs]))
[com.moclojer.components.logs :as logs]
[com.stuartsierra.component :as component]))

(defn request-fn
"Accepts :req which should be a map containing the following keys:
Expand All @@ -25,9 +25,9 @@

(defprotocol HttpProvider
(request
[this request-input])
[this request-input ctx])
(request-or-throw
[this req expected-status]))
[this req expected-status ctx]))

(defrecord Http [_]
component/Lifecycle
Expand All @@ -36,39 +36,39 @@

HttpProvider
(request
[_ {:keys [method url] :as request-input}]
[_ {:keys [method url] :as request-input} ctx]
(logs/log :info "sending http request"
:ctx {:method method
:url url})
:ctx (merge ctx {:method method
:url url}))
(let [start-time (System/currentTimeMillis)
{:keys [status] :as response} (request-fn request-input)
end-time (System/currentTimeMillis)
total-time (- end-time start-time)]
(logs/log :info "received http response"
:ctx {:response-time-millis total-time
:status status})
:ctx (merge ctx {:response-time-millis total-time
:status status}))
response))
(request-or-throw
[this req expected-status]
(let [{:keys [status] :as resp} (request this req)]
[this req expected-status ctx]
(let [{:keys [status] :as resp} (request this req ctx)]
(if (= status expected-status)
resp
(do
(logs/log :error "failed critical request. throwing..."
:ctx {:url (:url req)
:status status
:expected-status expected-status})
:ctx (merge ctx {:url (:url req)
:status status
:expected-status expected-status}))
(throw (ex-info "failed critical request"
{:status status
:expected-status expected-status
:req req
:resp resp})))))))
(merge ctx {:status status
:expected-status expected-status
:req req
:resp resp}))))))))

(comment
(def hp (component/start (->Http {})))

(request hp {:url "https://google.com"
:method :get})
:method :get} {})

(component/stop hp)
;;
Expand All @@ -82,7 +82,7 @@

HttpProvider
(request
[this req]
[this req ctx]

(let [mreq (select-keys req [:method :url])
resp (-> #(= mreq (select-keys % (keys mreq)))
Expand All @@ -91,26 +91,26 @@
(or {:status 500
:body "mocked response not set"}))]
(logs/log :info "sending http request"
:ctx mreq)
:ctx (merge ctx mreq))

(assoc
(if (fn? resp) (resp req) resp)
:instant (System/currentTimeMillis))))
(request-or-throw
[this req expected-status]
(let [{:keys [status] :as resp} (request this req)]
[this req expected-status ctx]
(let [{:keys [status] :as resp} (request this req ctx)]
(if (= status expected-status)
resp
(do
(logs/log :error "failed critical request. throwing..."
:ctx {:url (:url req)
:status status
:expected-status expected-status})
:ctx (merge ctx {:url (:url req)
:status status
:expected-status expected-status}))
(throw (ex-info "failed critical request"
{:status status
:expected-status expected-status
:req req
:resp resp})))))))
(merge ctx {:status status
:expected-status expected-status
:req req
:resp resp}))))))))

(comment
(def m (component/start
Expand All @@ -125,13 +125,13 @@
{:status 200
:body (:body req)})}])))

(request m {:url "https://test.com"})
(request m {:url "https://test.com"} {})
;; => {:status 200
;; :body "hello world"
;; :instant 1715218184868}

(request m {:url "https://test.com"
:method :put
:body "hello"})
:body "hello"} {})
;;
)
14 changes: 9 additions & 5 deletions src/com/moclojer/components/logs.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns com.moclojer.components.logs
(:require [clojure.string :as str]
[taoensso.timbre :as timbre]
[taoensso.timbre.appenders.core :as core-appenders]
[timbre-json-appender.core :as tas])
(:import [java.util.logging Filter Handler Logger]))
(:require
[taoensso.timbre :as timbre]
[taoensso.timbre.appenders.core :as core-appenders]
[timbre-json-appender.core :as tas])
(:import
[java.util.logging Filter Handler Logger]))

(defn clean-dep-logs
"clean logs on prod that are not from our application"
Expand Down Expand Up @@ -51,6 +52,9 @@
(defmacro log [level & args]
`(timbre/log ~level ~@args))

(defn gen-ctx-with-cid []
{:cid (str "cid-" (random-uuid) "-" (System/currentTimeMillis))})

(comment
(setup [["*" :info]] :auto :prod)
(log :info :world)
Expand Down
2 changes: 1 addition & 1 deletion src/com/moclojer/components/router.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns com.moclojer.components.router
(:require
[com.stuartsierra.component :as component]
[com.moclojer.components.logs :as logs]
[com.moclojer.components.sentry :as sentry]
[com.stuartsierra.component :as component]
[muuntaja.core :as m]
[reitit.coercion.malli :as reitit.malli]
[reitit.dev.pretty :as pretty]
Expand Down
22 changes: 9 additions & 13 deletions src/com/moclojer/components/storage.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
(get-file [this bucket-name filename])
(list-files [this bucket-name])
(upload!
[this bucket-name k value]
[this bucket-name k value cp]))
[this bucket-name k value ctx]
[this bucket-name k value cp ctx]))

(defn assoc-if [m k v]
(if v
Expand Down Expand Up @@ -78,19 +78,15 @@
:request {:Bucket bucket-name}})
:Contents))

(upload! [this bucket-name filename value]
(logs/log :info "uploading to storage"
:ctx {:bucket bucket-name
:filename filename
:value value})
(upload! this bucket-name filename value "application/yml"))
(upload! [this bucket-name filename value ctx]
(upload! this bucket-name filename value "application/yml" ctx))

(upload! [this bucket-name filename value content-type]
(upload! [this bucket-name filename value content-type ctx]
(logs/log :info "uploading to storage"
:ctx {:bucket bucket-name
:filename filename
:content-type content-type
:value value})
:ctx (merge ctx {:bucket bucket-name
:filename filename
:content-type content-type
:value value}))
(-> (-> this :storage)
(aws/invoke {:op :PutObject
:Content-Type content-type
Expand Down
5 changes: 2 additions & 3 deletions src/com/moclojer/components/webserver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
::log-request
(fn [req]
(logs/log
:info (str (str/upper-case (:request-method req))
:info (str (str/lower-case (:request-method req))
" "
(:uri req))
:ctx {:query (:query-string req)})

:ctx req)
req)))

(defn base-service [port]
Expand Down

0 comments on commit 5590377

Please sign in to comment.