   (if (keyword? x) (str "{" (name x) "}") x))
 
 (defn swagger-path-to-str
-  ([[x]] x)
-  ([[x & xs]] (str "/" (path-to-str x) (recur xs))))
+  ([paths] (swagger-path-to-str paths ""))
+  ([paths path]
+   (if (empty? paths)
+     path
+     (swagger-path-to-str (butlast paths) (str "/" (path-to-str (last paths)) path)))))
 
 (defn swagger-path [request]
-  ; first is method, second is path array
-  (-> request (get-in :swagger :key) second))
+  (swagger-path-to-str
+    ; first is method, second is path array
+    (-> request (get-in [:swagger :key]) second)))
 
 (defn swagger-parameters [request]
   ; filter out body parameters and merge to flat map

(ns io.sarnowski.swagger1st.util.newrelic
  (:import [com.newrelic.agent NewRelic Trace]))

(definterface NewRelicTraceable
  (callWithTrace [transaction-name custom-parameters callback]))

(deftype NewRelicTracer []
  NewRelicTraceable
    ; create a method with the @Trace annotation
    (^{Trace {:dispatcher true}}
      callWithTrace [_ transaction-name custom-parameters callback]
        ; set up transaction name
        (NewRelic/setTransactionName nil transaction-name)
        ; add all custom parameters
        (doseq [[key val] (sort-by key custom-parameters)]
          (NewRelic/addCustomParameter (name key) (str val)))
        ; call the actual function
        (callback)))

(def newrelic-tracer (NewRelicTracer.))


(defn tracer
  "A swagger middleware that configures NewRelic agents to track all operations as transactions."
  [context]
  (s1st/chain-handler
    context
    :on-request (fn [context next-handler request]
                  (let [transaction-name (swagger-path request)
                        custom-parameters (swagger-parameters request)]
                    (.callWithTrace newrelic-tracer
                                    transaction-name
                                    custom-parameters
                                    request
                                    #(next-handler request))))))
