Skip to content
Permalink
Browse files

Add function to override URI

  • Loading branch information...
diogok committed Aug 13, 2019
1 parent 5535f0a commit 6d2d1961446514a4f52bfdcf967f58b121c0c8ea
Showing with 34 additions and 8 deletions.
  1. +12 −0 README.md
  2. +2 −2 project.clj
  3. +7 −6 src/new_reliquary/ring.clj
  4. +13 −0 test/new_reliquary/ring_test.clj
@@ -98,6 +98,18 @@ This can be achieved easily by using `ring.middleware.params/wrap-params`.
(wrap-params)))
```
Example with custom uri function:
```clojure
(ns new-reliquary-example.main
(:require [new-reliquary.ring :refer [wrap-newrelic-transaction]]
[ring.middleware.params :refer [wrap-params]]))
(defn request-handler [request] {:body "Hello world"})
(def app (-> request-handler
(wrap-newrelic-transaction :key-fn #(.toUpperCase (:uri %)))
(wrap-params)))
```
## License
@@ -1,10 +1,10 @@
(defproject yleisradio/new-reliquary "1.1.0"
(defproject yleisradio/new-reliquary "1.2.0"
:description "Clojure newrelic java api wrapper"
:url "https://github.com/Yleisradio/new-reliquary"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0"]
[com.newrelic.agent.java/newrelic-api "4.0.0"]]
[com.newrelic.agent.java/newrelic-api "5.3.0"]]
:profiles { :dev { :dependencies [[ring/ring-core "1.6.3"]
[ring-mock "0.1.5"]
[org.clojure/tools.trace "0.7.9"]]}}
@@ -65,8 +65,8 @@
(first)
(second)))

(deftype NewRelicRingWrapperRequest [req] Request
(getRequestURI [_] (resolve-uri req))
(deftype NewRelicRingWrapperRequest [req key-fn] Request
(getRequestURI [_] (key-fn req))
(getRemoteUser [_] nil)
(getParameterNames [_] (keys (resolve-query-params req)))
(getParameterValues [_ name]
@@ -86,14 +86,15 @@
(doseq [[key value] (sort-by key (seq params))]
(newrelic/add-custom-parameter key value)))

(defn- web-transaction [request-hander request]
(defn- web-transaction [request-hander request key-fn]
(fn []
(let [newrelic-req (NewRelicRingWrapperRequest. request)
(let [newrelic-req (NewRelicRingWrapperRequest. request key-fn)
response (request-hander request)
newrelic-res (NewRelicRingWrapperResponse. response)]
(add-query-params (resolve-query-params request))
(newrelic/set-request-response newrelic-req newrelic-res)
response)))

(defn wrap-newrelic-transaction [handler]
(fn [request] (newrelic/with-newrelic-transaction (web-transaction handler request))))
(defn wrap-newrelic-transaction [handler & args]
(let [kv-args (apply hash-map args)]
(fn [request] (newrelic/with-newrelic-transaction (web-transaction handler request (or (kv-args :key-fn) resolve-uri))))))
@@ -13,6 +13,13 @@
(ring/wrap-newrelic-transaction)
(wrap-params)))

(defn build-app-key-fn [status content-type]
(-> (fn [req] {:body req
:headers {"Content-Type" content-type}
:status status})
(ring/wrap-newrelic-transaction :key-fn #(.toUpperCase (:uri %)))
(wrap-params)))

(def set-request-response-calls (atom []))
(def set-transaction-name-calls (atom []))
(def add-custom-parameter-calls (atom []))
@@ -88,3 +95,9 @@
(app (request :get "http://test.fi/dogs"))
(is (= (get-newrelic-response-content-types) ["text/xml"]))
(is (= (get-newrelic-response-statuses) [404])))))

(deftest with-custom-key-fn
(testing "tracks response status and content type"
(let [app (build-app-key-fn 404 "text/xml")]
(app (request :get "http://test.fi/dogs"))
(is (= (get-newrelic-request-urls) ["/DOGS"])))))

0 comments on commit 6d2d196

Please sign in to comment.
You can’t perform that action at this time.