sethtrain / raven-clj

A Clojure interface to Sentry
84 stars 29 forks source link

raven-clj raven cljdoc badge

A Clojure interface to Sentry.

Usage | Ring Middleware | Alternatives | Changes | Contributing

Usage

[raven-clj "1.7.0"] ;; latest release

raven-clj.core/capture is a general use function that can be placed throughout your Clojure code to log information to your Sentry server.

(require '[raven-clj.core :refer [capture]])

(def dsn "https://b70a31b3510c4cf793964a185cfe1fd0:b7d80b520139450f903720eb7991bf3d@example.com/1")

(capture dsn {:message "Test Exception Message"
              :tags {:version "1.0"}
              :logger "main-logger"
              :extra {:my-key 1
                      :some-other-value "foo bar"}})

Various interfaces are supported.

HTTP

(require '[raven-clj.interfaces :as interfaces])

(capture dsn
        (-> {:message "Test HTTP Exception"
             :tags {:testing "1.0"}}
            (interfaces/http ring-style-request-map)))

Exceptions

;; All namespaces in the following map will be regarded
;; as application code and highlighted in Sentry's stacktraces
;; > Note that this also matches "myapp.ns.core" etc.
(def your-namespace ["myapp.ns"])

(capture dsn
        (-> {:message "Test Stacktrace Exception"}
            (interfaces/stacktrace (Exception.) your-ns)))

Note about event-info map

In the capture function I use merge to merge together the final packet to send to Sentry. The only fields that can't be overwritten when sending information to capture is event-id and timestamp. Everything else can be overwritten by passing along a new value for the key:

(capture dsn
        (-> {:message "Test Stacktrace Exception"
             :logger "application-logger"}
            (interfaces/stacktrace (Exception.))))

Please refer to Building the JSON Packet for more information on what attributes are allowed within the packet sent to Sentry.

Uncaught exception handler

Since 1.6.0-alpha a function that will install an uncaught exception handler is included, see raven-clj.core/install-uncaught-exception-handler! for details.

Ring middleware

raven-clj also includes a Ring middleware that sends the Http and Stacktrace interfaces for Sentry packets. Usage (for Compojure):

(use 'raven-clj.ring)

(def dsn "https://b70a31b3510c4cf793964a185cfe1fd0:b7d80b520139450f903720eb7991bf3d@example.com/1")

;; If you want to fully utilize the Http interface you should make sure
;; you use the wrap-params and wrap-keyword-params middlewares to ensure
;; the request data is stored correctly.
(-> routes
    (wrap-sentry dsn)
    (handler/site))

;; You could also include some of the optional attributes, pass
;; your app's namespace prefixes so your app's stack frames are
;; highlighted in sentry or specify a function to alter the data
;; that is stored from the HTTP request.
(-> routes
    (wrap-sentry dsn {:extra {:tags {:version "1.0"}}
                      :namespaces ["myapp" "com.mylib"]
                      :http-alter-fn (fn [r] (dissoc r :data))})
    (handler/site))

Alternatives

There are a variety of Clojure libraries for Sentry, a quick, not necessarily up-to-date overview/comparison:

Changes

Contributing

PRs and issues welcome :tada:

Note: Because this library uses it's own version in various places the version is stored on the classpath. In order to modify the version you need to modify resources/raven_clj/version.txt.

License

Copyright © 2013-2015 Seth Buntin

Distributed under the Eclipse Public License, the same as Clojure.