Skip to content

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

enable metabot stats and prevent recursive token checks (#63574)

Oleksandr Solovyov50e10ee9c296
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/core.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/core.clj
index e6d333f1148..485c6eb4cf0 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/core.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/core.clj
@@ -25,13 +25,14 @@
(defenterprise metabot-stats
"Calculate total Metabot token usage over a window of the the previous UTC day 00:00-23:59"
- :feature :metabot-v3
+ :feature :none
[]
(let [yesterday-utc (-> (t/offset-date-time (t/zone-offset "+00"))
(t/minus (t/days 1)))]
- {:metabot-tokens (t2/select-one-fn :sum
- [:model/MetabotMessage [:%sum.total_tokens :sum]]
- {:where [:= [:cast :created_at :date] [:cast yesterday-utc :date]]})
+ {:metabot-tokens (or (t2/select-one-fn :sum
+ [:model/MetabotMessage [:%sum.total_tokens :sum]]
+ {:where [:= [:cast :created_at :date] [:cast yesterday-utc :date]]})
+ 0)
:metabot-queries (t2/select-one-fn :cnt
[:model/MetabotMessage [:%count.id :cnt]]
;; there are requests from users and responses from ai-service, in theory
diff --git a/src/metabase/premium_features/token_check.clj b/src/metabase/premium_features/token_check.clj
index 57b453be344..194502ca692 100644
--- a/src/metabase/premium_features/token_check.clj
+++ b/src/metabase/premium_features/token_check.clj
@@ -112,13 +112,15 @@
(defn- stats-for-token-request
[]
+ ;; NOTE: beware, if you use `defenterprise` here which uses any other `:feature` other than `:none`, it will
+ ;; recursively trigger token check and will die
(let [users (premium-features.settings/active-users-count)
ext-users (internal-stats/external-users-count)
embedding-dashboard-count (internal-stats/embedding-dashboard-count)
embedding-question-count (internal-stats/embedding-question-count)
stats (merge (internal-stats/query-execution-last-utc-day)
(embedding-settings embedding-dashboard-count embedding-question-count)
- #_(metabot-stats)
+ (metabot-stats)
{:users users
:embedding-dashboard-count embedding-dashboard-count
:embedding-question-count embedding-question-count
@@ -282,12 +284,18 @@
:status "invalid"
:error-details (trs "Token should be a valid 64 hexadecimal character token or an airgap token.")})))
+(def ^:dynamic *token-check-happening* "Var to prevent recursive calls to `fetch-token-status`" false)
+
(let [lock (Object.)]
(defn- fetch-token-status
"Locked version of `fetch-token-status*` allowing one request at a time."
[token]
+ (when *token-check-happening*
+ (throw (ex-info "Token check is being called recursively, there is a good chance some `defenterprise` is causing this"
+ {:pass-thru true})))
(locking lock
- (fetch-token-status* token))))
+ (binding [*token-check-happening* true]
+ (fetch-token-status* token)))))
(declare token-valid-now?)
@@ -354,6 +362,8 @@
(or (some-> (premium-features.settings/premium-embedding-token) valid-token->features)
#{})
(catch Throwable e
+ (when (:pass-thru (ex-data e))
+ (throw e))
(cached-logger (premium-features.settings/premium-embedding-token) e)
#{}))))