[#215] Add 3rd-party appender for Sentry (@samuelotter)

Appender that sends events to Sentry (https://sentry.io/).
This commit is contained in:
Samuel Otter 2017-01-12 22:00:15 +01:00 committed by Peter Taoussanis
parent 05cd4a771b
commit 1a434293a3
2 changed files with 65 additions and 1 deletions

View File

@ -43,7 +43,8 @@
[com.mchange/c3p0 "0.9.5.2"]
[cheshire "5.6.3"]
[ymilky/franzy "0.0.1"]
[com.newrelic.agent.java/newrelic-agent "3.31.1"]]}
[com.newrelic.agent.java/newrelic-agent "3.31.1"]
[raven-clj "1.5.0"]]}
:dev
[:1.9 :test :server-jvm

View File

@ -0,0 +1,63 @@
(ns taoensso.timbre.appenders.3rd-party.sentry
"Sentry appender
Requires https://github.com/sethtrain/raven-clj"
{:author "Samuel Otter (@samuelotter)"}
(:require
[taoensso.encore :as enc]
[taoensso.timbre :as timbre]
[raven-clj.core :as raven]
[raven-clj.interfaces :as interfaces]))
(def ^:private levels {:fatal "fatal"
:error "error"
:warn "warning"
:info "info"
:debug "debug"
:trace "debug"})
(defn sentry-appender
"Creates a sentry-appender. Requires the DSN (e.g.
\"https://<key>:<secret>@sentry.io/<project>\", see sentry documentation) to
be passed in.
opts may contain additional attributes:
* :tags :environment :release :modules will be passed on to sentry as
attributes See https://docs.sentry.io/clientdev/attributes/ for more
information.
* :event-fn can be used to modify the raw even before sending it
to sentry.
"
[dsn & [opts]]
(let [{:keys [event-fn] :or {event-fn identity}} opts
base-event (->> (select-keys opts [:tags :environment :release :modules])
(filter (comp not nil? second))
(into {}))]
{:enabled? true
:async? true
:min-level :warn ; Sentry supports info and debug as well but warning
; is a reasonable default given how sentry works.
:rate-limit nil
:output-fn :inherit
:fn
(fn [data]
(let [{:keys [instant level output_ ?err msg_ ?ns-str context]} data
event (as-> base-event event
(merge event {:message (force msg_)
:logger ?ns-str
:level (get levels level :warning)}
(when context
{:extra context}))
(if ?err
(interfaces/stacktrace event ?err)
event)
(event-fn event))]
(raven/capture dsn event)))}))
(comment
;; Create an example appender with default options:
(sentry-appender "https://<key>:<secret>@sentry.io/<project>")
;; Create an example appender with default options, but override `:min-level`:
(merge (sentry-appender "https://<key>:<secret>@sentry.io/<project>") {:min-level :debug}))