Skip to content

Commit 4e46a44

Browse files
committed
Grab cookie header from the map of request headers
Fixes #70 -=david=-
1 parent 83f02dd commit 4e46a44

File tree

7 files changed

+93
-54
lines changed

7 files changed

+93
-54
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ endeavour to be non-breaking (by moving to new names rather than by
1010
breaking existing names). COMMITS is an ever-increasing counter of
1111
commits since the beginning of this repository.
1212

13+
## [8.20.229]
14+
15+
- Grab cookie header from the map of request headers. Fixes #70
16+
1317
## [8.20.228]
1418

1519
- Update Sentry Java SDK to 8.20.0
@@ -448,7 +452,8 @@ commits since the beginning of this repository.
448452
compatible with Sentry 10.0.1 and below. If you wish to use those
449453
versions, please continue to use sentry-clj 1.7.30.
450454

451-
[Unreleased]: https://github.com/getsentry/sentry-clj/compare/8.20.228...HEAD
455+
[Unreleased]: https://github.com/getsentry/sentry-clj/compare/8.20.229...HEAD
456+
[8.20.229]: https://github.com/getsentry/sentry-clj/compare/8.20.228...8.20.229
452457
[8.20.228]: https://github.com/getsentry/sentry-clj/compare/7.22.227...8.20.228
453458
[7.22.227]: https://github.com/getsentry/sentry-clj/compare/7.20.226...7.22.227
454459
[7.20.226]: https://github.com/getsentry/sentry-clj/compare/7.19.225...7.20.226

deps.edn

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
com.github.seancorfield/expectations {:mvn/version "2.2.214"}
2121
lambdaisland/kaocha {:mvn/version "1.91.1392"}
2222
lambdaisland/kaocha-junit-xml {:mvn/version "1.17.101"}
23-
orchestra/orchestra {:mvn/version "2021.01.01-1"}
24-
org.clojure/test.check {:mvn/version "1.1.1"}}}
23+
org.clojure/test.check {:mvn/version "1.1.1"}
24+
ring/ring-mock {:mvn/version "0.6.2"}}}
2525

2626
:antq {:replace-deps {com.github.liquidz/antq {:mvn/version "2.11.1276"}
2727
org.slf4j/slf4j-nop {:mvn/version "2.0.17"}}
@@ -30,7 +30,6 @@
3030
:exec-args {:download true
3131
:force true
3232
:no-changes true
33-
:skip ["pom"]
3433
:upgrade true
3534
:verbose true}}
3635

examples/ring_with_tracing/src/ring_with_tracing/main.clj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@
44
[ring.adapter.jetty :refer [run-jetty]]
55
[ring.middleware.json :refer [wrap-json-params wrap-json-response]]
66
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
7+
[ring.middleware.params :refer [wrap-params]]
78
[sentry-clj.core :as sentry]
89
[sentry-clj.ring :refer [wrap-report-exceptions wrap-sentry-tracing]]
910
[sentry-clj.tracing :refer [with-start-child-span]]))
1011

1112
(def config
1213
{:adapter/jetty {:port 8080 :handler (ig/ref :handler/router)}
13-
:handler/router {:dsn "http://da98da654fbc48f901ef3ff7cb173e4e@localhost:9000/1" :app (ig/ref :handler/hello)}
14+
;;
15+
;; Change the DSN below to suit your particular setup.
16+
;;
17+
:handler/router {:dsn "http://6b3a553a3c70d550ff5dea8c4a646b9f@localhost:9000/1" :app (ig/ref :handler/hello)}
1418
:handler/hello {:name "Sentry"}})
1519

1620
(defmethod ig/init-key :adapter/jetty [_ {:keys [handler] :as opts}]
1721
(run-jetty handler (-> opts (dissoc :handler) (assoc :join? false))))
1822

1923
(defmethod ig/init-key :handler/hello [_ {:keys [name]}]
20-
(fn [_request]
24+
(fn [{:keys [query-params] :as _request}]
25+
26+
;; use ?throw=true to trigger an exception to thrown
27+
(when (get query-params "throw")
28+
(throw (RuntimeException. "I'm being exceptional")))
2129

2230
(with-start-child-span "task" "my-child-operation"
2331
(Thread/sleep 1000))
@@ -31,6 +39,7 @@
3139
(-> app
3240
(wrap-report-exceptions {})
3341
(wrap-sentry-tracing)
42+
(wrap-params)
3443
(wrap-keyword-params)
3544
(wrap-json-params)
3645
(wrap-json-response)))

src/sentry_clj/core.clj

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,46 @@
5454
(defn ^:private map->user
5555
"Converts a map into a User."
5656
^User
57-
[{:keys [email id username ip-address data]}]
57+
[{:keys [data email id ip-address username]}]
5858
(let [user (User.)]
59+
(when data
60+
(.setData user data))
5961
(when email
6062
(.setEmail user email))
6163
(when id
6264
(.setId user id))
63-
(when username
64-
(.setUsername user username))
6565
(when ip-address
6666
(.setIpAddress user ip-address))
67-
(when data
68-
(.setData user data))
67+
(when username
68+
(.setUsername user username))
6969
user))
7070

7171
(defn ^:private map->request
7272
"Converts a map into a Request."
7373
^Request
74-
[{:keys [url method query-string data cookies headers env other]}]
74+
[{:keys [data env headers method other query-string url] :as _request}]
7575
(let [request (Request.)]
76-
(when url
77-
(.setUrl request url))
78-
(when method
79-
(.setMethod request method))
80-
(when query-string
81-
(.setQueryString request query-string))
8276
(when data
8377
(.setData request (java-util-hashmappify-vals data)))
84-
(when cookies
85-
(.setCookies request (java-util-hashmappify-vals cookies)))
86-
(when headers
87-
(.setHeaders request (java-util-hashmappify-vals headers)))
8878
(when env
8979
(.setEnvs request (java-util-hashmappify-vals env)))
80+
(when headers
81+
(.setHeaders request (java-util-hashmappify-vals headers))
82+
(when-let [cookie (get headers "cookie" (get headers "Cookie"))]
83+
(.setCookies request cookie)))
84+
(when method
85+
(.setMethod request method))
9086
(when other
9187
(.setOthers request (java-util-hashmappify-vals other)))
88+
(when query-string
89+
(.setQueryString request query-string))
90+
(when url
91+
(.setUrl request url))
9292
request))
9393

9494
(defn ^:private merge-all-ex-data
9595
"Merges ex-data of all ex-info exceptions in the cause chain of exn into extra.
96-
Each ex-data is added under a separate key so that they don't clobber each other."
96+
Each ex-data is added under a separate key so that they don't clobber each other."
9797
[extra exn]
9898
(loop [exn exn
9999
num 0
@@ -103,18 +103,13 @@
103103
(recur (ex-cause exn)
104104
(inc num)
105105
(cond-> extra
106-
data
107-
(assoc (if (zero? num)
108-
"ex-data"
109-
(str "ex-data, cause " num ": " (ex-message exn)))
110-
data))))
106+
data (assoc (if (zero? num) "ex-data" (str "ex-data, cause " num ": " (ex-message exn))) data))))
111107
extra)))
112108

113109
(defn ^:private map->event
114110
"Converts a map into an event."
115111
^SentryEvent
116-
[{:keys [event-id message level release environment user request logger platform dist
117-
tags breadcrumbs server-name extra fingerprints throwable transaction]}]
112+
[{:keys [event-id message level release environment user request logger platform dist tags breadcrumbs server-name extra fingerprints throwable transaction] :as _event}]
118113
(let [sentry-event (SentryEvent. (DateUtils/getCurrentDateTime))
119114
updated-message (if (string? message) {:message message} message)]
120115
(when event-id

src/sentry_clj/ring.clj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
(defn ^:private request->http
1818
"Converts a Ring request into an HTTP interface for an event."
1919
[req]
20-
{:url (request-url req)
20+
{:data (:params req)
21+
:env {:session (-> req :session pr-str) "REMOTE_ADDR" (:remote-addr req)}
22+
:headers (:headers req)
2123
:method (-> req :request-method name)
22-
:data (:params req)
2324
:query-string (:query-string req "")
24-
:headers (:headers req)
25-
:env {:session (-> req :session pr-str) "REMOTE_ADDR" (:remote-addr req)}})
25+
:url (request-url req)})
2626

2727
(defn ^:private configure-scope!
2828
"Set a scopes callback function which is called
@@ -58,11 +58,11 @@
5858
(defn ^:private request->context-request
5959
"Converts a request into custom-sampling-context's request."
6060
[req]
61-
{:uri (request-url req)
62-
:query-string (:query-string req "")
63-
:method (-> req :request-method name upper-case)
61+
{:data (-> req :params)
6462
:headers (:headers req)
65-
:data (-> req :params)})
63+
:query-string (:query-string req "")
64+
:request-method (-> req :request-method name upper-case)
65+
:uri (request-url req)})
6666

6767
(defn ^:private compute-sentry-runtime
6868
"Compute Clojure runtime information."

test/sentry_clj/core_test.clj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
:method "GET"
4949
:query-string "?foo=bar"
5050
:cookies "cookie1=foo;cookie2=bar"
51-
:headers {"X-Clacks-Overhead" "Terry Pratchett"
51+
:headers {"Cookie" "cookie1=foo;cookie2=bar"
52+
"X-Clacks-Overhead" "Terry Pratchett"
5253
"X-w00t" "ftw!"}
5354
:env {"a" "b"}
5455
:data {"c" "d"}
@@ -102,8 +103,8 @@
102103
(let [request ^Request (#'sut/map->request {:url "http://example.com"
103104
:method "GET"
104105
:query-string "?foo=bar"
105-
:cookies "cookie1=foo;cookie2=bar"
106-
:headers {"X-Clacks-Overhead" "Terry Pratchett"
106+
:headers {"Cookie" "cookie1=foo;cookie2=bar"
107+
"X-Clacks-Overhead" "Terry Pratchett"
107108
"X-w00t" "ftw!"}
108109
:env {"a" "b"}
109110
:data {"c" "d"}
@@ -112,7 +113,7 @@
112113
(expect "GET" (.getMethod request))
113114
(expect "?foo=bar" (.getQueryString request))
114115
(expect "cookie1=foo;cookie2=bar" (.getCookies request))
115-
(expect {"X-Clacks-Overhead" "Terry Pratchett" "X-w00t" "ftw!"} (.getHeaders request))
116+
(expect {"Cookie" "cookie1=foo;cookie2=bar" "X-Clacks-Overhead" "Terry Pratchett" "X-w00t" "ftw!"} (.getHeaders request))
116117
(expect {"a" "b"} (.getEnvs request))
117118
(expect {"c" "d"} (.getData request))
118119
(expect {"x" "y"} (.getOthers request)))))
@@ -144,7 +145,7 @@
144145
"username" "username"}
145146
"request" {"cookies" "cookie1=foo;cookie2=bar"
146147
"env" {"a" "b"}
147-
"headers" {"X-Clacks-Overhead" "Terry Pratchett", "X-w00t" "ftw!"}
148+
"headers" {"Cookie" "cookie1=foo;cookie2=bar" "X-Clacks-Overhead" "Terry Pratchett", "X-w00t" "ftw!"}
148149
"method" "GET"
149150
"data" {"c" "d"}
150151
"other" {"x" "y"}
@@ -183,7 +184,7 @@
183184
"username" "username"}
184185
"request" {"cookies" "cookie1=foo;cookie2=bar"
185186
"env" {"a" "b"}
186-
"headers" {"X-Clacks-Overhead" "Terry Pratchett", "X-w00t" "ftw!"}
187+
"headers" {"Cookie" "cookie1=foo;cookie2=bar" "X-Clacks-Overhead" "Terry Pratchett", "X-w00t" "ftw!"}
187188
"method" "GET"
188189
"data" {"c" "d"}
189190
"other" {"x" "y"}
@@ -229,7 +230,7 @@
229230
"username" "username"}
230231
"request" {"cookies" "cookie1=foo;cookie2=bar"
231232
"env" {"a" "b"}
232-
"headers" {"X-Clacks-Overhead" "Terry Pratchett", "X-w00t" "ftw!"}
233+
"headers" {"Cookie" "cookie1=foo;cookie2=bar" "X-Clacks-Overhead" "Terry Pratchett", "X-w00t" "ftw!"}
233234
"method" "GET"
234235
"data" {"c" "d"}
235236
"other" {"x" "y"}
@@ -278,7 +279,7 @@
278279
"username" "username"}
279280
"request" {"cookies" "cookie1=foo;cookie2=bar"
280281
"env" {"a" "b"}
281-
"headers" {"X-Clacks-Overhead" "Terry Pratchett", "X-w00t" "ftw!"}
282+
"headers" {"Cookie" "cookie1=foo;cookie2=bar" "X-Clacks-Overhead" "Terry Pratchett", "X-w00t" "ftw!"}
282283
"method" "GET"
283284
"data" {"c" "d"}
284285
"other" {"x" "y"}

test/sentry_clj/ring_test.clj

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
(:require
33
[cheshire.core :as json]
44
[expectations.clojure.test :refer [defexpect expect expecting side-effects]]
5+
[ring.middleware.cookies :as cookies-middleware]
6+
[ring.mock.request :as mock]
57
[sentry-clj.core :as sentry]
68
[sentry-clj.ring :as ring]
79
[sentry-clj.tracing :as st])
@@ -28,9 +30,9 @@
2830

2931
(defn ^:private wrapped
3032
[req]
31-
(if (:ok req) "woo" (throw e)))
33+
(if (:ok req) "w00t" (throw e)))
3234

33-
(def ^:private req
35+
(def ^:private hello-world-request
3436
{:scheme :https
3537
:uri "/hello-world"
3638
:request-method :get
@@ -55,7 +57,7 @@
5557
(defexpect wrap-report-exceptions-test
5658
(expecting "passing through"
5759
(let [handler (ring/wrap-report-exceptions wrapped {})]
58-
(expect "woo" (handler (assoc req :ok true))))))
60+
(expect "w00t" (handler (assoc hello-world-request :ok true))))))
5961

6062
(defexpect with-defaults-test
6163
(expecting "with defaults"
@@ -72,7 +74,7 @@
7274
(expect {:status 500
7375
:headers {"Content-Type" "text/html"}
7476
:body "<html><head><title>Error</title></head><body><p>Internal Server Error</p></body></html>"}
75-
(handler req))
77+
(handler hello-world-request))
7678
(expect {"breadcrumbs" (),
7779
"contexts" {},
7880
"request" {"data" {"one" 1},
@@ -98,7 +100,7 @@
98100
handler (ring/wrap-report-exceptions wrapped {:preprocess-fn preprocess
99101
:postprocess-fn postprocess
100102
:error-fn error})]
101-
(expect (assoc req :exception e) (handler req))
103+
(expect (assoc hello-world-request :exception e) (handler hello-world-request))
102104
(expect {"breadcrumbs" (),
103105
"contexts" {},
104106
"environment" "qa",
@@ -123,11 +125,11 @@
123125

124126
(defexpect wrap-sentry-tracing-test
125127
(let [sentry-options (get-test-options {:traces-sample-rate 1.0 :debug true})
126-
ok-req (assoc req :ok true)]
128+
ok-req (assoc hello-world-request :ok true)]
127129
(Sentry/init ^SentryOptions sentry-options)
128130
(expecting "passing through"
129131
(let [handler (ring/wrap-sentry-tracing wrapped)]
130-
(expect "woo" (handler ok-req))))
132+
(expect "w00t" (handler ok-req))))
131133
(expecting "preprocess fn called"
132134
(let [handler (ring/wrap-sentry-tracing wrapped {:preprocess-fn preprocess})]
133135
(expect (fn [_transaction]
@@ -138,4 +140,32 @@
138140
(deliver p (.getData (.getRequest scope))))))
139141
@p)))
140142
(side-effects [st/finish-transaction!]
141-
(expect "woo" (handler ok-req))))))))
143+
(expect "w00t" (handler ok-req))))))))
144+
145+
(defexpect cookie-decoding-test
146+
(expecting "cookies are decoded properly"
147+
(let [event {:throwable e
148+
:request {:url "https://example.com/hello-world"
149+
:method nil
150+
:data {:one 1}
151+
:query-string ""
152+
:headers {"cookie" "sessionId=1234" "ok" 2 "host" "example.com"}
153+
:env {:session "{}" "REMOTE_ADDR" "127.0.0.1"}}
154+
:user {:ip_address "127.0.0.1"}}
155+
sentry-event (strip-timestamp (serialize event))
156+
handler (-> (ring/wrap-report-exceptions wrapped {})
157+
(cookies-middleware/wrap-cookies))
158+
request (-> (assoc hello-world-request :ok false)
159+
(mock/cookie :sessionId "1234"))
160+
{:keys [status] :as response} (handler request)]
161+
(expect 500 status)
162+
(expect {"breadcrumbs" (),
163+
"contexts" {},
164+
"request" {"cookies" "sessionId=1234",
165+
"data" {"one" 1},
166+
"env" {"REMOTE_ADDR" "127.0.0.1", "session" "{}"},
167+
"headers" {"cookie" "sessionId=1234", "host" "example.com", "ok" 2},
168+
"query_string" "",
169+
"url" "https://example.com/hello-world"},
170+
"sdk" {"version" "blah"},
171+
"user" {}} sentry-event))))

0 commit comments

Comments
 (0)