Skip to content

Instantly share code, notes, and snippets.

@practicalli-johnny
Last active June 21, 2022 13:12
Show Gist options
  • Save practicalli-johnny/2ab85ba6d3b1ce8255779ba78914d54b to your computer and use it in GitHub Desktop.
Save practicalli-johnny/2ab85ba6d3b1ce8255779ba78914d54b to your computer and use it in GitHub Desktop.
(ns practicalli.app.middleware
(:require
[com.brunobonacci.mulog :as mulog]))
;;;; Logging middleware
;; https://github.com/BrunoBonacci/mulog/blob/master/doc/ring-tracking.md
(defn wrap-trace-events
"Log event trace for each api event with mulog/log."
[handler id]
(fn [request]
;; Add context of each request to all trace events generated for the specific request
(mulog/with-context
{:uri (get request :uri)
:request-method (get request :request-method)}
;; track the request duration and outcome
(mulog/trace :io.redefine.datawarp/http-request
;; add key/value pairs for tracking event only
{:pairs [:content-type (get-in request [:headers "content-type"])
:content-encoding (get-in request [:headers "content-encoding"])
:middleware id]
;; capture http status code from the response
:capture (fn [{:keys [status]}] {:http-status status})}
;; call the request handler
(handler request)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment