Skip to content

Commit

Permalink
Migrate to sentry-clj. Getting rid of raven lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Aliiev authored and Arthur Aliiev committed Mar 7, 2024
1 parent e442ea2 commit f6bc78b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{org.clojure/clojure "1.9.0"
org.clojure/tools.logging "0.4.0"
com.stuartsierra/component "0.3.2"
exoscale/raven "0.4.2"
io.sentry/sentry-clj "7.4.213"
spootnik/net "0.3.3-beta24"
spootnik/uncaught "0.5.3"
metrics-clojure "2.10.0"
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[org.clojure/clojure "1.10.1"]
[org.clojure/tools.logging "1.0.0"]
[com.stuartsierra/component "1.0.0"]
[exoscale/raven "0.4.15"]
[io.sentry/sentry-clj "7.4.213"]
[spootnik/uncaught "0.5.5"]
[metrics-clojure "2.10.0"]
[metrics-clojure-riemann "2.10.0"]
Expand Down
10 changes: 4 additions & 6 deletions src/spootnik/reporter.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns spootnik.reporter
(:require [com.stuartsierra.component :as c]
[clojure.tools.logging :as log]
[raven.client :as raven]
[spootnik.reporter.impl :as rptr]
[manifold.deferred :as d]
spootnik.reporter.specs))
Expand Down Expand Up @@ -80,11 +79,10 @@

([error extra]
(log/error error)
(-> (capture! (-> (if (instance? Exception error)
(-> {:data (ex-data error)}
(raven/add-exception! error))
{:message error})
(raven/add-extra! extra)))
(-> (capture! (if (instance? Exception error)
{:extra extra
:throwable error}
{:message error}))
(d/catch Throwable
(fn [e]
(log/error e "Sentry failure")))
Expand Down
50 changes: 41 additions & 9 deletions src/spootnik/reporter/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[manifold.deferred :as d]
[clojure.java.io :as io]
[com.stuartsierra.component :as c]
[raven.client :as raven]
[sentry-clj.core :as sentry-io]
[metrics.reporters.console :as console]
[metrics.reporters.jmx :as jmx]
[metrics.reporters.graphite :as graphite]
Expand All @@ -19,7 +19,8 @@
[camel-snake-kebab.core :as csk]
[clojure.string :as str]
[clojure.tools.logging :refer [info error]]
[spootnik.uncaught :refer [with-uncaught]])
[spootnik.uncaught :refer [with-uncaught]]
[spootnik.reporter.sentry :refer [localhost]])
(:import com.aphyr.riemann.client.RiemannClient
com.aphyr.riemann.client.RiemannBatchClient
com.aphyr.riemann.client.TcpTransport
Expand Down Expand Up @@ -209,7 +210,7 @@
state (or (:state ev) (:state defaults))
time ^Long (or time (quot (System/currentTimeMillis) 1000))]
(-> (.event client)
(.host (or host (:host defaults) (raven/localhost)))
(.host (or host (:host defaults) (localhost)))
(.service (or service (:service defaults) "<none>"))
(.time (long time))
(cond-> metric (.metric metric)
Expand Down Expand Up @@ -369,19 +370,44 @@
[^PushGateway pg ^CollectorRegistry registry ^String job ^java.util.Map grouping-keys]
(.push pg registry job grouping-keys))

(defn- e->sentry-request [e]
(let [payload (-> e :extra :payload)]
{:url (-> payload :uri)
:method (-> payload :request-method name clojure.string/upper-case)
:query-string (-> payload :query-string)
:headers (-> payload :headers)}))

(defn send-event [options e tags]
(let [{:keys [extra throwable]} e

event {:message (ex-message e)
:request (e->sentry-request e)
:level :error
:platform "java"
:user {:id (-> e :extra :org/uuid str)}
:tags tags
:server-name (localhost)
:fingerprints (some-> options :fingerpint seq)
:extra extra
:throwable throwable}]

(try
(sentry-io/send-event event)
(catch Exception e
(error e "cannot send sentry event")))))

(defn parse-pggrouping-keys [grouping-keys]
(into {} (for [[k v] grouping-keys] [(csk/->snake_case_string k) v])))

;; "sentry" is a sentry map like {:dsn "..."}
;; "raven-options" is the options map sent to raven http client
;; http://aleph.io/codox/aleph/aleph.http.html#var-request
;; "sentry" is a configuration map sent to sentry.io on initialisation
;; https://github.com/getsentry/sentry-clj/tree/master?tab=readme-ov-file#additional-initialisation-options
(defrecord Reporter [rclient raven-options reporters registry sentry metrics riemann prevent-capture? prometheus
started? pushgateway]
c/Lifecycle
(start [this]
(if started?
this
(let [prometheus-registry (CollectorRegistry/defaultRegistry)
(let [prometheus-registry (CollectorRegistry/defaultRegistry)
[pgclient pgjob pgregistry pggrouping-keys] (when pushgateway [(build-pushgateway-client pushgateway)
(name (:job pushgateway))
(CollectorRegistry.)
Expand All @@ -405,6 +431,9 @@
prometheus
prometheus-registry)
opts)))]

(sentry-io/init! (:dsn sentry) sentry)

(when-not prevent-capture?
(with-uncaught e
(capture! (assoc this :raven-options options) e)))
Expand Down Expand Up @@ -436,7 +465,10 @@
(.close ^RiemannClient rclient)
(catch Exception _)))
(when prometheus
(.close ^java.io.Closeable (:server prometheus))))
(.close ^java.io.Closeable (:server prometheus)))

(sentry-io/close!))

(assoc this
:raven-options nil
:reporters nil
Expand Down Expand Up @@ -519,7 +551,7 @@
(capture! [this e tags]
(if (:dsn sentry)
(-> (try
(raven/capture! raven-options (:dsn sentry) e tags)
(send-event raven-options e tags)
(catch Exception e
(d/error-deferred e)))
(d/chain
Expand Down
38 changes: 38 additions & 0 deletions src/spootnik/reporter/sentry.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ns spootnik.reporter.sentry
(:require
[clojure.string :as str]
[clojure.java.shell :as sh]))

(def hostname-refresh-interval
"How often to allow reading /etc/hostname, in seconds."
60)

(defn get-hostname
"Get the current hostname by shelling out to 'hostname'"
[]
(or
(try
(let [{:keys [exit out]} (sh/sh "hostname")]
(when (= exit 0)
(str/trim out)))
(catch Exception _))
"<unknown>"))

(defn hostname
"Fetches the hostname by shelling to 'hostname', whenever the given age
is stale enough. If the given age is recent, as defined by
hostname-refresh-interval, returns age and val instead."
[[age val]]
(if (and val (<= (* 1000 hostname-refresh-interval)
(- (System/currentTimeMillis) age)))
[age val]
[(System/currentTimeMillis) (get-hostname)]))

(let [cache (atom [nil nil])]
(defn localhost
"Returns the local host name."
[]
(if (re-find #"^Windows" (System/getProperty "os.name"))
(or (System/getenv "COMPUTERNAME") "localhost")
(or (System/getenv "HOSTNAME")
(second (swap! cache hostname))))))

0 comments on commit f6bc78b

Please sign in to comment.