🎉 Add specific profile registration and activation metrics.

This commit is contained in:
Andrey Antukh 2021-02-22 12:52:27 +01:00 committed by Andrés Moya
parent 5ce450f578
commit 19f098359b
3 changed files with 84 additions and 49 deletions

View file

@ -37,7 +37,15 @@
:max-pool-size 20} :max-pool-size 20}
:app.metrics/metrics :app.metrics/metrics
{} {:definitions
{:profile-register
{: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}}}
:app.migrations/all :app.migrations/all
{:main (ig/ref :app.migrations/migrations) {:main (ig/ref :app.migrations/migrations)

View file

@ -26,7 +26,6 @@
[app.util.time :as dt] [app.util.time :as dt]
[buddy.hashers :as hashers] [buddy.hashers :as hashers]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.tools.logging :as log]
[cuerdas.core :as str])) [cuerdas.core :as str]))
;; --- Helpers & Specs ;; --- Helpers & Specs
@ -42,10 +41,12 @@
;; --- Mutation: Register Profile ;; --- Mutation: Register Profile
(declare annotate-profile-register)
(declare check-profile-existence!) (declare check-profile-existence!)
(declare create-profile) (declare create-profile)
(declare create-profile-relations) (declare create-profile-relations)
(declare email-domain-in-whitelist?) (declare email-domain-in-whitelist?)
(declare register-profile)
(s/def ::invitation-token ::us/not-empty-string) (s/def ::invitation-token ::us/not-empty-string)
(s/def ::register-profile (s/def ::register-profile
@ -63,9 +64,23 @@
:code :email-domain-is-not-allowed)) :code :email-domain-is-not-allowed))
(db/with-atomic [conn pool] (db/with-atomic [conn pool]
(let [cfg (assoc cfg :conn conn)]
(register-profile cfg params))))
(defn- annotate-profile-register
"A helper for properly increase the profile-register metric once the
transaction is completed."
[metrics profile]
(fn []
(when (::created profile)
((get-in metrics [:definitions :profile-register]) :inc))))
(defn- register-profile
[{:keys [conn tokens session metrics] :as cfg} params]
(check-profile-existence! conn params) (check-profile-existence! conn params)
(let [profile (->> (create-profile conn params) (let [profile (->> (create-profile conn params)
(create-profile-relations conn))] (create-profile-relations conn))
profile (assoc profile ::created true)]
(create-profile-initial-data conn profile) (create-profile-initial-data conn profile)
(if-let [token (:invitation-token params)] (if-let [token (:invitation-token params)]
@ -77,10 +92,11 @@
claims (assoc claims claims (assoc claims
:member-id (:id profile) :member-id (:id profile)
:member-email (:email profile)) :member-email (:email profile))
token (tokens :generate claims)] token (tokens :generate claims)
(with-meta resp {:invitation-token token}]
{:invitation-token token} (with-meta resp
{:transform-response ((:create session) (:id profile))})) {:transform-response ((:create session) (:id profile))
:before-complete (annotate-profile-register metrics profile)}))
;; If no token is provided, send a verification email ;; If no token is provided, send a verification email
(let [vtoken (tokens :generate (let [vtoken (tokens :generate
@ -104,7 +120,8 @@
:name (:fullname profile) :name (:fullname profile)
:token vtoken :token vtoken
:extra-data ptoken}) :extra-data ptoken})
profile))))) (with-meta profile
{:before-complete (annotate-profile-register metrics profile)})))))
(defn email-domain-in-whitelist? (defn email-domain-in-whitelist?
"Returns true if email's domain is in the given whitelist or if given "Returns true if email's domain is in the given whitelist or if given
@ -142,7 +159,7 @@
[attempt password] [attempt password]
(try (try
(hashers/verify attempt password) (hashers/verify attempt password)
(catch Exception e (catch Exception _e
{:update false {:update false
:valid false}))) :valid false})))
@ -268,10 +285,12 @@
(s/keys :req-un [::email ::fullname ::backend])) (s/keys :req-un [::email ::fullname ::backend]))
(sv/defmethod ::login-or-register {:auth false} (sv/defmethod ::login-or-register {:auth false}
[{:keys [pool] :as cfg} params] [{:keys [pool metrics] :as cfg} params]
(db/with-atomic [conn pool] (db/with-atomic [conn pool]
(-> (assoc cfg :conn conn) (let [profile (-> (assoc cfg :conn conn)
(login-or-register params)))) (login-or-register params))]
(with-meta profile
{:before-complete (annotate-profile-register metrics profile)}))))
(defn login-or-register (defn login-or-register
[{:keys [conn] :as cfg} {:keys [email backend] :as params}] [{:keys [conn] :as cfg} {:keys [email backend] :as params}]
@ -293,7 +312,7 @@
(let [profile (->> (create-profile conn params) (let [profile (->> (create-profile conn params)
(create-profile-relations conn))] (create-profile-relations conn))]
(create-profile-initial-data conn profile) (create-profile-initial-data conn profile)
profile))] (assoc profile ::created true)))]
(let [profile (profile/retrieve-profile-data-by-email conn email) (let [profile (profile/retrieve-profile-data-by-email conn email)
profile (if profile profile (if profile

View file

@ -40,8 +40,15 @@
{:id profile-id}) {:id profile-id})
claims) claims)
(defn- annotate-profile-activation
"A helper for properly increase the profile-activation metric once the
transaction is completed."
[metrics]
(fn []
((get-in metrics [:definitions :profile-activation]) :inc)))
(defmethod process-token :verify-email (defmethod process-token :verify-email
[{:keys [conn session] :as cfg} _params {:keys [profile-id] :as claims}] [{:keys [conn session metrics] :as cfg} _ {:keys [profile-id] :as claims}]
(let [profile (profile/retrieve-profile conn profile-id) (let [profile (profile/retrieve-profile conn profile-id)
claims (assoc claims :profile profile)] claims (assoc claims :profile profile)]
@ -56,7 +63,8 @@
{:id (:id profile)})) {:id (:id profile)}))
(with-meta claims (with-meta claims
{:transform-response ((:create session) profile-id)}))) {:transform-response ((:create session) profile-id)
:before-complete (annotate-profile-activation metrics)})))
(defmethod process-token :auth (defmethod process-token :auth
[{:keys [conn] :as cfg} _params {:keys [profile-id] :as claims}] [{:keys [conn] :as cfg} _params {:keys [profile-id] :as claims}]