Add more metrics to update-file.

This commit is contained in:
Andrey Antukh 2021-09-02 16:38:02 +02:00 committed by Andrés Moya
parent 320a4552bc
commit f6e18de6af
6 changed files with 86 additions and 74 deletions

View file

@ -28,11 +28,24 @@
{:name "actions_profile_register_count" {:name "actions_profile_register_count"
:help "A global counter of user registrations." :help "A global counter of user registrations."
:type :counter} :type :counter}
:profile-activation :profile-activation
{:name "actions_profile_activation_count" {:name "actions_profile_activation_count"
:help "A global counter of profile activations" :help "A global counter of profile activations"
:type :counter}
:update-file-changes
{:name "rpc_update_file_changes_total"
:help "A total number of changes submitted to update-file."
:type :counter}
:update-file-bytes-processed
{:name "rpc_update_file_bytes_processed_total"
:help "A total number of bytes processed by update-file."
:type :counter}}} :type :counter}}}
:app.migrations/all :app.migrations/all
{:main (ig/ref :app.migrations/migrations)} {:main (ig/ref :app.migrations/migrations)}

View file

@ -92,18 +92,14 @@
_ (when (seq labels) _ (when (seq labels)
(.labelNames instance (into-array String labels))) (.labelNames instance (into-array String labels)))
instance (.register instance registry)] instance (.register instance registry)]
(reify
clojure.lang.IDeref
(deref [_] instance)
clojure.lang.IFn {::instance instance
(invoke [_ cmd] ::fn (fn [{:keys [by labels] :or {by 1}}]
(.inc ^Counter instance)) (if labels
(.. ^Counter instance
(invoke [_ cmd labels] (labels (into-array String labels))
(.. ^Counter instance (inc by))
(labels (into-array String labels)) (.inc ^Counter instance by)))}))
(inc))))))
(defn make-gauge (defn make-gauge
[{:keys [name help registry reg labels] :as props}] [{:keys [name help registry reg labels] :as props}]
@ -115,21 +111,16 @@
(.labelNames instance (into-array String labels))) (.labelNames instance (into-array String labels)))
instance (.register instance registry)] instance (.register instance registry)]
(reify {::instance instance
clojure.lang.IDeref ::fn (fn [{:keys [cmd by labels] :or {by 1}}]
(deref [_] instance) (if labels
(let [labels (into-array String [labels])]
clojure.lang.IFn (case cmd
(invoke [_ cmd] :inc (.. ^Gauge instance (labels labels) (inc by))
(case cmd :dec (.. ^Gauge instance (labels labels) (dec by))))
:inc (.inc ^Gauge instance) (case cmd
:dec (.dec ^Gauge instance))) :inc (.inc ^Gauge instance by)
:dec (.dec ^Gauge instance by))))}))
(invoke [_ cmd labels]
(let [labels (into-array String [labels])]
(case cmd
:inc (.. ^Gauge instance (labels labels) (inc))
:dec (.. ^Gauge instance (labels labels) (dec))))))))
(def default-quantiles (def default-quantiles
[[0.75 0.02] [[0.75 0.02]
@ -150,18 +141,14 @@
_ (when (seq labels) _ (when (seq labels)
(.labelNames instance (into-array String labels))) (.labelNames instance (into-array String labels)))
instance (.register instance registry)] instance (.register instance registry)]
(reify
clojure.lang.IDeref
(deref [_] instance)
clojure.lang.IFn {::instance instance
(invoke [_ cmd val] ::fn (fn [{:keys [val labels]}]
(.observe ^Summary instance val)) (if labels
(.. ^Summary instance
(invoke [_ cmd val labels] (labels (into-array String labels))
(.. ^Summary instance (observe val))
(labels (into-array String labels)) (.observe ^Summary instance val)))}))
(observe val))))))
(def default-histogram-buckets (def default-histogram-buckets
[1 5 10 25 50 75 100 250 500 750 1000 2500 5000 7500]) [1 5 10 25 50 75 100 250 500 750 1000 2500 5000 7500])
@ -177,18 +164,14 @@
_ (when (seq labels) _ (when (seq labels)
(.labelNames instance (into-array String labels))) (.labelNames instance (into-array String labels)))
instance (.register instance registry)] instance (.register instance registry)]
(reify
clojure.lang.IDeref
(deref [_] instance)
clojure.lang.IFn {::instance instance
(invoke [_ cmd val] ::fn (fn [{:keys [val labels]}]
(.observe ^Histogram instance val)) (if labels
(.. ^Histogram instance
(invoke [_ cmd val labels] (labels (into-array String labels))
(.. ^Histogram instance (observe val))
(labels (into-array String labels)) (.observe ^Histogram instance val)))}))
(observe val))))))
(defn create (defn create
[{:keys [type] :as props}] [{:keys [type] :as props}]
@ -205,19 +188,19 @@
(with-meta (with-meta
(fn (fn
([a] ([a]
(mobj :inc) ((::fn mobj) nil)
(origf a)) (origf a))
([a b] ([a b]
(mobj :inc) ((::fn mobj) nil)
(origf a b)) (origf a b))
([a b c] ([a b c]
(mobj :inc) ((::fn mobj) nil)
(origf a b c)) (origf a b c))
([a b c d] ([a b c d]
(mobj :inc) ((::fn mobj) nil)
(origf a b c d)) (origf a b c d))
([a b c d & more] ([a b c d & more]
(mobj :inc) ((::fn mobj) nil)
(apply origf a b c d more))) (apply origf a b c d more)))
(assoc mdata ::original origf)))) (assoc mdata ::original origf))))
([rootf mobj labels] ([rootf mobj labels]
@ -226,13 +209,13 @@
(with-meta (with-meta
(fn (fn
([a] ([a]
(mobj :inc labels) ((::fn mobj) {:labels labels})
(origf a)) (origf a))
([a b] ([a b]
(mobj :inc labels) ((::fn mobj) {:labels labels})
(origf a b)) (origf a b))
([a b & more] ([a b & more]
(mobj :inc labels) ((::fn mobj) {:labels labels})
(apply origf a b more))) (apply origf a b more)))
(assoc mdata ::original origf))))) (assoc mdata ::original origf)))))
@ -245,15 +228,15 @@
([a] ([a]
(with-measure (with-measure
:expr (origf a) :expr (origf a)
:cb #(mobj :observe %))) :cb #((::fn mobj) {:val %})))
([a b] ([a b]
(with-measure (with-measure
:expr (origf a b) :expr (origf a b)
:cb #(mobj :observe %))) :cb #((::fn mobj) {:val %})))
([a b & more] ([a b & more]
(with-measure (with-measure
:expr (apply origf a b more) :expr (apply origf a b more)
:cb #(mobj :observe %)))) :cb #((::fn mobj) {:val %}))))
(assoc mdata ::original origf)))) (assoc mdata ::original origf))))
([rootf mobj labels] ([rootf mobj labels]
@ -264,26 +247,26 @@
([a] ([a]
(with-measure (with-measure
:expr (origf a) :expr (origf a)
:cb #(mobj :observe % labels))) :cb #((::fn mobj) {:val % :labels labels})))
([a b] ([a b]
(with-measure (with-measure
:expr (origf a b) :expr (origf a b)
:cb #(mobj :observe % labels))) :cb #((::fn mobj) {:val % :labels labels})))
([a b & more] ([a b & more]
(with-measure (with-measure
:expr (apply origf a b more) :expr (apply origf a b more)
:cb #(mobj :observe % labels)))) :cb #((::fn mobj) {:val % :labels labels}))))
(assoc mdata ::original origf))))) (assoc mdata ::original origf)))))
(defn instrument-vars! (defn instrument-vars!
[vars {:keys [wrap] :as props}] [vars {:keys [wrap] :as props}]
(let [obj (create props)] (let [obj (create props)]
(cond (cond
(instance? Counter @obj) (instance? Counter (::instance obj))
(doseq [var vars] (doseq [var vars]
(alter-var-root var (or wrap wrap-counter) obj)) (alter-var-root var (or wrap wrap-counter) obj))
(instance? Summary @obj) (instance? Summary (::instance obj))
(doseq [var vars] (doseq [var vars]
(alter-var-root var (or wrap wrap-summary) obj)) (alter-var-root var (or wrap wrap-summary) obj))
@ -294,13 +277,13 @@
[f {:keys [wrap] :as props}] [f {:keys [wrap] :as props}]
(let [obj (create props)] (let [obj (create props)]
(cond (cond
(instance? Counter @obj) (instance? Counter (::instance obj))
((or wrap wrap-counter) f obj) ((or wrap wrap-counter) f obj)
(instance? Summary @obj) (instance? Summary (::instance obj))
((or wrap wrap-summary) f obj) ((or wrap wrap-summary) f obj)
(instance? Histogram @obj) (instance? Histogram (::instance obj))
((or wrap wrap-summary) f obj) ((or wrap wrap-summary) f obj)
:else :else

View file

@ -12,6 +12,7 @@
[app.common.spec :as us] [app.common.spec :as us]
[app.common.uuid :as uuid] [app.common.uuid :as uuid]
[app.db :as db] [app.db :as db]
[app.metrics :as mtx]
[app.rpc.permissions :as perms] [app.rpc.permissions :as perms]
[app.rpc.queries.files :as files] [app.rpc.queries.files :as files]
[app.rpc.queries.projects :as proj] [app.rpc.queries.projects :as proj]
@ -291,7 +292,7 @@
(simpl/del-object backend file))) (simpl/del-object backend file)))
(defn- update-file (defn- update-file
[{:keys [conn] :as cfg} {:keys [file changes changes-with-metadata session-id profile-id] :as params}] [{:keys [conn metrics] :as cfg} {:keys [file changes changes-with-metadata session-id profile-id] :as params}]
(when (> (:revn params) (when (> (:revn params)
(:revn file)) (:revn file))
@ -301,14 +302,22 @@
:context {:incoming-revn (:revn params) :context {:incoming-revn (:revn params)
:stored-revn (:revn file)})) :stored-revn (:revn file)}))
(let [changes (if changes-with-metadata (let [mtx1 (get-in metrics [:definitions :update-file-changes])
mtx2 (get-in metrics [:definitions :update-file-bytes-processed])
changes (if changes-with-metadata
(mapcat :changes changes-with-metadata) (mapcat :changes changes-with-metadata)
changes) changes)
;; Trace the number of changes processed
_ ((::mtx/fn mtx1) {:by (count changes)})
ts (dt/now) ts (dt/now)
file (-> (files/retrieve-data cfg file) file (-> (files/retrieve-data cfg file)
(update :revn inc) (update :revn inc)
(update :data (fn [data] (update :data (fn [data]
;; Trace the length of bytes of processed data
((::mtx/fn mtx2) {:by (alength data)})
(-> data (-> data
(blob/decode) (blob/decode)
(assoc :id (:id file)) (assoc :id (:id file))

View file

@ -15,6 +15,7 @@
[app.http.oauth :refer [extract-props]] [app.http.oauth :refer [extract-props]]
[app.loggers.audit :as audit] [app.loggers.audit :as audit]
[app.media :as media] [app.media :as media]
[app.metrics :as mtx]
[app.rpc.mutations.projects :as projects] [app.rpc.mutations.projects :as projects]
[app.rpc.mutations.teams :as teams] [app.rpc.mutations.teams :as teams]
[app.rpc.queries.profile :as profile] [app.rpc.queries.profile :as profile]
@ -150,7 +151,8 @@
transaction is completed." transaction is completed."
[metrics] [metrics]
(fn [] (fn []
((get-in metrics [:definitions :profile-register]) :inc))) (let [mobj (get-in metrics [:definitions :profile-register])]
((::mtx/fn mobj) {:by 1}))))
(defn register-profile (defn register-profile
[{:keys [conn tokens session metrics] :as cfg} {:keys [token] :as params}] [{:keys [conn tokens session metrics] :as cfg} {:keys [token] :as params}]

View file

@ -9,6 +9,7 @@
[app.common.exceptions :as ex] [app.common.exceptions :as ex]
[app.common.spec :as us] [app.common.spec :as us]
[app.db :as db] [app.db :as db]
[app.metrics :as mtx]
[app.rpc.mutations.teams :as teams] [app.rpc.mutations.teams :as teams]
[app.rpc.queries.profile :as profile] [app.rpc.queries.profile :as profile]
[app.util.services :as sv] [app.util.services :as sv]
@ -42,7 +43,8 @@
transaction is completed." transaction is completed."
[metrics] [metrics]
(fn [] (fn []
((get-in metrics [:definitions :profile-activation]) :inc))) (let [mobj (get-in metrics [:definitions :profile-activation])]
((::mtx/fn mobj) {:by 1}))))
(defmethod process-token :verify-email (defmethod process-token :verify-email
[{:keys [conn session metrics] :as cfg} _ {:keys [profile-id] :as claims}] [{:keys [conn session metrics] :as cfg} _ {:keys [profile-id] :as claims}]

View file

@ -228,9 +228,12 @@
([params] (update-file* *pool* params)) ([params] (update-file* *pool* params))
([conn {:keys [file-id changes session-id profile-id revn] ([conn {:keys [file-id changes session-id profile-id revn]
:or {session-id (uuid/next) revn 0}}] :or {session-id (uuid/next) revn 0}}]
(let [file (db/get-by-id conn :file file-id) (let [file (db/get-by-id conn :file file-id)
msgbus (:app.msgbus/msgbus *system*)] msgbus (:app.msgbus/msgbus *system*)
(#'files/update-file {:conn conn :msgbus msgbus} metrics (:app.metrics/metrics *system*)]
(#'files/update-file {:conn conn
:msgbus msgbus
:metrics metrics}
{:file file {:file file
:revn revn :revn revn
:changes changes :changes changes