diff --git a/backend/src/app/main.clj b/backend/src/app/main.clj index 8baefbbb9..bfa65c4d4 100644 --- a/backend/src/app/main.clj +++ b/backend/src/app/main.clj @@ -28,11 +28,24 @@ {:name "actions_profile_register_count" :help "A global counter of user registrations." :type :counter} + :profile-activation {:name "actions_profile_activation_count" :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}}} + + :app.migrations/all {:main (ig/ref :app.migrations/migrations)} diff --git a/backend/src/app/metrics.clj b/backend/src/app/metrics.clj index 74fb318ac..148e9b3a2 100644 --- a/backend/src/app/metrics.clj +++ b/backend/src/app/metrics.clj @@ -92,18 +92,14 @@ _ (when (seq labels) (.labelNames instance (into-array String labels))) instance (.register instance registry)] - (reify - clojure.lang.IDeref - (deref [_] instance) - clojure.lang.IFn - (invoke [_ cmd] - (.inc ^Counter instance)) - - (invoke [_ cmd labels] - (.. ^Counter instance - (labels (into-array String labels)) - (inc)))))) + {::instance instance + ::fn (fn [{:keys [by labels] :or {by 1}}] + (if labels + (.. ^Counter instance + (labels (into-array String labels)) + (inc by)) + (.inc ^Counter instance by)))})) (defn make-gauge [{:keys [name help registry reg labels] :as props}] @@ -115,21 +111,16 @@ (.labelNames instance (into-array String labels))) instance (.register instance registry)] - (reify - clojure.lang.IDeref - (deref [_] instance) - - clojure.lang.IFn - (invoke [_ cmd] - (case cmd - :inc (.inc ^Gauge instance) - :dec (.dec ^Gauge instance))) - - (invoke [_ cmd labels] - (let [labels (into-array String [labels])] - (case cmd - :inc (.. ^Gauge instance (labels labels) (inc)) - :dec (.. ^Gauge instance (labels labels) (dec)))))))) + {::instance instance + ::fn (fn [{:keys [cmd by labels] :or {by 1}}] + (if labels + (let [labels (into-array String [labels])] + (case cmd + :inc (.. ^Gauge instance (labels labels) (inc by)) + :dec (.. ^Gauge instance (labels labels) (dec by)))) + (case cmd + :inc (.inc ^Gauge instance by) + :dec (.dec ^Gauge instance by))))})) (def default-quantiles [[0.75 0.02] @@ -150,18 +141,14 @@ _ (when (seq labels) (.labelNames instance (into-array String labels))) instance (.register instance registry)] - (reify - clojure.lang.IDeref - (deref [_] instance) - clojure.lang.IFn - (invoke [_ cmd val] - (.observe ^Summary instance val)) - - (invoke [_ cmd val labels] - (.. ^Summary instance - (labels (into-array String labels)) - (observe val)))))) + {::instance instance + ::fn (fn [{:keys [val labels]}] + (if labels + (.. ^Summary instance + (labels (into-array String labels)) + (observe val)) + (.observe ^Summary instance val)))})) (def default-histogram-buckets [1 5 10 25 50 75 100 250 500 750 1000 2500 5000 7500]) @@ -177,18 +164,14 @@ _ (when (seq labels) (.labelNames instance (into-array String labels))) instance (.register instance registry)] - (reify - clojure.lang.IDeref - (deref [_] instance) - clojure.lang.IFn - (invoke [_ cmd val] - (.observe ^Histogram instance val)) - - (invoke [_ cmd val labels] - (.. ^Histogram instance - (labels (into-array String labels)) - (observe val)))))) + {::instance instance + ::fn (fn [{:keys [val labels]}] + (if labels + (.. ^Histogram instance + (labels (into-array String labels)) + (observe val)) + (.observe ^Histogram instance val)))})) (defn create [{:keys [type] :as props}] @@ -205,19 +188,19 @@ (with-meta (fn ([a] - (mobj :inc) + ((::fn mobj) nil) (origf a)) ([a b] - (mobj :inc) + ((::fn mobj) nil) (origf a b)) ([a b c] - (mobj :inc) + ((::fn mobj) nil) (origf a b c)) ([a b c d] - (mobj :inc) + ((::fn mobj) nil) (origf a b c d)) ([a b c d & more] - (mobj :inc) + ((::fn mobj) nil) (apply origf a b c d more))) (assoc mdata ::original origf)))) ([rootf mobj labels] @@ -226,13 +209,13 @@ (with-meta (fn ([a] - (mobj :inc labels) + ((::fn mobj) {:labels labels}) (origf a)) ([a b] - (mobj :inc labels) + ((::fn mobj) {:labels labels}) (origf a b)) ([a b & more] - (mobj :inc labels) + ((::fn mobj) {:labels labels}) (apply origf a b more))) (assoc mdata ::original origf))))) @@ -245,15 +228,15 @@ ([a] (with-measure :expr (origf a) - :cb #(mobj :observe %))) + :cb #((::fn mobj) {:val %}))) ([a b] (with-measure :expr (origf a b) - :cb #(mobj :observe %))) + :cb #((::fn mobj) {:val %}))) ([a b & more] (with-measure :expr (apply origf a b more) - :cb #(mobj :observe %)))) + :cb #((::fn mobj) {:val %})))) (assoc mdata ::original origf)))) ([rootf mobj labels] @@ -264,26 +247,26 @@ ([a] (with-measure :expr (origf a) - :cb #(mobj :observe % labels))) + :cb #((::fn mobj) {:val % :labels labels}))) ([a b] (with-measure :expr (origf a b) - :cb #(mobj :observe % labels))) + :cb #((::fn mobj) {:val % :labels labels}))) ([a b & more] (with-measure :expr (apply origf a b more) - :cb #(mobj :observe % labels)))) + :cb #((::fn mobj) {:val % :labels labels})))) (assoc mdata ::original origf))))) (defn instrument-vars! [vars {:keys [wrap] :as props}] (let [obj (create props)] (cond - (instance? Counter @obj) + (instance? Counter (::instance obj)) (doseq [var vars] (alter-var-root var (or wrap wrap-counter) obj)) - (instance? Summary @obj) + (instance? Summary (::instance obj)) (doseq [var vars] (alter-var-root var (or wrap wrap-summary) obj)) @@ -294,13 +277,13 @@ [f {:keys [wrap] :as props}] (let [obj (create props)] (cond - (instance? Counter @obj) + (instance? Counter (::instance obj)) ((or wrap wrap-counter) f obj) - (instance? Summary @obj) + (instance? Summary (::instance obj)) ((or wrap wrap-summary) f obj) - (instance? Histogram @obj) + (instance? Histogram (::instance obj)) ((or wrap wrap-summary) f obj) :else diff --git a/backend/src/app/rpc/mutations/files.clj b/backend/src/app/rpc/mutations/files.clj index 6436f0fc9..d84911ef8 100644 --- a/backend/src/app/rpc/mutations/files.clj +++ b/backend/src/app/rpc/mutations/files.clj @@ -12,6 +12,7 @@ [app.common.spec :as us] [app.common.uuid :as uuid] [app.db :as db] + [app.metrics :as mtx] [app.rpc.permissions :as perms] [app.rpc.queries.files :as files] [app.rpc.queries.projects :as proj] @@ -291,7 +292,7 @@ (simpl/del-object backend 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) (:revn file)) @@ -301,14 +302,22 @@ :context {:incoming-revn (:revn params) :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) changes) + ;; Trace the number of changes processed + _ ((::mtx/fn mtx1) {:by (count changes)}) + ts (dt/now) file (-> (files/retrieve-data cfg file) (update :revn inc) (update :data (fn [data] + ;; Trace the length of bytes of processed data + ((::mtx/fn mtx2) {:by (alength data)}) (-> data (blob/decode) (assoc :id (:id file)) diff --git a/backend/src/app/rpc/mutations/profile.clj b/backend/src/app/rpc/mutations/profile.clj index cc1dce079..099092ba5 100644 --- a/backend/src/app/rpc/mutations/profile.clj +++ b/backend/src/app/rpc/mutations/profile.clj @@ -15,6 +15,7 @@ [app.http.oauth :refer [extract-props]] [app.loggers.audit :as audit] [app.media :as media] + [app.metrics :as mtx] [app.rpc.mutations.projects :as projects] [app.rpc.mutations.teams :as teams] [app.rpc.queries.profile :as profile] @@ -150,7 +151,8 @@ transaction is completed." [metrics] (fn [] - ((get-in metrics [:definitions :profile-register]) :inc))) + (let [mobj (get-in metrics [:definitions :profile-register])] + ((::mtx/fn mobj) {:by 1})))) (defn register-profile [{:keys [conn tokens session metrics] :as cfg} {:keys [token] :as params}] diff --git a/backend/src/app/rpc/mutations/verify_token.clj b/backend/src/app/rpc/mutations/verify_token.clj index e294cfb73..61b5f9abb 100644 --- a/backend/src/app/rpc/mutations/verify_token.clj +++ b/backend/src/app/rpc/mutations/verify_token.clj @@ -9,6 +9,7 @@ [app.common.exceptions :as ex] [app.common.spec :as us] [app.db :as db] + [app.metrics :as mtx] [app.rpc.mutations.teams :as teams] [app.rpc.queries.profile :as profile] [app.util.services :as sv] @@ -42,7 +43,8 @@ transaction is completed." [metrics] (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 [{:keys [conn session metrics] :as cfg} _ {:keys [profile-id] :as claims}] diff --git a/backend/test/app/test_helpers.clj b/backend/test/app/test_helpers.clj index c7541a4a0..ba8999123 100644 --- a/backend/test/app/test_helpers.clj +++ b/backend/test/app/test_helpers.clj @@ -228,9 +228,12 @@ ([params] (update-file* *pool* params)) ([conn {:keys [file-id changes session-id profile-id revn] :or {session-id (uuid/next) revn 0}}] - (let [file (db/get-by-id conn :file file-id) - msgbus (:app.msgbus/msgbus *system*)] - (#'files/update-file {:conn conn :msgbus msgbus} + (let [file (db/get-by-id conn :file file-id) + msgbus (:app.msgbus/msgbus *system*) + metrics (:app.metrics/metrics *system*)] + (#'files/update-file {:conn conn + :msgbus msgbus + :metrics metrics} {:file file :revn revn :changes changes