Skip to content

Commit

Permalink
Refactor middleware: combine middlewares with or without patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
arttuka committed Oct 24, 2023
1 parent 3b44f93 commit 6f88b04
Showing 1 changed file with 7 additions and 31 deletions.
38 changes: 7 additions & 31 deletions src/clojure_elastic_apm/ring.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,16 @@
matched?)))))

(defn match-patterns [patterns uri]
(->> patterns
(map #(match-uri % uri))
(drop-while false?)
(first)))
(if patterns
(->> patterns
(map #(match-uri % uri))
(drop-while false?)
(first))
uri))

(defn wrap-apm-transaction
([handler]
(fn
([{:keys [request-method uri headers] :as request}]
(let [tx-name (str (.toUpperCase (name request-method)) " " uri)
traceparent (get-in headers ["traceparent"])]
(with-apm-transaction [tx {:name tx-name :type type-request :traceparent traceparent}]
(let [{:keys [status] :as response} (handler (assoc request :clojure-elastic-apm/transaction tx))]
(when status
(.setResult tx (str "HTTP " status)))
response))))
([{:keys [request-method uri headers] :as request} respond raise]
(let [tx-name (str (.toUpperCase (name request-method)) " " uri)
traceparent (get-in headers ["traceparent"])
tx (apm/start-transaction {:name tx-name :type type-request :traceparent traceparent})
req (assoc request :clojure-elastic-apm/transaction tx)]
(with-open [_ (apm/activate tx)]
(handler req
(fn [{:keys [status] :as response}]
(when status
(.setResult tx (str "HTTP " status)))
(apm/end tx)
(respond response))
(fn [err]
(when (instance? Exception err)
(apm/capture-exception tx err))
(apm/end tx)
(raise err))))))))
(wrap-apm-transaction handler nil))
([handler patterns]
(fn
([{:keys [request-method uri headers] :as request}]
Expand All @@ -68,7 +45,6 @@
response))
(handler request))))
([{:keys [request-method uri headers] :as request} respond raise]

(let [matched (match-patterns patterns uri)
tx-name (str (.toUpperCase (name request-method)) " " matched)
traceparent (get-in headers ["traceparent"])]
Expand Down

0 comments on commit 6f88b04

Please sign in to comment.