From 9b78b2a4325997f4440f9003c218c872d078df06 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 15 Feb 2022 09:22:00 +0100 Subject: [PATCH] :sparkles: Improve error reporting on background tasks --- backend/src/app/http/errors.clj | 4 +--- backend/src/app/loggers/database.clj | 18 ++++++++---------- backend/src/app/util/time.clj | 5 +++++ backend/src/app/worker.clj | 10 +++++----- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/backend/src/app/http/errors.clj b/backend/src/app/http/errors.clj index a53637849..0c24a3df7 100644 --- a/backend/src/app/http/errors.clj +++ b/backend/src/app/http/errors.clj @@ -10,7 +10,6 @@ [app.common.exceptions :as ex] [app.common.logging :as l] [app.common.spec :as us] - [app.common.uuid :as uuid] [clojure.spec.alpha :as s] [cuerdas.core :as str])) @@ -24,8 +23,7 @@ [request error] (let [data (ex-data error)] (merge - {:id (uuid/next) - :path (:uri request) + {:path (:uri request) :method (:request-method request) :hint (ex-message error) :params (:params request) diff --git a/backend/src/app/loggers/database.clj b/backend/src/app/loggers/database.clj index ebece9af0..d4e339100 100644 --- a/backend/src/app/loggers/database.clj +++ b/backend/src/app/loggers/database.clj @@ -27,10 +27,8 @@ (defonce enabled (atom true)) (defn- persist-on-database! - [{:keys [pool] :as cfg} {:keys [id] :as event}] - (db/with-atomic [conn pool] - (db/insert! conn :server-error-report - {:id id :content (db/tjson event)}))) + [{:keys [pool]} {:keys [id] :as event}] + (db/insert! pool :server-error-report {:id id :content (db/tjson event)})) (defn- parse-event-data [event] @@ -51,7 +49,7 @@ (assoc :host (cf/get :host)) (assoc :public-uri (cf/get :public-uri)) (assoc :version (:full cf/version)) - (update :id (fn [id] (or id (uuid/next)))))) + (assoc :id (uuid/next)))) (defn handle-event [{:keys [executor] :as cfg} event] @@ -59,12 +57,13 @@ (try (let [event (parse-event event) uri (cf/get :public-uri)] + (l/debug :hint "registering error on database" :id (:id event) :uri (str uri "/dbg/error/" (:id event))) + (persist-on-database! cfg event)) - (catch Exception e - (l/warn :hint "unexpected exception on database error logger" - :cause e))))) + (catch Exception cause + (l/warn :hint "unexpected exception on database error logger" :cause cause))))) (defmethod ig/pre-init-spec ::reporter [_] (s/keys :req-un [::wrk/executor ::db/pool ::receiver])) @@ -76,8 +75,7 @@ (defmethod ig/init-key ::reporter [_ {:keys [receiver] :as cfg}] (l/info :msg "initializing database error persistence") - (let [output (a/chan (a/sliding-buffer 5) - (filter error-event?))] + (let [output (a/chan (a/sliding-buffer 5) (filter error-event?))] (receiver :sub output) (a/go-loop [] (let [msg (a/> data ::s/problems (take 10) seq vec) :spec-value (some->> data ::s/value) :data (some-> data (dissoc ::s/problems ::s/value ::s/spec)) @@ -424,6 +423,7 @@ (run-task conn)) (catch Throwable cause (l/error :hint "unhandled exception on scheduled task" + ::l/context (get-error-context cause task) :task-id id :cause cause))))] (try