Integrate yada with Bugsnag notifier

This commit is contained in:
Marcin Kulik 2017-03-04 13:07:26 +01:00
parent 800f466fa9
commit 767e33145f
2 changed files with 45 additions and 19 deletions

View File

@ -1,24 +1,41 @@
(ns asciinema.main (ns asciinema.main
(:gen-class) (:gen-class)
(:require [com.stuartsierra.component :as component] (:require [asciinema.yada :as y]
[clj-bugsnag.core :as bugsnag]
[com.stuartsierra.component :as component]
[duct.util.runtime :refer [add-shutdown-hook]] [duct.util.runtime :refer [add-shutdown-hook]]
[duct.util.system :refer [load-system]] [duct.util.system :refer [load-system]]
[environ.core :refer [env]] [environ.core :refer [env]]
[clojure.java.io :as io])) [clojure.java.io :as io]))
(defn- request-context [req]
(str (-> req (get :request-method :unknown) name .toUpperCase)
" "
(:uri req)))
(defn- create-exception-notifier []
(when-let [key (:bugsnag-key env)]
(let [environment (:env-name env "production")
version (:git-sha env)]
(fn [ex req]
(bugsnag/notify ex {:api-key key
:environment environment
:project-ns "asciinema"
:version version
:context (request-context req)
:meta {:request (dissoc req :body)}})))))
(defn -main [& args] (defn -main [& args]
(let [bindings {'http-port (Integer/parseInt (:port env "3000")) (binding [y/*exception-notifier* (create-exception-notifier)]
'db-uri (:database-url env) (let [bindings {'http-port (Integer/parseInt (:port env "3000"))
'env-name (:env-name env "production") 'db-uri (:database-url env)
'git-sha (:git-sha env) 's3-bucket (:s3-bucket env)
'bugsnag-key (:bugsnag-key env) 's3-access-key (:s3-access-key env)
's3-bucket (:s3-bucket env) 's3-secret-key (:s3-secret-key env)
's3-access-key (:s3-access-key env) 'redis-host (:redis-host env "localhost")
's3-secret-key (:s3-secret-key env) 'redis-port (Integer/parseInt (:redis-port env "6379"))}
'redis-host (:redis-host env "localhost") system (->> (load-system [(io/resource "asciinema/system.edn")] bindings)
'redis-port (Integer/parseInt (:redis-port env "6379"))}
system (->> (load-system [(io/resource "asciinema/system.edn")] bindings)
(component/start))] (component/start))]
(add-shutdown-hook ::stop-system #(component/stop system)) (add-shutdown-hook ::stop-system #(component/stop system))
(println "Started HTTP server on port" (-> system :http :port))) (println "Started HTTP server on port" (-> system :http :port))))
@(promise)) @(promise))

View File

@ -4,6 +4,8 @@
[yada.status :as status] [yada.status :as status]
[yada.yada :as yada])) [yada.yada :as yada]))
(def ^:dynamic *exception-notifier* nil)
(def not-found-model (def not-found-model
{:produces {:produces
#{"text/html" "text/plain"} #{"text/html" "text/plain"}
@ -22,15 +24,22 @@
"text/html" (str "<html><body><h1>" status-name "</h1></body></html>") "text/html" (str "<html><body><h1>" status-name "</h1></body></html>")
status-name))) status-name)))
(defn logger [ctx] (defn create-logger []
(when-let [error (:error ctx)] (let [notifier *exception-notifier*]
(when (not= (-> ctx :response :status) 404) (fn [ctx]
(log/error error)))) (when-let [error (:error ctx)]
(let [status (-> ctx :response :status)]
(when (not= status 404)
(log/error error))
(when (and (= status 500) notifier)
(let [ex (or (-> error ex-data :error) error)]
(notifier ex (:request ctx))))))
ctx)))
(defn resource [model] (defn resource [model]
(let [error-statuses (set (concat (range 400 404) (range 405 600) ))] (let [error-statuses (set (concat (range 400 404) (range 405 600) ))]
(-> model (-> model
(assoc :logger logger) (assoc :logger (create-logger))
(update-in [:responses 404] #(or % not-found-model)) (update-in [:responses 404] #(or % not-found-model))
(update-in [:responses error-statuses] #(or % {:produces #{"text/html" "text/plain"} (update-in [:responses error-statuses] #(or % {:produces #{"text/html" "text/plain"}
:response error-response})) :response error-response}))