diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index c6c6a8ac..84106b5b 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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` diff --git a/clj-otel-api/src/steffan_westcott/clj_otel/api/trace/http.clj b/clj-otel-api/src/steffan_westcott/clj_otel/api/trace/http.clj index 79601d95..78143dbd 100644 --- a/clj-otel-api/src/steffan_westcott/clj_otel/api/trace/http.clj +++ b/clj-otel-api/src/steffan_westcott/clj_otel/api/trace/http.clj @@ -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 @@ -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