Skip to content

Commit

Permalink
Add Sentry tracing and supporting ring middleware.
Browse files Browse the repository at this point in the history
Big thanks to @karuta0825 for the tracing functionality!

Update Sentry Java SDK to 5.7.1.
Various library updates.

Fixes #32.

-=david=-
  • Loading branch information
dharrigan committed Apr 2, 2022
1 parent 357f775 commit 78c307b
Show file tree
Hide file tree
Showing 19 changed files with 543 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: [ '8', '17']
java: ['8', '17']
steps:
- uses: actions/checkout@v3

- name: Setup Cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.m2
Expand Down
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ commits since the beginning of this repository.
### Added
### Removed

## [5.7.172]

### Added

- The ability to use tracing with Sentry. Big thanks to @karuta0825. There is an example in the `examples` directory.

### Changed

- Update Sentry Java SDK to 5.7.1.
- Various library updates.
- Update github workers cache to v3

## [5.7.171]

### Changed
Expand Down Expand Up @@ -256,7 +268,8 @@ commits since the beginning of this repository.
compatible with Sentry 10.0.1 and below. If you wish to use those
versions, please continue to use sentry-clj 1.7.30.

[Unreleased]: https://github.com/getsentry/sentry-clj/compare/5.7.171...HEAD
[Unreleased]: https://github.com/getsentry/sentry-clj/compare/5.7.172...HEAD
[5.7.172]: https://github.com/getsentry/sentry-clj/compare/5.7.171...5.7.172
[5.7.171]: https://github.com/getsentry/sentry-clj/compare/5.6.170...5.7.171
[5.6.170]: https://github.com/getsentry/sentry-clj/compare/5.6.169...5.6.170
[5.6.169]: https://github.com/getsentry/sentry-clj/compare/5.6.166...5.6.169
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ If you want an interpolated message, you need to provide the full map, i.e.,
| | [More Information](https://docs.sentry.io/platforms/java/enriching-events/breadcrumbs/) |
| `:contexts` | A map of key/value pairs to attach to every Event that is sent. |
| | [More Information)(https://docs.sentry.io/platforms/java/enriching-events/context/) |
| `:traces-sample-rate` | Set a uniform sample rate(a number of between 0.0 and 1.0) for all transactions for tracing |
| `:traces-sample-fn` | A function (taking a custom sample context and a transaction context) enables you to control trace transactions |

Some examples:

Expand Down
5 changes: 3 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
;;
;;
;;
io.sentry/sentry {:mvn/version "5.7.0"}
io.sentry/sentry {:mvn/version "5.7.1"}
ring/ring-core {:mvn/version "1.9.5"}}

:aliases {:build {:deps {io.github.seancorfield/build-clj {:git/tag "v0.8.0"
Expand All @@ -13,7 +13,7 @@

:test {:extra-paths ["test"]
:extra-deps {cheshire/cheshire {:mvn/version "5.10.2"}
com.github.seancorfield/expectations {:mvn/version "2.0.157"}
com.github.seancorfield/expectations {:mvn/version "2.0.160"}
lambdaisland/kaocha {:mvn/version "1.64.1010"}
lambdaisland/kaocha-cloverage {:mvn/version "1.0.75"}
lambdaisland/kaocha-junit-xml {:mvn/version "0.0.76"}
Expand All @@ -24,6 +24,7 @@
org.slf4j/slf4j-nop {:mvn/version "1.7.36"}}
:exec-fn antq.tool/outdated
:exec-args {:directory ["."]
:exclude [org.clojure/clojure]
:skip ["pom"]
:verbose true
:upgrade true
Expand Down
4 changes: 4 additions & 0 deletions examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ as:
`bin/run -d DSN`

Where `DSN` is your DSN URL.

== Tracing

This example shows how to perform tracing with Ring.
4 changes: 2 additions & 2 deletions examples/basic/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
;;
;;
ch.qos.logback/logback-classic {:mvn/version "1.2.11"}
io.sentry/sentry {:mvn/version "5.7.0"}
io.sentry/sentry-clj {:mvn/version "5.7.171"}
io.sentry/sentry {:mvn/version "5.7.1"}
io.sentry/sentry-clj {:local/root "../../../sentry-clj"}
org.clojure/tools.cli {:mvn/version "1.0.206"}
org.clojure/tools.logging {:mvn/version "1.2.4"}
org.slf4j/jcl-over-slf4j {:mvn/version "1.7.36"}
Expand Down
7 changes: 3 additions & 4 deletions examples/basic/src/basic/main.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
(ns basic.main
(:require
[clojure.tools.logging :as log]
[sentry-clj.core :as sentry]))
(:require
[clojure.tools.logging :as log]
[sentry-clj.core :as sentry]))

(defn ^:private create-sentry-logger
"Create a Sentry Logger using the supplied `dsn`.
If no `dsn` is supplied, simply log the `event` to a `logger`."
[{:keys [dsn] :as config}]
(if dsn
Expand Down
20 changes: 20 additions & 0 deletions examples/ring_with_tracing/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{:paths ["src" "resources"]

:deps {org.clojure/clojure {:mvn/version "1.10.3"}
;;
;;
;;
ch.qos.logback/logback-classic {:mvn/version "1.2.11"}
integrant/integrant {:mvn/version "0.8.0"}
integrant/repl {:mvn/version "0.3.2"}
io.sentry/sentry {:mvn/version "5.7.1"}
io.sentry/sentry-clj {:local/root "../../../sentry-clj"}
org.clojure/tools.cli {:mvn/version "1.0.206"}
org.clojure/tools.logging {:mvn/version "1.2.4"}
org.slf4j/jcl-over-slf4j {:mvn/version "1.7.36"}
org.slf4j/jul-to-slf4j {:mvn/version "1.7.36"}
org.slf4j/log4j-over-slf4j {:mvn/version "1.7.36"}
org.slf4j/slf4j-api {:mvn/version "1.7.36"}
ring/ring-core {:mvn/version "1.9.5"}
ring/ring-jetty-adapter {:mvn/version "1.9.5"}
ring/ring-json {:mvn/version "0.5.1"}}}
20 changes: 20 additions & 0 deletions examples/ring_with_tracing/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">

<statusListener class="ch.qos.logback.core.status.NopStatusListener" />

<contextName>examples.basic</contextName>

<variable name="ROOT_LOG_LEVEL" value="${ROOT_LOG_LEVEL:-info}" />

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>[%boldWhite(%d)] --- [%boldBlue(%-5level)][%boldGreen(%-25.25logger{25})] - %msg%n%rEx</Pattern>
</encoder>
</appender>

<root level="${ROOT_LOG_LEVEL}">
<appender-ref ref="CONSOLE"/>
</root>

</configuration>
50 changes: 50 additions & 0 deletions examples/ring_with_tracing/src/ring_with_tracing/main.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns ring-with-tracing.main
(:require
[integrant.core :as ig]
[ring.adapter.jetty :refer [run-jetty]]
[ring.middleware.json :refer [wrap-json-params wrap-json-response]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
[sentry-clj.core :as sentry]
[sentry-clj.ring :refer [wrap-report-exceptions wrap-sentry-tracing]]
[sentry-clj.tracing :refer [with-start-child-span]]))

(def config
{:adapter/jetty {:port 8080, :handler (ig/ref :handler/router)}
:handler/router {:app (ig/ref :handler/hello)}
:handler/hello {:name "Sentry"}})

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

(defmethod ig/init-key :handler/hello [_ {:keys [name]}]
(fn [request]

(with-start-child-span "task" "my-child-operation"
(Thread/sleep 1000))

{:status 200
:headers {"Content-type" "application/json"}
:body (str "Hi " name)}))

(defmethod ig/init-key :handler/router [_ {:keys [app]}]
;; set your dsn
(let [dsn "https://abcdefg@localhost:9000/2"]
(sentry/init! dsn {:dsn dsn
:environment "local"
:traces-sample-rate 1.0
:debug true}))

(-> app
(wrap-report-exceptions {})
wrap-sentry-tracing
wrap-keyword-params
wrap-json-params
wrap-json-response))

(defmethod ig/halt-key! :adapter/jetty [_ server]
(.stop server))

(defn -main
[& _]
(-> config
ig/init))
7 changes: 7 additions & 0 deletions examples/ring_with_tracing/src/user.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns user
(:require
[integrant.core :as ig]
[integrant.repl :refer [clear go halt prep init reset reset-all]]
[ring-with-tracing.main]))

(integrant.repl/set-prep! (comp ig/prep (constantly ring-with-tracing.main/config)))
4 changes: 2 additions & 2 deletions examples/uncaught/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
;;
;;
ch.qos.logback/logback-classic {:mvn/version "1.2.11"}
io.sentry/sentry {:mvn/version "5.7.0"}
io.sentry/sentry-clj {:mvn/version "5.7.171"}
io.sentry/sentry {:mvn/version "5.7.1"}
io.sentry/sentry-clj {:local/root "../../../sentry-clj"}
org.clojure/tools.cli {:mvn/version "1.0.206"}
org.clojure/tools.logging {:mvn/version "1.2.4"}
org.slf4j/jcl-over-slf4j {:mvn/version "1.7.36"}
Expand Down
10 changes: 5 additions & 5 deletions examples/uncaught/src/uncaught/main.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns uncaught.main
(:require
[clojure.tools.cli :refer [parse-opts]]
[clojure.tools.logging :as log]
[sentry-clj.core :as sentry])
(:gen-class))
(:require
[clojure.tools.cli :refer [parse-opts]]
[clojure.tools.logging :as log]
[sentry-clj.core :as sentry])
(:gen-class))

(defn ^:private set-default-exception-handler
"Register our own Uncaught Exception Handler using the provided `sentry-client`."
Expand Down
35 changes: 24 additions & 11 deletions src/sentry_clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
:fatal SentryLevel/FATAL
SentryLevel/INFO))

(defn ^:private java-util-hashmappify-vals
(defn java-util-hashmappify-vals
"Converts an ordinary Clojure map into a Clojure map with nested map
values recursively translated into java.util.HashMap objects. Based
on walk/stringify-keys."
values recursively translated into java.util.HashMap objects. Based
on walk/stringify-keys."
[m]
(let [f (fn [[k v]]
(let [k (if (keyword? k) (name k) k)
Expand Down Expand Up @@ -157,7 +157,9 @@
enable-uncaught-exception-handler ;; deprecated
uncaught-handler-enabled
before-send-fn
before-breadcrumb-fn]} (merge sentry-defaults config)
before-breadcrumb-fn
traces-sample-rate
traces-sample-fn]} (merge sentry-defaults config)
sentry-options (SentryOptions.)]

(.setDsn sentry-options dsn)
Expand Down Expand Up @@ -196,7 +198,16 @@
(reify io.sentry.SentryOptions$BeforeBreadcrumbCallback
(execute [_ breadcrumb hint]
(before-breadcrumb-fn breadcrumb hint)))))

(when traces-sample-rate
(.setTracesSampleRate sentry-options traces-sample-rate))
(when traces-sample-fn
(.setTracesSampler sentry-options ^io.sentry.SentryOptions$TracesSamplerCallback
(reify io.sentry.SentryOptions$TracesSamplerCallback
(sample [_ ctx]
(traces-sample-fn {:custom-sample-context (-> ctx
.getCustomSamplingContext
.getData)
:transaction-context (.getTransactionContext ctx)})))))
sentry-options))

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
Expand Down Expand Up @@ -228,6 +239,8 @@
| | [More Information](https://docs.sentry.io/platforms/java/enriching-events/breadcrumbs/) |
| `:contexts` | A map of key/value pairs to attach to every Event that is sent. |
| | [More Information)(https://docs.sentry.io/platforms/java/enriching-events/context/) |
| `:traces-sample-rate` | Set a uniform sample rate(a number of between 0.0 and 1.0) for all transactions for tracing |
| `:traces-sample-fn` | A function (taking a custom sample context and a transaction context) enables you to control trace transactions |
Some examples:
Expand Down Expand Up @@ -271,12 +284,12 @@
(defn send-event
"Sends the given event to Sentry, returning the event's id
Supports sending throwables:
Supports sending throwables:
```
(sentry/send-event {:message \"oh no\",
:throwable (RuntimeException. \"foo bar\"})
```
"
```
(sentry/send-event {:message \"oh no\",
:throwable (RuntimeException. \"foo bar\"})
```
"
[event]
(str (Sentry/captureEvent (map->event event))))
Loading

0 comments on commit 78c307b

Please sign in to comment.