0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-22 14:39:45 -05:00

Minor enhacements on log processing.

This commit is contained in:
Andrey Antukh 2021-12-23 18:36:58 +01:00
parent c6054f7ab2
commit 1839397ebc
10 changed files with 83 additions and 108 deletions

View file

@ -3,7 +3,8 @@
rumext.alpha/defc clojure.core/defn rumext.alpha/defc clojure.core/defn
rumext.alpha/fnc clojure.core/fn rumext.alpha/fnc clojure.core/fn
app.common.data/export clojure.core/def app.common.data/export clojure.core/def
app.db/with-atomic clojure.core/with-open} app.db/with-atomic clojure.core/with-open
app.common.logging/with-context clojure.core/do}
:hooks :hooks
{:analyze-call {:analyze-call

View file

@ -74,5 +74,3 @@
;; (prn "==============" rtype (into {} ?meta)) ;; (prn "==============" rtype (into {} ?meta))
;; (prn (api/sexpr result)) ;; (prn (api/sexpr result))
{:node result})) {:node result}))

View file

@ -90,20 +90,9 @@
(try (try
(handler request) (handler request)
(catch Throwable e (catch Throwable e
(try (l/with-context (errors/get-error-context request e)
(let [cdata (errors/get-error-context request e)] (l/error :hint (ex-message e) :cause e)
(l/update-thread-context! cdata) {:status 500 :body "internal server error"}))))))
(l/error :hint "unhandled exception"
:message (ex-message e)
:error-id (str (:id cdata))
:cause e))
{:status 500 :body "internal server error"}
(catch Throwable e
(l/error :hint "unhandled exception"
:message (ex-message e)
:cause e)
{:status 500 :body "internal server error"})))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Http Main Handler (Router) ;; Http Main Handler (Router)

View file

@ -7,7 +7,6 @@
(ns app.http.errors (ns app.http.errors
"A errors handling for the http server." "A errors handling for the http server."
(:require (:require
[app.common.data :as d]
[app.common.exceptions :as ex] [app.common.exceptions :as ex]
[app.common.logging :as l] [app.common.logging :as l]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
@ -20,36 +19,18 @@
(get headers "x-real-ip") (get headers "x-real-ip")
(get request :remote-addr))) (get request :remote-addr)))
(defn- simple-prune
([s] (simple-prune s (* 1024 1024)))
([s max-length]
(if (> (count s) max-length)
(str (subs s 0 max-length) " [...]")
s)))
(defn- stringify-data
[data]
(binding [clojure.pprint/*print-right-margin* 200]
(let [result (with-out-str (clojure.pprint/pprint data))]
(simple-prune result (* 1024 1024)))))
(defn get-error-context (defn get-error-context
[request error] [request error]
(let [data (ex-data error)] (let [data (ex-data error)]
(d/without-nils (merge
(merge {:id (uuid/next)
{:id (str (uuid/next)) :path (:uri request)
:path (str (:uri request)) :method (:request-method request)
:method (name (:request-method request)) :hint (or (:hint data) (ex-message error))
:hint (or (:hint data) (ex-message error)) :params (l/stringify-data (:params request))
:params (stringify-data (:params request)) :data (l/stringify-data (dissoc data :explain))
:data (stringify-data (dissoc data :explain)) :ip-addr (parse-client-ip request)
:ip-addr (parse-client-ip request) :profile-id (:profile-id request)}
:explain (str/prune (:explain data) (* 1024 1024) "[...]")}
(when-let [id (:profile-id request)]
{:profile-id id})
(let [headers (:headers request)] (let [headers (:headers request)]
{:user-agent (get headers "user-agent") {:user-agent (get headers "user-agent")
@ -57,7 +38,7 @@
(when (map? data) (when (map? data)
{:error-type (:type data) {:error-type (:type data)
:error-code (:code data)}))))) :error-code (:code data)}))))
(defmulti handle-exception (defmulti handle-exception
(fn [err & _rest] (fn [err & _rest]
@ -89,13 +70,9 @@
(defmethod handle-exception :assertion (defmethod handle-exception :assertion
[error request] [error request]
(let [edata (ex-data error) (let [edata (ex-data error)]
cdata (get-error-context request error)] (l/with-context (get-error-context request error)
(l/update-thread-context! cdata) (l/error :hint "internal error: assertion" :cause error))
(l/error :hint "internal error: assertion"
:error-id (str (:id cdata))
:cause error)
{:status 500 {:status 500
:body {:type :server-error :body {:type :server-error
:code :assertion :code :assertion
@ -116,12 +93,10 @@
(if (and (ex/exception? (:rollback edata)) (if (and (ex/exception? (:rollback edata))
(ex/exception? (:handling edata))) (ex/exception? (:handling edata)))
(handle-exception (:handling edata) request) (handle-exception (:handling edata) request)
(let [cdata (get-error-context request error)] (do
(l/update-thread-context! cdata) (l/with-context (get-error-context request error)
(l/error :hint "internal error" (l/error :hint (ex-message error) :cause error))
:error-message (ex-message error)
:error-id (str (:id cdata))
:cause error)
{:status 500 {:status 500
:body {:type :server-error :body {:type :server-error
:code :unexpected :code :unexpected
@ -130,15 +105,13 @@
(defmethod handle-exception org.postgresql.util.PSQLException (defmethod handle-exception org.postgresql.util.PSQLException
[error request] [error request]
(let [cdata (get-error-context request error) (let [state (.getSQLState ^java.sql.SQLException error)]
state (.getSQLState ^java.sql.SQLException error)]
(l/update-thread-context! cdata) (l/with-context (get-error-context request error)
(l/error :hint "psql exception" (l/error :hint "psql exception"
:error-message (ex-message error) :error-message (ex-message error)
:error-id (str (:id cdata)) :state state
:sql-state state :cause error))
:cause error)
(cond (cond
(= state "57014") (= state "57014")

View file

@ -73,7 +73,6 @@
(if-let [{:keys [id profile-id] :as session} (retrieve-from-request cfg request)] (if-let [{:keys [id profile-id] :as session} (retrieve-from-request cfg request)]
(do (do
(a/>!! (::events-ch cfg) id) (a/>!! (::events-ch cfg) id)
(l/update-thread-context! {:profile-id profile-id})
(handler (assoc request :profile-id profile-id))) (handler (assoc request :profile-id profile-id)))
(handler request)))) (handler request))))

View file

@ -266,13 +266,8 @@
(= ::noop (:strategy edata)) (= ::noop (:strategy edata))
(assoc :inc-by 0)) (assoc :inc-by 0))
(l/with-context (get-error-context error item)
(let [cdata (get-error-context error item)] (l/error :cause error :hint "unhandled exception on task")
(l/update-thread-context! cdata)
(l/error :cause error
:hint "unhandled exception on task"
:id (:id cdata))
(if (>= (:retry-num item) (:max-retries item)) (if (>= (:retry-num item) (:max-retries item))
{:status :failed :task item :error error} {:status :failed :task item :error error}
{:status :retry :task item :error error}))))) {:status :retry :task item :error error})))))

View file

@ -6,7 +6,7 @@
(ns app.common.data (ns app.common.data
"Data manipulation and query helper functions." "Data manipulation and query helper functions."
(:refer-clojure :exclude [read-string hash-map merge name]) (:refer-clojure :exclude [read-string hash-map merge name parse-double])
#?(:cljs #?(:cljs
(:require-macros [app.common.data])) (:require-macros [app.common.data]))
(:require (:require

View file

@ -17,9 +17,12 @@
org.apache.logging.log4j.LogManager org.apache.logging.log4j.LogManager
org.apache.logging.log4j.Logger org.apache.logging.log4j.Logger
org.apache.logging.log4j.ThreadContext org.apache.logging.log4j.ThreadContext
org.apache.logging.log4j.CloseableThreadContext
org.apache.logging.log4j.message.MapMessage org.apache.logging.log4j.message.MapMessage
org.apache.logging.log4j.spi.LoggerContext))) org.apache.logging.log4j.spi.LoggerContext)))
#?(:clj (set! *warn-on-reflection* true))
#?(:clj #?(:clj
(defn build-map-message (defn build-map-message
[m] [m]
@ -37,14 +40,13 @@
(defn get-logger (defn get-logger
[lname] [lname]
#?(:clj (.getLogger ^LoggerContext logger-context ^String lname) #?(:clj (.getLogger ^LoggerContext logger-context ^String lname)
:cljs :cljs (glog/getLogger
(glog/getLogger (cond
(cond (string? lname) lname
(string? lname) lname (= lname :root) ""
(= lname :root) "" (simple-ident? lname) (name lname)
(simple-ident? lname) (name lname) (qualified-ident? lname) (str (namespace lname) "." (name lname))
(qualified-ident? lname) (str (namespace lname) "." (name lname)) :else (str lname)))))
:else (str lname)))))
(defn get-level (defn get-level
[level] [level]
@ -151,20 +153,48 @@
;; CLJ Specific ;; CLJ Specific
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- simple-prune
([s] (simple-prune s (* 1024 1024)))
([s max-length]
(if (> (count s) max-length)
(str (subs s 0 max-length) " [...]")
s)))
#?(:clj #?(:clj
(defn update-thread-context! (defn stringify-data
[val]
(cond
(instance? clojure.lang.Named val)
(name val)
(string? val)
val
(coll? val)
(binding [clojure.pprint/*print-right-margin* 120]
(-> (with-out-str (pprint val))
(simple-prune (* 1024 1024 3))))
:else
(str val))))
#?(:clj
(defn data->context-map
^java.util.Map
[data] [data]
(run! (fn [[key val]] (into {}
(ThreadContext/put (comp (filter second)
(name key) (map (fn [[key val]]
(cond [(stringify-data key)
(coll? val) (stringify-data val)])))
(binding [clojure.pprint/*print-right-margin* 120]
(with-out-str (pprint val)))
(instance? clojure.lang.Named val) (name val)
:else (str val))))
data))) data)))
#?(:clj
(defmacro with-context
[data & body]
`(with-open [instance# (CloseableThreadContext/putAll (data->context-map ~data))]
~@body)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CLJS Specific ;; CLJS Specific
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

View file

@ -211,13 +211,11 @@
[spec x message context] [spec x message context]
(if (s/valid? spec x) (if (s/valid? spec x)
x x
(let [data (s/explain-data spec x) (let [data (s/explain-data spec x)]
explain (with-out-str (s/explain-out data))]
(ex/raise :type :assertion (ex/raise :type :assertion
:code :spec-validation :code :spec-validation
:hint message :hint message
:data data :data data
:explain explain
:context context :context context
#?@(:cljs [:stack (.-stack (ex-info message {}))]))))) #?@(:cljs [:stack (.-stack (ex-info message {}))])))))
@ -253,12 +251,9 @@
[spec data] [spec data]
(let [result (s/conform spec data)] (let [result (s/conform spec data)]
(when (= result ::s/invalid) (when (= result ::s/invalid)
(let [data (s/explain-data spec data) (let [data (s/explain-data spec data)]
explain (with-out-str
(s/explain-out data))]
(throw (ex/error :type :validation (throw (ex/error :type :validation
:code :spec-validation :code :spec-validation
:explain explain
:data data)))) :data data))))
result)) result))

View file

@ -81,10 +81,7 @@
(js/console.group "Validation Error:") (js/console.group "Validation Error:")
(ex/ignoring (ex/ignoring
(js/console.info (js/console.info
(with-out-str (with-out-str (pprint error))))
(pprint (dissoc error :explain))))
(when-let [explain (:explain error)]
(js/console.error explain)))
(js/console.groupEnd "Validation Error:")) (js/console.groupEnd "Validation Error:"))
@ -138,8 +135,7 @@
(defmethod ptk/handle-error :server-error (defmethod ptk/handle-error :server-error
[{:keys [data hint] :as error}] [{:keys [data hint] :as error}]
(let [hint (or hint (:hint data) (:message data)) (let [hint (or hint (:hint data) (:message data))
info (with-out-str (pprint (dissoc data :explain))) info (with-out-str (pprint data))
expl (:explain data)
msg (str "Internal Server Error: " hint)] msg (str "Internal Server Error: " hint)]
(ts/schedule (ts/schedule
@ -150,7 +146,6 @@
(js/console.group msg) (js/console.group msg)
(js/console.info info) (js/console.info info)
(when expl (js/console.error expl))
(js/console.groupEnd msg))) (js/console.groupEnd msg)))
(defn on-unhandled-error (defn on-unhandled-error