Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Better logging around token checks (#68625)

dpsutton503108d247b4
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index d538652bcbb..4664bfe75f5 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -969,7 +969,8 @@
:api #{metabase.premium-features.api
metabase.premium-features.core
metabase.premium-features.init}
- :uses #{api
+ :uses #{analytics
+ api
app-db
classloader
config
diff --git a/src/metabase/analytics/prometheus.clj b/src/metabase/analytics/prometheus.clj
index 4319dceb09e..88e64fccedc 100644
--- a/src/metabase/analytics/prometheus.clj
+++ b/src/metabase/analytics/prometheus.clj
@@ -490,6 +490,9 @@
:buckets [100 500 1000 5000 10000 30000 60000 300000 1800000 7200000 14400000 21600000]})
(prometheus/counter :metabase-transforms/python-api-calls-total
{:description "Total number of Python runner API calls."
+ :labels [:status]})
+ (prometheus/counter :metabase-token-check/attempt
+ {:description "Total number of token checks. Includes a status label."
:labels [:status]})])
(defn- quartz-collectors
@@ -627,6 +630,19 @@
(setup!))
(prometheus/inc (:registry system) metric (qualified-vals labels) amount)))
+(defn inc-if-initialized!
+ "Call iapetos.core/inc on the metric in the global registry.
+ Inits registry if it's not been initialized yet."
+ ([metric] (when system (inc! metric nil 1)))
+ ([metric labels-or-amount]
+ (when system
+ (if (number? labels-or-amount)
+ (inc! metric nil labels-or-amount)
+ (inc! metric labels-or-amount 1))))
+ ([metric labels amount]
+ (when system
+ (prometheus/inc (:registry system) metric (qualified-vals labels) amount))))
+
(defn dec!
"Call iapetos.core/dec on the metric in the global registry.
Inits registry if it's not been initialized yet.
diff --git a/src/metabase/analytics/settings.clj b/src/metabase/analytics/settings.clj
index 9d4ba7d3bea..4045e4e1280 100644
--- a/src/metabase/analytics/settings.clj
+++ b/src/metabase/analytics/settings.clj
@@ -1,7 +1,6 @@
(ns metabase.analytics.settings
(:require
[java-time.api :as t]
- [metabase.appearance.core :as appearance]
[metabase.config.core :as config]
[metabase.settings.core :as setting :refer [defsetting]]
[metabase.util.date-2 :as u.date]
@@ -33,8 +32,7 @@
:doc false)
(defsetting anon-tracking-enabled
- (deferred-tru "Enable the collection of anonymous usage data in order to help {0} improve."
- (setting/application-name-for-setting-descriptions appearance/application-name))
+ (deferred-tru "Enable the collection of anonymous usage data in order to help us improve.")
:type :boolean
:default true
:visibility :public
diff --git a/src/metabase/premium_features/token_check.clj b/src/metabase/premium_features/token_check.clj
index e37c31821ca..0e1905e5a53 100644
--- a/src/metabase/premium_features/token_check.clj
+++ b/src/metabase/premium_features/token_check.clj
@@ -13,6 +13,7 @@
[diehard.core :as dh]
[environ.core :refer [env]]
[java-time.api :as t]
+ [metabase.analytics.prometheus :as analytics]
[metabase.config.core :as config]
[metabase.events.core :as events]
[metabase.internal-stats.core :as internal-stats]
@@ -185,15 +186,17 @@
(log/infof "Checking with the MetaStore to see whether token '%s' is valid..." (u.str/mask token))
(let [{:keys [body status] :as resp} (http-fetch base-url token site-uuid)]
(cond
- (http/success? resp) (some-> body json/decode+kw)
+ (http/success? resp) (do (analytics/inc-if-initialized! :metabase-token-check/attempt {:status :success})
+ (some-> body json/decode+kw))
(<= 400 status 499) (or (some-> body json/decode+kw)
{:valid false
:status "Unable to validate token"
:error-details "Token validation provided no response"})
;; exceptions are not cached.
- :else (throw (ex-info "An unknown error occurred when validating token." {:status status
- :body body})))))
+ :else (do (analytics/inc-if-initialized! :metabase-token-check/attempt {:status :failure})
+ (throw (ex-info "An unknown error occurred when validating token." {:status status
+ :body body}))))))
(defn- metering-url
[token base-url]
@@ -354,7 +357,7 @@
;; to circuit break. (#65294)
(when-not ((requiring-resolve 'metabase.app-db.core/db-is-set-up?))
(throw (ex-info "Metabase DB is not yet set up"
- {:reason :token-check/app-db-not-ready})))
+ {:cause :token-check/app-db-not-ready})))
(locking lock
(binding [*token-check-happening* true]
(try (dh/with-circuit-breaker breaker
@@ -363,7 +366,7 @@
(-check-token token-checker token)))
(catch dev.failsafe.CircuitBreakerOpenException _e
(throw (ex-info (tru "Token validation is currently unavailable.")
- {:cause :circuit-breaker})))
+ {:cause :token-check/circuit-breaker})))
;; other exceptions are wrapped by Diehard in a FailsafeException. Unwrap them before
;; rethrowing.
(catch dev.failsafe.FailsafeException e
@@ -416,7 +419,9 @@
(-check-token [_ token]
(try (-check-token token-checker token)
(catch Exception e
- (u/ignore-exceptions (some-> (ex-data e) :body json/decode+kw))
+ (when-not (-> e ex-data :cause #{:token-check/circuit-breaker :token-check/app-db-not-ready})
+ (log/infof "Error checking token: %s" (ex-message e))
+ (analytics/inc-if-initialized! :metabase-token-check/attempt {:status :failure}))
{:valid false
:status (tru "Unable to validate token")
:error-details (.getMessage e)})))
@@ -453,12 +458,16 @@
"The token checker. Combines http/airgapping validation, circuit breaking, grace periods, caching, and error
handling."
(make-checker {:base store-and-airgap-token-checker
- :circuit-breaker {:failure-threshold-ratio-in-period [10 10 (u/seconds->ms 10)]
- :delay-ms (u/seconds->ms 30)
- :success-threshold 1}
- :timeout-ms (u/seconds->ms 10)
- :ttl-ms (u/hours->ms 12)
- :grace-period (guava-cache-grace-period 36 TimeUnit/HOURS)}))
+ :circuit-breaker {:failure-threshold-ratio-in-period [10 10 (u/seconds->ms 60)]
+
+ :delay-ms (u/seconds->ms 30)
+ :success-threshold 1
+ :on-open (fn [_] (log/info "Engaging circuit breaker in token check"))
+ :on-half-open (fn [_] (log/info "In token check circuit breaker but attempting check"))
+ :on-close (fn [_] (log/info "Token Check restored"))}
+ :timeout-ms (u/seconds->ms 10)
+ :ttl-ms (u/hours->ms 12)
+ :grace-period (guava-cache-grace-period 36 TimeUnit/HOURS)}))
(defn clear-cache!
"Clear the token cache so that [[fetch-token-and-parse-body]] will return the latest data."