Skip to content

Commit

Permalink
Add Compojure route data support
Browse files Browse the repository at this point in the history
- Added middleware `wrap-compojure-route` to add Compojure route data
  to server spans.
- Allow `route-fn` in `wrap-route` to return nil if no rote data found.
  This isn't necessary for the Reitit and Compojure support, but helps
  make `wrap-route` more robust.
  • Loading branch information
steffan-westcott committed Dec 24, 2023
1 parent 28f9be0 commit 39a8ef5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ WARNING: Until version `1.0.0` there is a greater possibility of breaking change
- [ADD] Terse syntax alternatives for `span-opts` map parameter in `new-span!` and related functions that create spans.
- [ADD] Convenience function `add-event!` to add an event to the bound or current span.
- [ADD] Populate `err.type` trace semantic attribute on manually created client and server spans.
- [ADD] Support for adding Compojure route data to HTTP server spans.
- [MAINT] Ensure example microservices uberjars are built and run on the same JDK.
- Bump deps
* [MAINT] OpenTelemetry `1.33.0`
Expand Down
40 changes: 27 additions & 13 deletions clj-otel-api/src/steffan_westcott/clj_otel/api/trace/http.clj
Original file line number Diff line number Diff line change
Expand Up @@ -333,25 +333,29 @@
(defn wrap-route
"Ring middleware to add a matched route to the server span data and Ring
request map. `route-fn` is a function which given a request returns the
matched route as a string."
matched route as a string, or nil if no match."
[handler route-fn]
(fn
([{:keys [request-method]
:as request}]
(let [route (route-fn request)]
(add-route-data! request-method route)
(handler (assoc-in request
[:io.opentelemetry/server-request-attrs SemanticAttributes/HTTP_ROUTE]
route))))
(if-let [route (route-fn request)]
(do
(add-route-data! request-method route)
(handler (assoc-in request
[:io.opentelemetry/server-request-attrs SemanticAttributes/HTTP_ROUTE]
route)))
(handler request)))
([{:keys [request-method io.opentelemetry/server-span-context]
:as request} respond raise]
(let [route (route-fn request)]
(add-route-data! request-method route {:context server-span-context})
(handler (assoc-in request
[:io.opentelemetry/server-request-attrs SemanticAttributes/HTTP_ROUTE]
route)
respond
raise)))))
(if-let [route (route-fn request)]
(do
(add-route-data! request-method route {:context server-span-context})
(handler (assoc-in request
[:io.opentelemetry/server-request-attrs SemanticAttributes/HTTP_ROUTE]
route)
respond
raise))
(handler request respond raise)))))

(defn wrap-reitit-route
"Ring middleware to add matched Reitit route to the server span and Ring
Expand All @@ -362,6 +366,16 @@
(fn [request]
(get-in request [:reitit.core/match :template]))))

(defn wrap-compojure-route
"Ring middleware to add matched Compojure route to the server span and Ring
request map. Use `compojure.core/wrap-routes` to apply this middleware to
all route handlers."
[handler]
(wrap-route handler
(fn [{prefix :compojure/route-context
[_ path] :compojure/route}]
(str prefix path))))

;;; Pedestal interceptors

(defn- server-request-attrs-interceptor
Expand Down

0 comments on commit 39a8ef5

Please sign in to comment.