The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Introduce minimal dependency analytics interface (#72262)
metambenc0927e9c157a
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 30ada1b6b54..13b34510d3c 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -181,9 +181,9 @@:api #{metabase.analytics.apimetabase.analytics.coremetabase.analytics.init- metabase.analytics.prometheusmetabase.analytics.snowplow}- :uses #{api+ :uses #{analytics-interface+ apiapp-dbappearanceconfig@@ -223,6 +223,11 @@:model/TransformRun:model/User}}+ analytics-interface+ {:team "Querying Platform"+ :api #{metabase.analytics-interface.core}+ :uses #{}}+analyze{:team "Graphy":api #{metabase.analyze.core}@@ -459,6 +464,7 @@metabase.channel.template.coremetabase.channel.urls}:uses #{analytics+ analytics-interfaceapiapp-dbappearance@@ -815,7 +821,7 @@metabase.driver.sql.utilmetabase.driver.syncmetabase.driver.util}- :uses #{analytics+ :uses #{analytics-interfaceapp-dbappearanceauth-provider@@ -957,7 +963,7 @@frontend-errors{:team "UX West":api #{metabase.frontend-errors.api}- :uses #{analytics+ :uses #{analytics-interfaceapirequestutil}}@@ -1203,6 +1209,7 @@:uses #{activity-feedagent-libanalytics+ analytics-interfaceapiapi-scopeapp-db@@ -1350,6 +1357,7 @@metabase.notification.modelsmetabase.notification.payload.core}:uses #{analytics+ analytics-interfaceapiappearanceauth-identity@@ -1496,7 +1504,7 @@:api #{metabase.premium-features.apimetabase.premium-features.coremetabase.premium-features.init}- :uses #{analytics+ :uses #{analytics-interfaceapiapp-dbclassloader@@ -1607,6 +1615,7 @@metabase.queries.models.query ; TODO FIXMEmetabase.queries.schema}:uses #{analytics+ analytics-interfaceanalyzeapiapp-db@@ -1731,6 +1740,7 @@metabase.query-processor.utilmetabase.query-processor.util.persisted-cache}:uses #{analytics+ analytics-interfaceanalyzeapiapp-db@@ -1783,7 +1793,7 @@:api #{metabase.request.coremetabase.request.initmetabase.request.schema}- :uses #{analytics+ :uses #{analytics-interfaceapiconfigembedding@@ -1843,6 +1853,7 @@metabase.search.specmetabase.search.task.search-index}:uses #{analytics+ analytics-interfaceapiapp-dbappearance@@ -1901,6 +1912,7 @@metabase.server.settingsmetabase.server.streaming-response} ; TODO -- too many API namespaces:uses #{analytics+ analytics-interfaceapiapi-keysapp-db@@ -2001,6 +2013,7 @@metabase.slackbot.init}:friends #{metabot}:uses #{analytics+ analytics-interfaceapichannelconfig@@ -2035,7 +2048,7 @@sql-parsing{:team "DevEx":api #{metabase.sql-parsing.core}- :uses #{analytics+ :uses #{analytics-interfaceutil}}sql-tools@@ -2043,6 +2056,7 @@:api #{metabase.sql-tools.coremetabase.sql-tools.init}:uses #{analytics+ analytics-interfacedriverlibsettings@@ -2090,7 +2104,7 @@metabase.sync.schedulesmetabase.sync.task.sync-databasesmetabase.sync.util}- :uses #{analytics+ :uses #{analytics-interfaceanalyzeapiapp-db@@ -2215,7 +2229,7 @@metabase.transforms.initmetabase.transforms.schemametabase.transforms.util}- :uses #{analytics+ :uses #{analytics-interfaceapiapp-dbchannel@@ -2312,6 +2326,7 @@metabase.upload.dbmetabase.upload.init}:uses #{analytics+ analytics-interfaceapiappearancedriver@@ -2503,7 +2518,7 @@:api #{metabase.warehouses.coremetabase.warehouses.initmetabase.warehouses.models.database}- :uses #{analytics+ :uses #{analytics-interfaceapiapp-dbaudit-app@@ -2820,6 +2835,7 @@:api #{metabase-enterprise.data-complexity-score.apimetabase-enterprise.data-complexity-score.init}:uses #{analytics+ analytics-interfaceapiapp-dbaudit-app@@ -2964,6 +2980,7 @@:api #{metabase-enterprise.gsheets.apimetabase-enterprise.gsheets.init}:uses #{analytics+ analytics-interfaceapipremium-featuressettings@@ -3054,7 +3071,7 @@:api #{metabase-enterprise.remote-sync.apimetabase-enterprise.remote-sync.coremetabase-enterprise.remote-sync.init}- :uses #{analytics+ :uses #{analytics-interfaceapiapp-dbcollections@@ -3147,7 +3164,7 @@:api #{metabase-enterprise.scim.coremetabase-enterprise.scim.initmetabase-enterprise.scim.routes}- :uses #{analytics+ :uses #{analytics-interfaceapiapi-keysmodels@@ -3198,6 +3215,7 @@metabase-enterprise.semantic-search.init}:uses #{activity-feedanalytics+ analytics-interfaceapiapp-dbconfig@@ -3344,7 +3362,7 @@:api #{metabase-enterprise.transforms-python.apimetabase-enterprise.transforms-python.coremetabase-enterprise.transforms-python.init}- :uses #{analytics+ :uses #{analytics-interfaceapiapp-dbconfigdiff --git a/e2e/test/scenarios/admin/internal-analytics.cy.spec.ts b/e2e/test/scenarios/admin/internal-analytics.cy.spec.tsnew file mode 100644index 00000000000..4faac99d323--- /dev/null+++ b/e2e/test/scenarios/admin/internal-analytics.cy.spec.ts@@ -0,0 +1,30 @@+import { restore } from "e2e/support/helpers";++describe("internal analytics", () => {+ beforeEach(() => {+ restore();+ cy.signInAsAdmin();+ });++ it("should POST batched internal analytics events to the backend", () => {+ cy.intercept("POST", "/api/analytics/internal").as("internalAnalytics");++ cy.visit("/");++ cy.window().then((win: any) => {+ // Send more than flush-buffer-size (50) events to trigger an immediate flush.+ // See metabase.analytics.impl/flush-buffer-size.+ for (let i = 0; i < 51; i++) {+ win.__internalAnalytics.inc("e2e-test/counter", { test: "true" }, 1);+ }+ });++ cy.wait("@internalAnalytics").then((interception) => {+ expect(interception.response?.statusCode).to.equal(204);+ expect(interception.request.body.events).to.be.an("array");+ expect(interception.request.body.events.length).to.be.greaterThan(0);+ expect(interception.request.body.events[0]).to.have.property("op");+ expect(interception.request.body.events[0]).to.have.property("metric");+ });+ });+});diff --git a/enterprise/backend/src/metabase_enterprise/data_complexity_score/complexity.clj b/enterprise/backend/src/metabase_enterprise/data_complexity_score/complexity.cljindex ad2cf22d317..fa22e2d7f42 100644--- a/enterprise/backend/src/metabase_enterprise/data_complexity_score/complexity.clj+++ b/enterprise/backend/src/metabase_enterprise/data_complexity_score/complexity.clj@@ -8,8 +8,8 @@[clojure.string :as str][metabase-enterprise.data-complexity-score.complexity-embedders :as embedders][metabase-enterprise.semantic-search.core :as semantic-search]+ [metabase.analytics-interface.core :as analytics.interface][metabase.analytics.core :as analytics]- [metabase.analytics.prometheus :as prometheus][metabase.audit-app.core :as audit][metabase.collections.core :as collections][metabase.util :as u]@@ -409,9 +409,9 @@(try(f)(finally- (prometheus/observe! :metabase-data-complexity/phase-duration-ms- {:stage stage :catalog catalog}- (u/since-ms timer))))))+ (analytics.interface/observe! :metabase-data-complexity/phase-duration-ms+ {:stage stage :catalog catalog}+ (u/since-ms timer))))))(defn complexity-scores"Compute the complexity score for the `:library`, `:universe`, and `:metabot` catalogs of this@@ -475,8 +475,8 @@;; partial failure doesn't silently mark the fingerprint as published.(with-meta result {::snowplow-published? published?})))(finally- (prometheus/observe! :metabase-data-complexity/scoring-duration-ms- (u/since-ms total-timer))))))+ (analytics.interface/observe! :metabase-data-complexity/scoring-duration-ms+ (u/since-ms total-timer))))))(comment(complexity-scores))diff --git a/enterprise/backend/src/metabase_enterprise/gsheets/api.clj b/enterprise/backend/src/metabase_enterprise/gsheets/api.cljindex e72e42a8cf1..351d84dfaad 100644--- a/enterprise/backend/src/metabase_enterprise/gsheets/api.clj+++ b/enterprise/backend/src/metabase_enterprise/gsheets/api.clj@@ -7,7 +7,7 @@:as gsheets.settings:refer [gsheets gsheets!]][metabase-enterprise.harbormaster.client :as hm.client]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.analytics.snowplow :as snowplow][metabase.api.common :as api][metabase.api.macros :as api.macros]diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.cljindex af8600beaf3..8f1db83a234 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj@@ -11,7 +11,7 @@[metabase-enterprise.remote-sync.source.protocol :as source.p][metabase-enterprise.remote-sync.spec :as spec][metabase-enterprise.serialization.core :as serialization]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.api.common :as api][metabase.app-db.cluster-lock :as cluster-lock][metabase.collections.models.collection :as collection]diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.cljindex db71464ee93..8c48fc451c7 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj@@ -5,7 +5,7 @@[clojure.java.io :as io][clojure.string :as str][metabase-enterprise.remote-sync.source.protocol :as source.p]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.util :as u][metabase.util.log :as log])(:importdiff --git a/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj b/enterprise/backend/src/metabase_enterprise/scim/v2/api.cljindex 11bfbaeb8c8..f4b22bead12 100644--- a/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj+++ b/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj@@ -5,7 +5,7 @@`v2` in the API path represents the fact that we implement SCIM 2.0."(:require[metabase-enterprise.scim.settings :as scim.settings]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.api.macros :as api.macros][metabase.models.interface :as mi][metabase.permissions.core :as perms]diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/core.cljindex 7add9418d76..1b8f7736063 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/core.clj@@ -10,7 +10,7 @@[metabase-enterprise.semantic-search.pgvector-api :as semantic.pgvector-api][metabase-enterprise.semantic-search.repair :as semantic.repair][metabase-enterprise.semantic-search.settings :as semantic.settings]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.premium-features.core :refer [defenterprise]][metabase.search.engine :as search.engine][metabase.tracing.core :as tracing]diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/embedding.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/embedding.cljindex 9ce99b41fc8..aee4a49d10c 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/embedding.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/embedding.clj@@ -4,7 +4,8 @@[clojure.string :as str][metabase-enterprise.semantic-search.models.token-tracking :as semantic.models.token-tracking][metabase-enterprise.semantic-search.settings :as semantic-settings]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.tracing.core :as tracing][metabase.util :as u][metabase.util.json :as json]@@ -219,10 +220,10 @@{:provider provider :model model-name}total-tokens)(when snowplow?- (analytics/track-token-usage!+ (analytics.core/track-token-usage!{:snowplow true:prometheus false ; already tracked via inc! above- :request-id (analytics/uuid->ai-service-hex-uuid (random-uuid))+ :request-id (analytics.core/uuid->ai-service-hex-uuid (random-uuid)):model-id model-name:total-tokens total-tokens:prompt-tokens prompt-tokensdiff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/gate.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/gate.cljindex 37331a4b23a..3dcc39cd47e 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/gate.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/gate.clj@@ -22,7 +22,7 @@[buddy.core.hash :as buddy-hash][honey.sql :as sql][metabase-enterprise.semantic-search.settings :as semantic.settings]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.util :as u][metabase.util.json :as json][metabase.util.log :as log]diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/index.cljindex ab518847c40..7810e1892a5 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj@@ -12,7 +12,7 @@[metabase-enterprise.semantic-search.embedding :as embedding][metabase-enterprise.semantic-search.scoring :as scoring][metabase-enterprise.semantic-search.settings :as semantic-settings]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.models.interface :as mi][metabase.search.config :as search.config][metabase.search.core :as search]@@ -205,7 +205,7 @@[connectable table-name](try(->> (index-size connectable table-name)- (analytics/set! :metabase-search/semantic-index-size))+ (analytics/set-gauge! :metabase-search/semantic-index-size))(catch Exception e(log/warn e "Failed to set :metabase-search/semantic-index-size metric"))))diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/indexer.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/indexer.cljindex 8d5cdba564a..c8e367a2932 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/indexer.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/indexer.clj@@ -26,7 +26,7 @@[metabase-enterprise.semantic-search.index :as semantic.index][metabase-enterprise.semantic-search.index-metadata :as semantic.index-metadata][metabase-enterprise.semantic-search.settings :as semantic.settings]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.util :as u][metabase.util.log :as log][next.jdbc :as jdbc]@@ -125,7 +125,7 @@(clear-stall-if-needed [](when (:stalled-at @indexing-state)- (analytics/set! :metabase-search/semantic-indexer-stalled 0)+ (analytics/set-gauge! :metabase-search/semantic-indexer-stalled 0)(clear-stall! pgvector index-metadata index)(vswap! indexing-state assoc :stalled-at nil)))](let [poll-result (poll)@@ -208,7 +208,7 @@;; according to appropriate policies.:else(do- (analytics/set! :metabase-search/semantic-indexer-stalled 1)+ (analytics/set-gauge! :metabase-search/semantic-indexer-stalled 1)(log/debugf "Processing %d documents in stalled mode (using DLQ) for index %s" (count gate-docs) (:table-name index))(let [{:keys [failures]} (semantic.dlq/try-batch! pgvector index gate-docs)](when (seq failures)diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/task/metric_collector.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/task/metric_collector.cljindex f15d1ab54ac..da7e1006e45 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/task/metric_collector.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/task/metric_collector.clj@@ -7,7 +7,7 @@[metabase-enterprise.semantic-search.dlq :as semantic.dlq][metabase-enterprise.semantic-search.env :as semantic.env][metabase-enterprise.semantic-search.util :as semantic.u]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.task.core :as task][metabase.util.log :as log][next.jdbc :as jdbc]@@ -38,7 +38,7 @@(if (semantic.u/table-exists? pgvector gate-table-name)(let [table-size (row-count pgvector gate-table-name)](log/debugf "Setting `semantic-gate-size` metric to %d" table-size)- (analytics/set! :metabase-search/semantic-gate-size table-size)+ (analytics/set-gauge! :metabase-search/semantic-gate-size table-size)nil)(log/warn "Gate table does not exist. Index may not have been initialized."))))@@ -60,7 +60,7 @@(when (semantic.u/table-exists? pgvector dlq-table-name)(let [table-size (row-count pgvector dlq-table-name)](log/debugf "Setting `semantic-dlq-size` metric to %d" table-size)- (analytics/set! :metabase-search/semantic-dlq-size table-size)+ (analytics/set-gauge! :metabase-search/semantic-dlq-size table-size)nil)))(log/warn "DLQ table does not exist. Index may not have been initialized.")))diff --git a/enterprise/backend/src/metabase_enterprise/transforms_python/python_runner.clj b/enterprise/backend/src/metabase_enterprise/transforms_python/python_runner.cljindex 60da7f744e8..23c3b2aedd0 100644--- a/enterprise/backend/src/metabase_enterprise/transforms_python/python_runner.clj+++ b/enterprise/backend/src/metabase_enterprise/transforms_python/python_runner.clj@@ -7,7 +7,7 @@[medley.core :as m][metabase-enterprise.transforms-python.s3 :as s3][metabase-enterprise.transforms-python.settings :as transforms-python.settings]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.config.core :as config][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib]@@ -184,8 +184,8 @@duration-ms :- int?status :- [:enum :success :error :timeout]](log/infof "Python API call %s: run-id=%d duration=%dms" (name status) job-run-id duration-ms)- (prometheus/inc! :metabase-transforms/python-api-calls-total {:status (name status)})- (prometheus/observe! :metabase-transforms/python-api-call-duration-ms {} duration-ms))+ (analytics/inc! :metabase-transforms/python-api-calls-total {:status (name status)})+ (analytics/observe! :metabase-transforms/python-api-call-duration-ms {} duration-ms))(defmacro with-python-api-timing"Execute body while timing a Python API call."diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.cljindex b23403a9c7d..69c36924762 100644--- a/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/core_test.clj@@ -5,7 +5,7 @@[metabase-enterprise.semantic-search.env :as semantic.env][metabase-enterprise.semantic-search.pgvector-api :as semantic.pgvector-api][metabase-enterprise.semantic-search.test-util :as semantic.tu]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.app-db.core :as mdb][metabase.search.appdb.core :as appdb][metabase.search.engine :as search.engine]diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/embedding_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/embedding_test.cljindex e7a81305d3d..1e7d3956b6d 100644--- a/enterprise/backend/test/metabase_enterprise/semantic_search/embedding_test.clj+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/embedding_test.clj@@ -12,7 +12,7 @@[metabase-enterprise.semantic-search.pgvector-api :as semantic.pgvector-api][metabase-enterprise.semantic-search.settings :as semantic.settings][metabase-enterprise.semantic-search.test-util :as semantic.tu]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.analytics.snowplow-test :as snowplow-test][metabase.test :as mt][metabase.util.json :as json]diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.cljindex 46feaf74b34..91aaeaf871a 100644--- a/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj@@ -6,7 +6,7 @@[metabase-enterprise.semantic-search.env :as semantic.env][metabase-enterprise.semantic-search.index :as semantic.index][metabase-enterprise.semantic-search.test-util :as semantic.tu]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.api.common :as api][metabase.collections.models.collection :as collection][metabase.models.interface :as mi]diff --git a/frontend/src/metabase/utils/internal-analytics.ts b/frontend/src/metabase/utils/internal-analytics.tsnew file mode 100644index 00000000000..fb5ad895276--- /dev/null+++ b/frontend/src/metabase/utils/internal-analytics.ts@@ -0,0 +1,16 @@+import { POST } from "metabase/utils/api";++type InternalAnalyticsEvent = {+ op: "inc" | "dec" | "set" | "observe" | "clear";+ metric: string;+ labels?: Record<string, string> | null;+ amount?: number;+};++const postEvents = POST("/api/analytics/internal");++export function postInternalAnalytics(+ events: InternalAnalyticsEvent[],+): Promise<void> {+ return postEvents({ events });+}diff --git a/mage/src/mage/modules.clj b/mage/src/mage/modules.cljindex 403c46d68c1..ba833f95794 100644--- a/mage/src/mage/modules.clj+++ b/mage/src/mage/modules.clj@@ -89,6 +89,7 @@(def driver-affecting-overrides"These modules affect drivers when computing, but we want to override and not consider them to affect drivers."'#{analytics+ analytics-interfaceapiapi-scopeapi-keysdiff --git a/src/metabase/analytics/api.clj b/src/metabase/analytics/api.cljindex 8ddc6b267d5..844b03535ab 100644--- a/src/metabase/analytics/api.clj+++ b/src/metabase/analytics/api.clj@@ -1,8 +1,10 @@(ns metabase.analytics.api(:require+ [metabase.analytics.prometheus :as prometheus][metabase.analytics.stats :as stats][metabase.api.macros :as api.macros]- [metabase.permissions.core :as perms]))+ [metabase.permissions.core :as perms]+ [metabase.util.log :as log]));; I don't think this endpoint is actually used anywhere for anything.;;@@ -16,3 +18,26 @@[](perms/check-has-application-permission :monitoring)(stats/legacy-anonymous-usage-stats))++(def ^:private InternalAnalyticsEvent+ [:map+ [:op [:enum :inc :dec :set :observe :clear]]+ [:metric :keyword]+ [:labels {:optional true} [:maybe [:map-of :keyword :string]]]+ [:amount {:optional true} [:maybe number?]]])++(api.macros/defendpoint :post "/internal" :- :nil+ "Receive a batch of internal analytics events from the frontend and record them as Prometheus metrics."+ [_route-params+ _query-params+ {:keys [events]} :- [:map [:events [:sequential InternalAnalyticsEvent]]]]+ (doseq [{:keys [op metric labels amount]} events]+ (try+ (case op+ :inc (prometheus/inc! metric labels (or amount 1))+ :dec (prometheus/dec! metric labels (or amount 1))+ :set (prometheus/set! metric labels amount)+ :observe (prometheus/observe! metric labels (or amount 1))+ :clear (prometheus/clear! metric))+ (catch Exception e+ (log/warnf e "Failed to record internal analytics event %s %s" op metric)))))diff --git a/src/metabase/analytics/core.clj b/src/metabase/analytics/core.cljindex 314cc0a9ad1..08500c8aae9 100644--- a/src/metabase/analytics/core.clj+++ b/src/metabase/analytics/core.clj@@ -36,11 +36,7 @@known-labelsinitial-value- clear!connection-pool-info- inc!- observe!- set!setup!shutdown!]diff --git a/src/metabase/analytics/impl.clj b/src/metabase/analytics/impl.cljnew file mode 100644index 00000000000..eee9b0733a5--- /dev/null+++ b/src/metabase/analytics/impl.clj@@ -0,0 +1,19 @@+(ns metabase.analytics.impl+ "CLJ implementation of [[metabase.analytics-interface.core/Reporter]].+ Delegates to Prometheus."+ (:require+ [metabase.analytics-interface.core :as analytics.interface]+ [metabase.analytics.prometheus :as prometheus]))++(analytics.interface/set-reporter!+ (reify analytics.interface/Reporter+ (-inc! [_ metric labels amount]+ (prometheus/inc! metric labels amount))+ (-dec-gauge! [_ metric labels amount]+ (prometheus/dec! metric labels amount))+ (-set-gauge! [_ metric labels amount]+ (prometheus/set! metric labels amount))+ (-observe! [_ metric labels amount]+ (prometheus/observe! metric labels amount))+ (-clear! [_ metric]+ (prometheus/clear! metric))))diff --git a/src/metabase/analytics/impl.cljs b/src/metabase/analytics/impl.cljsnew file mode 100644index 00000000000..2f21a069764--- /dev/null+++ b/src/metabase/analytics/impl.cljs@@ -0,0 +1,83 @@+(ns metabase.analytics.impl+ "CLJS implementation of [[metabase.analytics-interface.core/Reporter]].+ Batches events and POSTs them to the backend, which records them as Prometheus metrics.++ Under heavy load we prefer to drop the oldest buffered events rather than flood the backend+ with extra flushes — newer events are usually more interesting, and gauge `:set` operations+ are strictly more correct when the newest value wins. The count of dropped events rides+ along on the next flush as an [[dropped-metric]] :inc event so dashboards can tell 'quiet+ system' apart from 'we dropped half your events'."+ (:require+ [metabase.analytics-interface.core :as analytics.interface]))++(def ^:private flush-interval-ms 5000)++(def ^:private buffer-capacity 1000)++(def ^:private dropped-metric :metabase-frontend/analytics-events-dropped)++(defonce ^:private state (atom {:events [] :dropped 0}))++(defn- enqueue+ "Append `event` to the buffer, dropping the oldest events if the result would exceed `capacity`."+ [{:keys [events dropped]} event capacity]+ (let [events' (conj events event)+ overflow (max 0 (- (count events') capacity))]+ {:events (if (pos? overflow) (vec (drop overflow events')) events')+ :dropped (+ dropped overflow)}))++(defn- take-pending+ "Return `[events-to-send new-state]`. When any drops have occurred, prepend a single :inc event+ for [[dropped-metric]] carrying the dropped count so the backend can record it."+ [{:keys [events dropped]}]+ (let [out (if (pos? dropped)+ (into [{:op :inc :metric dropped-metric :labels nil :amount dropped}] events)+ events)]+ [out {:events [] :dropped 0}]))++(defn- post-events!+ "Side-effecting POST of a batch of events to the backend."+ [events]+ (let [post-fn (.-postInternalAnalytics (js/require "metabase/utils/internal-analytics"))]+ (post-fn (clj->js events))))++(defn- flush-buffer!+ "Flush the buffer to the backend. Clears the buffer and resets the dropped count atomically."+ []+ (let [[to-send new-state] (take-pending @state)]+ (reset! state new-state)+ (when (seq to-send)+ (post-events! to-send))))++(defonce ^:private _interval+ (js/setInterval flush-buffer! flush-interval-ms))++(defn- buffer-event!+ [event]+ (swap! state enqueue event buffer-capacity))++(analytics.interface/set-reporter!+ (reify analytics.interface/Reporter+ (-inc! [_ metric labels amount]+ (buffer-event! {:op :inc+ :metric metric+ :labels labels+ :amount amount}))+ (-dec-gauge! [_ metric labels amount]+ (buffer-event! {:op :dec+ :metric metric+ :labels labels+ :amount amount}))+ (-set-gauge! [_ metric labels amount]+ (buffer-event! {:op :set+ :metric metric+ :labels labels+ :amount amount}))+ (-observe! [_ metric labels amount]+ (buffer-event! {:op :observe+ :metric metric+ :labels labels+ :amount amount}))+ (-clear! [_ metric]+ (buffer-event! {:op :clear+ :metric metric}))))diff --git a/src/metabase/analytics/init.clj b/src/metabase/analytics/init.cljindex 4b9aa733bd4..8911ee49909 100644--- a/src/metabase/analytics/init.clj+++ b/src/metabase/analytics/init.clj@@ -1,4 +1,5 @@(ns metabase.analytics.init(:require+ [metabase.analytics.impl] ; this registers the reporter[metabase.analytics.settings][metabase.analytics.task.send-anonymous-stats]))diff --git a/src/metabase/analytics/llm_token_usage.clj b/src/metabase/analytics/llm_token_usage.cljindex 0d9fc461ed4..eeab38e6705 100644--- a/src/metabase/analytics/llm_token_usage.clj+++ b/src/metabase/analytics/llm_token_usage.clj@@ -1,7 +1,7 @@(ns metabase.analytics.llm-token-usage"LLM token usage tracking for Snowplow and Prometheus."(:require- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.analytics.snowplow :as snowplow][metabase.analytics.util :as analytics.util][metabase.util.malli :as mu]@@ -60,9 +60,9 @@[{:keys [model-id tag prompt-tokens completion-tokens]}:- PrometheusArgs](let [labels {:model model-id :source tag}]- (prometheus/inc! :metabase-metabot/llm-input-tokens labels prompt-tokens)- (prometheus/inc! :metabase-metabot/llm-output-tokens labels completion-tokens)- (prometheus/observe! :metabase-metabot/llm-tokens-per-call labels (+ prompt-tokens completion-tokens))))+ (analytics/inc! :metabase-metabot/llm-input-tokens labels prompt-tokens)+ (analytics/inc! :metabase-metabot/llm-output-tokens labels completion-tokens)+ (analytics/observe! :metabase-metabot/llm-tokens-per-call labels (+ prompt-tokens completion-tokens))))(mu/defn track-token-usage!"Convenience wrapper that fires Snowplow and/or Prometheus token tracking.diff --git a/src/metabase/analytics/prometheus.clj b/src/metabase/analytics/prometheus.cljindex dcd4dcef4b3..5e4e3b1ca5c 100644--- a/src/metabase/analytics/prometheus.clj+++ b/src/metabase/analytics/prometheus.clj@@ -619,6 +619,8 @@(prometheus/counter :metabase-frontend/errors{:description "Number of frontend errors reported by the browser.":labels [:type]})+ (prometheus/counter :metabase-frontend/analytics-events-dropped+ {:description "Number of frontend analytics events dropped before being POSTed to the backend, because the client-side buffer was full."})(prometheus/counter :metabase-export/errors{:description "Number of errors during data export.":labels [:format]})])@@ -758,19 +760,6 @@(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/quartz.clj b/src/metabase/analytics/quartz.cljindex 95b25c59699..1ff8bc20ab5 100644--- a/src/metabase/analytics/quartz.clj+++ b/src/metabase/analytics/quartz.clj@@ -2,7 +2,7 @@(:require[clojurewerkz.quartzite.matchers :as qm][clojurewerkz.quartzite.scheduler :as qs]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.task.core :as task][metabase.util.log :as log])(:import@@ -31,7 +31,7 @@(try(let [tags {:status (if job-exception "failed" "succeeded"):job-name (.. ctx getJobDetail getKey toString)}]- (prometheus/inc! :metabase-tasks/quartz-tasks-executed tags))+ (analytics/inc! :metabase-tasks/quartz-tasks-executed tags))(catch Throwable e(log/error e "Failed to record Prometheus metric for Quartz job completion"))))))@@ -80,9 +80,9 @@;; This ensures we capture the state even if it changed outside of a direct completion event.(try(doseq [[state count] (get-quartz-task-states scheduler)]- (prometheus/set! :metabase-tasks/quartz-tasks-states- {:state state}- count))+ (analytics/set-gauge! :metabase-tasks/quartz-tasks-states+ {:state state}+ count))(catch Throwable e(log/error e "Failed to record Prometheus metrics for Quartz trigger completion"))))))diff --git a/src/metabase/analytics/sdk.clj b/src/metabase/analytics/sdk.cljindex 65451e7f8f5..fb523d2680c 100644--- a/src/metabase/analytics/sdk.clj+++ b/src/metabase/analytics/sdk.clj@@ -9,7 +9,7 @@then we can use the information on the tables to track information about the embedding client,and TODO: send it out in `summarize-execution`."(:require- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.util.log :as log][metabase.util.malli :as mu]))@@ -46,8 +46,8 @@"Tabulates the number of responses by status code made by clients of the SDK."[sdk-client {:keys [status]}](case sdk-client- "embedding-sdk-react" (prometheus/inc! :metabase-sdk/response {:status (str status)})- "embedding-iframe" (prometheus/inc! :metabase-embedding-iframe/response {:status (str status)})+ "embedding-sdk-react" (analytics/inc! :metabase-sdk/response {:status (str status)})+ "embedding-iframe" (analytics/inc! :metabase-embedding-iframe/response {:status (str status)})(log/infof "Unknown client. client: %s" sdk-client)))(defn embedding-context?diff --git a/src/metabase/analytics_interface/core.cljc b/src/metabase/analytics_interface/core.cljcnew file mode 100644index 00000000000..8a207ebd2d3--- /dev/null+++ b/src/metabase/analytics_interface/core.cljc@@ -0,0 +1,98 @@+(ns metabase.analytics-interface.core+ "Dependency-free analytics interface for use in CLJC code (including metabase.lib).++ Provides a thin reporting API that works in both CLJ and CLJS runtimes.+ This namespace has zero dependencies, so it can be safely required from `metabase.lib`+ without circular dependencies.++ ## How it works++ [[Reporter]] is a protocol defining the core metrics operations.+ Each runtime provides an implementation and registers it via [[set-reporter!]]:+ - **CLJ (backend):** `metabase.analytics.impl` registers an implementation+ that delegates to Prometheus.+ - **CLJS (frontend):** `metabase.analytics.impl` registers an implementation+ that batches events and POSTs them to the backend.++ ## Usage from library code++ (require '[metabase.analytics-interface.core :as analytics])++ (analytics/inc! :my-experiment/runs-total {:variant \"new\"})+ (analytics/observe! :my-experiment/overhead-ms {:variant \"new\"} 42)+ (analytics/set-gauge! :my-gauge/current-value 10)+ (analytics/dec-gauge! :my-gauge/active-count)")++(defprotocol Reporter+ (-inc! [this metric labels amount]+ "Increment a counter metric.")+ (-dec-gauge! [this metric labels amount]+ "Decrement a gauge metric.")+ (-set-gauge! [this metric labels amount]+ "Set a gauge metric to an absolute value.")+ (-observe! [this metric labels amount]+ "Record a histogram/summary observation.")+ (-clear! [this metric]+ "Clear all values for a metric (reset all label combinations)."))++(defonce ^:private reporter (atom nil))++(defn get-reporter+ "Return the currently registered [[Reporter]], or nil."+ []+ @reporter)++(defn set-reporter!+ "Register a [[Reporter]] implementation. Called once per runtime at init time."+ [r]+ (reset! reporter r))++(defn inc!+ "Increment a counter metric. No-op if no reporter is registered."+ ([metric]+ (inc! metric nil 1))+ ([metric labels-or-amount]+ (if (number? labels-or-amount)+ (inc! metric nil labels-or-amount)+ (inc! metric labels-or-amount 1)))+ ([metric labels amount]+ (when-let [r @reporter]+ (-inc! r metric labels amount))))++(defn dec-gauge!+ "Decrement a gauge metric. No-op if no reporter is registered."+ ([metric]+ (dec-gauge! metric nil 1))+ ([metric labels-or-amount]+ (if (number? labels-or-amount)+ (dec-gauge! metric nil labels-or-amount)+ (dec-gauge! metric labels-or-amount 1)))+ ([metric labels amount]+ (when-let [r @reporter]+ (-dec-gauge! r metric labels amount))))++(defn set-gauge!+ "Set a gauge metric to an absolute value. No-op if no reporter is registered."+ ([metric amount]+ (set-gauge! metric nil amount))+ ([metric labels amount]+ (when-let [r @reporter]+ (-set-gauge! r metric labels amount))))++(defn observe!+ "Record a histogram/summary observation. No-op if no reporter is registered."+ ([metric]+ (observe! metric nil 1))+ ([metric labels-or-amount]+ (if (number? labels-or-amount)+ (observe! metric nil labels-or-amount)+ (observe! metric labels-or-amount 1)))+ ([metric labels amount]+ (when-let [r @reporter]+ (-observe! r metric labels amount))))++(defn clear!+ "Clear all values for a metric. No-op if no reporter is registered."+ [metric]+ (when-let [r @reporter]+ (-clear! r metric)))diff --git a/src/metabase/channel/email.clj b/src/metabase/channel/email.cljindex 6889bf7e09b..a167bbaca6e 100644--- a/src/metabase/channel/email.clj+++ b/src/metabase/channel/email.clj@@ -3,7 +3,7 @@[clojure.data :as data][clojure.set :as set][clojure.string :as str]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.channel.settings :as channel.settings][metabase.permissions.core :as perms][metabase.premium-features.core :as premium-features]diff --git a/src/metabase/channel/impl/email.clj b/src/metabase/channel/impl/email.cljindex b4fc832d5fb..b6fc884cc17 100644--- a/src/metabase/channel/impl/email.clj+++ b/src/metabase/channel/impl/email.clj@@ -3,7 +3,8 @@[clojure.string :as str][hiccup.core :refer [html]][medley.core :as m]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.channel.core :as channel][metabase.channel.email :as email][metabase.channel.email.logo :as email.logo]@@ -31,7 +32,7 @@(set! *warn-on-reflection* true)-(defmethod prometheus/known-labels :metabase-notification/template-render [_]+(defmethod analytics.core/known-labels :metabase-notification/template-render [_](for [template-type [:email/handlebars-text :email/handlebars-resource]]{:template-type template-type:channel-type :channel/email}))@@ -103,9 +104,9 @@(defn- render-body[{:keys [details] :as _template} payload](let [template-type (keyword (:type details))]- (prometheus/inc! :metabase-notification/template-render- {:template-type template-type- :channel-type :channel/email})+ (analytics/inc! :metabase-notification/template-render+ {:template-type template-type+ :channel-type :channel/email})(case template-type:email/handlebars-resource(handlebars/render (:path details) payload)diff --git a/src/metabase/channel/models/channel.clj b/src/metabase/channel/models/channel.cljindex a505e954c8e..3332adfb732 100644--- a/src/metabase/channel/models/channel.clj+++ b/src/metabase/channel/models/channel.clj@@ -1,7 +1,8 @@(ns ^{:added "0.51.0"} metabase.channel.models.channel(:require[malli.core :as mc]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.api.common :as api][metabase.channel.template.handlebars :as handlebars][metabase.models.interface :as mi]@@ -166,10 +167,10 @@(name action) channel_type template-type api/*current-user-id* (pr-str (:body details)))(log/infof "ChannelTemplate %s: channel_type=%s template_type=%s user_id=%s"(name action) channel_type template-type api/*current-user-id*))- (prometheus/inc! (case action- :create :metabase-notification/template-create- :update :metabase-notification/template-update)- {:channel-type channel_type})))+ (analytics/inc! (case action+ :create :metabase-notification/template-create+ :update :metabase-notification/template-update)+ {:channel-type channel_type})))(t2/define-before-insert :model/ChannelTemplate[instance]@@ -194,8 +195,8 @@;; Currently only email channel has templates, but this is extensible(def ^:private template-channel-labels [{:channel-type :channel/email}])-(defmethod prometheus/known-labels :metabase-notification/template-create [_] template-channel-labels)-(defmethod prometheus/known-labels :metabase-notification/template-update [_] template-channel-labels)+(defmethod analytics.core/known-labels :metabase-notification/template-create [_] template-channel-labels)+(defmethod analytics.core/known-labels :metabase-notification/template-update [_] template-channel-labels)(defmethod mi/can-write? :model/ChannelTemplate[& _]diff --git a/src/metabase/core/core.clj b/src/metabase/core/core.cljindex a61c31e5407..f00409e464c 100644--- a/src/metabase/core/core.clj+++ b/src/metabase/core/core.clj@@ -3,8 +3,8 @@[clojure.string :as str][clojure.tools.trace :as trace][environ.core :as env]- [metabase.analytics.core :as analytics]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.api-routes.core :as api-routes][metabase.app-db.core :as mdb][metabase.classloader.core :as classloader]@@ -67,14 +67,14 @@;;; --------------------------------------------------- Info Metric----------------------------------------------------(defmethod analytics/known-labels :metabase-info/build+(defmethod analytics.core/known-labels :metabase-info/build[_];; We need to update the labels configured for this metric before we expose anything new added to `mb-version-info`[(merge (select-keys config/mb-version-info [:tag :hash :date]){:version config/mb-version-string:major-version (config/current-major-version)})])-(defmethod analytics/initial-value :metabase-info/build [_ _] 1)+(defmethod analytics.core/initial-value :metabase-info/build [_ _] 1);;; --------------------------------------------------- Lifecycle ----------------------------------------------------@@ -108,7 +108,7 @@(task/stop-scheduler!)(server/stop-web-server!)(tracing/shutdown!)- (analytics/shutdown!)+ (analytics.core/shutdown!)(notification/shutdown!);; This timeout was chosen based on a 30s default termination grace period in Kubernetes.(let [timeout-seconds 20]@@ -192,10 +192,10 @@(init-status/set-progress! 0.4);; Set up Prometheus(log/info "Setting up prometheus metrics")- (analytics/setup!)+ (analytics.core/setup!)(init-status/set-progress! 0.5)(task/init-scheduler!)- (analytics/add-listeners-to-scheduler!)+ (analytics.core/add-listeners-to-scheduler!);; run a very quick check to see if we are doing a first time installation;; the test we are using is if there is at least 1 User in the database(let [new-install? (not (setup/has-user-setup))]@@ -247,8 +247,8 @@(u/format-milliseconds init-duration-ms)(u/format-milliseconds jvm-to-complete-ms))(system/startup-time-millis! init-duration-ms)- (prometheus/set! :metabase-startup/init-duration-millis init-duration-ms)- (prometheus/set! :metabase-startup/jvm-to-complete-millis jvm-to-complete-ms))))+ (analytics/set-gauge! :metabase-startup/init-duration-millis init-duration-ms)+ (analytics/set-gauge! :metabase-startup/jvm-to-complete-millis jvm-to-complete-ms))));;; -------------------------------------------------- Normal Start --------------------------------------------------diff --git a/src/metabase/driver/connection.clj b/src/metabase/driver/connection.cljindex 73332d436ce..1b99609fdd7 100644--- a/src/metabase/driver/connection.clj+++ b/src/metabase/driver/connection.clj@@ -9,7 +9,7 @@Primary API: [[effective-details]], [[with-write-connection]], [[default-details]]."(:require- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.driver.connection.workspaces :as driver.w][metabase.driver.util :as driver.u][metabase.premium-features.core :refer [defenterprise]]@@ -119,7 +119,7 @@(when (and write-details(not *suppress-resolution-telemetry*)(not (driver.w/has-connection-swap? (:id database))))- (try (prometheus/inc! :metabase-db-connection/type-resolved {:connection-type "write-data"})+ (try (analytics/inc! :metabase-db-connection/type-resolved {:connection-type "write-data"})(catch Exception _ nil)))(-> (driver.w/maybe-swap-details (:id database) base)(assoc ::effective-connection-type (if write-details :write-data :default))@@ -168,7 +168,7 @@(if-let [conn-type (::effective-connection-type connection-details)](do(log/debugf "Acquiring %s connection for db %s" conn-type (::database-id connection-details))- (try (prometheus/inc! :metabase-db-connection/write-op {:connection-type (name conn-type)})+ (try (analytics/inc! :metabase-db-connection/write-op {:connection-type (name conn-type)})(catch Exception _ nil)))(log/warnf "%s was unable to determine connection type" `track-connection-acquisition!)))diff --git a/src/metabase/frontend_errors/api.clj b/src/metabase/frontend_errors/api.cljindex 8e184a771fd..272abdc7d68 100644--- a/src/metabase/frontend_errors/api.clj+++ b/src/metabase/frontend_errors/api.clj@@ -1,6 +1,6 @@(ns metabase.frontend-errors.api(:require- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.request.core :as request]@@ -48,7 +48,7 @@_query-params{:keys [type]} :- [:map [:type ::frontend-error-type]]_req]- (prometheus/inc! :metabase-frontend/errors {:type type})+ (analytics/inc! :metabase-frontend/errors {:type type})api/generic-204-no-content)(def ^{:arglists '([request respond raise])} routesdiff --git a/src/metabase/lib/js.cljs b/src/metabase/lib/js.cljsindex aa098ebb5f0..b945126e775 100644--- a/src/metabase/lib/js.cljs+++ b/src/metabase/lib/js.cljs@@ -60,6 +60,8 @@[clojure.string :as str][goog.object :as gobject][medley.core :as m]+ [metabase.analytics-interface.core :as analytics.interface]+ [metabase.analytics.impl]^{:clj-kondo/ignore [:discouraged-namespace]} [metabase.legacy-mbql.normalize :as mbql.normalize][metabase.lib.aggregation :as lib.aggregation][metabase.lib.binning :as lib.binning]@@ -95,7 +97,18 @@[metabase.util.time :as u.time]));;; This ensures that all of metabase.lib.* is loaded, so all the `defmethod`s are properly registered.-(comment lib.core/keep-me)+;;; metabase.analytics.impl registers the CLJS reporter for [[metabase.analytics-interface.core]].++(comment lib.core/keep-me+ metabase.analytics.impl/keep-me)++;; Expose for E2E testing+(when (exists? js/window)+ (set! (.-__internalAnalytics js/window)+ #js {:inc (fn [metric labels amount]+ (analytics.interface/inc! (keyword metric)+ (js->clj labels :keywordize-keys true)+ (or amount 1)))}))(defn ^:export suggestedName"Return a nice description of a query.diff --git a/src/metabase/metabot/agent/core.clj b/src/metabase/metabot/agent/core.cljindex 6b951c1edfb..4a98d1f60fb 100644--- a/src/metabase/metabot/agent/core.clj+++ b/src/metabase/metabot/agent/core.clj@@ -3,7 +3,7 @@(:require[clojure.java.io :as io][clojure.string :as str]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.api-scope.core :as api-scope][metabase.api.common :as api][metabase.config.core :as config]@@ -604,7 +604,7 @@(with-span :info {:name :metabot.agent/run-agent-loop:profile-id profile-id:msg-count (count (:messages opts))}- (prometheus/inc! :metabase-metabot/agent-requests labels)+ (analytics/inc! :metabase-metabot/agent-requests labels)(let [start-ms (u/start-timer)](binding [*debug-log* (when debug? (atom []))scope/*current-user-scope* scopes@@ -616,13 +616,13 @@(iterate loop-step)(drop-while #(= :continue (:status %)))first)]- (prometheus/observe! :metabase-metabot/agent-iterations labels iteration)+ (analytics/observe! :metabase-metabot/agent-iterations labels iteration);; Emit debug log as a data part if debug mode was active(if (and debug? (seq @*debug-log*))(rf result (debug-log-part @*debug-log*))result))(catch Exception e- (prometheus/inc! :metabase-metabot/agent-errors labels)+ (analytics/inc! :metabase-metabot/agent-errors labels)(if (:api-error (ex-data e))(if (:status (ex-data e))(log/errorf "Agent loop API error: %s status=%s provider=%s body=%s"@@ -636,4 +636,4 @@(log/error e "Agent loop error"))(rf init (error-part e)))(finally- (prometheus/observe! :metabase-metabot/agent-duration-ms labels (u/since-ms start-ms)))))))))))+ (analytics/observe! :metabase-metabot/agent-duration-ms labels (u/since-ms start-ms)))))))))))diff --git a/src/metabase/metabot/api.clj b/src/metabase/metabot/api.cljindex ec787cfa5f4..9fd3dfd5fb7 100644--- a/src/metabase/metabot/api.clj+++ b/src/metabase/metabot/api.clj@@ -4,7 +4,7 @@[clojure.core.async :as a][clojure.string :as str][medley.core :as m]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.api.routes.common :refer [+auth]]@@ -117,9 +117,9 @@content (->> parts(remove #(#{:start :usage :finish :data} (:type %)))(mapv strip-tool-output-bloat))]- (prometheus/observe! :metabase-metabot/message-persist-bytes- {:profile-id (or profile-id "unknown")}- (u/string-byte-count (json/encode content)))+ (analytics/observe! :metabase-metabot/message-persist-bytes+ {:profile-id (or profile-id "unknown")}+ (u/string-byte-count (json/encode content)))(t2/with-transaction [_conn](when state-part(app-db/update-or-insert! :model/MetabotConversation {:id conversation-id}diff --git a/src/metabase/metabot/self.clj b/src/metabase/metabot/self.cljindex 48963864e44..064a5494fbd 100644--- a/src/metabase/metabot/self.clj+++ b/src/metabase/metabot/self.clj@@ -10,8 +10,8 @@TODO:- figure out what's lacking compared to ai-service"(:require- [metabase.analytics.core :as analytics]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.api.common :as api][metabase.metabot.provider-util :as provider-util][metabase.metabot.self.claude :as claude]@@ -137,10 +137,10 @@[tracking-opts](map (fn [part](when (= (:type part) :error)- (prometheus/inc! :metabase-metabot/llm-errors- {:model (:model tracking-opts "unknown")- :source (:tag tracking-opts "none")- :error-type "llm-sse-error"}))+ (analytics/inc! :metabase-metabot/llm-errors+ {:model (:model tracking-opts "unknown")+ :source (:tag tracking-opts "none")+ :error-type "llm-sse-error"}))part)))(defn- report-token-usage-xf@@ -164,7 +164,7 @@model (or model (:model part) "unknown")prompt (:promptTokens usage 0)completion (:completionTokens usage 0)]- (analytics/track-token-usage!+ (analytics.core/track-token-usage!;; The caller can omit request-id (and other snowplow opts) to skip snowplow tracking.{:prometheus true:snowplow (some? request-id)@@ -176,7 +176,7 @@:estimated-costs-usd 0.0:duration-ms (long (u/since-ms start-ms)):user-id api/*current-user-id*- :request-id (some-> request-id analytics/uuid->ai-service-hex-uuid)+ :request-id (some-> request-id analytics.core/uuid->ai-service-hex-uuid):session-id session-id:source source:tag tag})@@ -199,18 +199,18 @@(when (and (some? source)(some? request-id)(= (:type part) :tool-output))- (analytics/track-event! :snowplow/ai_service_event- {:hashed-metabase-license-token (analytics/hashed-metabase-token-or-uuid)- :request-id (analytics/uuid->ai-service-hex-uuid request-id)- :source source- :event "agent_used_tool"- :user-id api/*current-user-id*- :session-id session-id- :profile (some-> profile-id name)- :duration-ms (some-> (:duration-ms part) long)- :result (if (:error part) "error" "success")- :event-details (cond-> {"tool_name" (:function part)}- (some? iteration) (assoc "step" iteration))}))+ (analytics.core/track-event! :snowplow/ai_service_event+ {:hashed-metabase-license-token (analytics.core/hashed-metabase-token-or-uuid)+ :request-id (analytics.core/uuid->ai-service-hex-uuid request-id)+ :source source+ :event "agent_used_tool"+ :user-id api/*current-user-id*+ :session-id session-id+ :profile (some-> profile-id name)+ :duration-ms (some-> (:duration-ms part) long)+ :result (if (:error part) "error" "success")+ :event-details (cond-> {"tool_name" (:function part)}+ (some? iteration) (assoc "step" iteration))}))part)))(defn- with-retries@@ -220,7 +220,7 @@[tracking-opts thunk](let [labels {:model (:model tracking-opts) :source (:tag tracking-opts)}](loop [attempt 1]- (prometheus/inc! :metabase-metabot/llm-requests labels)+ (analytics/inc! :metabase-metabot/llm-requests labels)(let [timer (u/start-timer)result (try{:ok (thunk)}@@ -233,13 +233,13 @@:max max-llm-retries:delay delay:status (:status (ex-data e))})- (prometheus/inc! :metabase-metabot/llm-retries labels)+ (analytics/inc! :metabase-metabot/llm-retries labels){:retry delay})- (do (prometheus/inc! :metabase-metabot/llm-errors- (assoc labels :error-type (.getSimpleName (class e))))+ (do (analytics/inc! :metabase-metabot/llm-errors+ (assoc labels :error-type (.getSimpleName (class e))))(throw e))))(finally- (prometheus/observe! :metabase-metabot/llm-duration-ms labels (u/since-ms timer))))]+ (analytics/observe! :metabase-metabot/llm-duration-ms labels (u/since-ms timer))))](if-let [delay (:retry result)](do (Thread/sleep ^long delay)(recur (inc attempt)))diff --git a/src/metabase/notification/payload/temp_storage.clj b/src/metabase/notification/payload/temp_storage.cljindex 68cf4418119..4f751d4a169 100644--- a/src/metabase/notification/payload/temp_storage.clj+++ b/src/metabase/notification/payload/temp_storage.clj@@ -13,7 +13,7 @@threshold. See :notification/file-too-large and :notification/truncated?."(:require[clojure.java.io :as io]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.notification.settings :as notification.settings][metabase.query-processor.schema :as qp.schema][metabase.util :as u]@@ -220,11 +220,11 @@(.close output-stream)(let [file-size (.length file)max-bytes (notification.settings/notification-temp-file-size-max-bytes)]- (prometheus/inc! :metabase-notification/temp-storage- {:storage (cond (:notification/truncated? @streaming-state) :truncated- (zero? max-bytes) :not-limited- (< max-bytes file-size) :above-threshold- :else :disk)})+ (analytics/inc! :metabase-notification/temp-storage+ {:storage (cond (:notification/truncated? @streaming-state) :truncated+ (zero? max-bytes) :not-limited+ (< max-bytes file-size) :above-threshold+ :else :disk)})(log/infof "💾 Stored %d rows to disk: %s (never loaded into memory)%s"@row-count(human-readable-size file-size)@@ -244,8 +244,8 @@;; Return in-memory rows(do- (prometheus/inc! :metabase-notification/temp-storage- {:storage :memory})+ (analytics/inc! :metabase-notification/temp-storage+ {:storage :memory})(log/infof "✓ Completed with %d rows in memory (under threshold)" @row-count)(-> result(assoc :row_count @row-countdiff --git a/src/metabase/notification/send.clj b/src/metabase/notification/send.cljindex 43ceb317b90..568a0bf8668 100644--- a/src/metabase/notification/send.clj+++ b/src/metabase/notification/send.clj@@ -1,7 +1,8 @@(ns metabase.notification.send(:require[java-time.api :as t]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.channel.core :as channel][metabase.config.core :as config][metabase.notification.models :as models.notification]@@ -88,11 +89,11 @@(channel/send! channel message))(log/debugf "Sent with %d retries" (count @retry-errors))(log/info "Sent successfully")))- (prometheus/inc! :metabase-notification/channel-send-ok {:payload-type payload-type- :channel-type channel-type})+ (analytics/inc! :metabase-notification/channel-send-ok {:payload-type payload-type+ :channel-type channel-type})(catch Throwable e- (prometheus/inc! :metabase-notification/channel-send-error {:payload-type payload-type- :channel-type channel-type})+ (analytics/inc! :metabase-notification/channel-send-error {:payload-type payload-type+ :channel-type channel-type})(log/warn e "Failed to send")))))(defn- hydrate-notification@@ -119,10 +120,10 @@{:payload-type payload-type:channel-type channel-type}))-(defmethod prometheus/known-labels :metabase-notification/send-ok [_] payload-labels)-(defmethod prometheus/known-labels :metabase-notification/send-error [_] payload-labels)-(defmethod prometheus/known-labels :metabase-notification/channel-send-ok [_] payload-channel-labels)-(defmethod prometheus/known-labels :metabase-notification/channel-send-error [_] payload-channel-labels)+(defmethod analytics.core/known-labels :metabase-notification/send-ok [_] payload-labels)+(defmethod analytics.core/known-labels :metabase-notification/send-error [_] payload-labels)+(defmethod analytics.core/known-labels :metabase-notification/channel-send-ok [_] payload-channel-labels)+(defmethod analytics.core/known-labels :metabase-notification/channel-send-error [_] payload-channel-labels)(defn- since-trigger-ms[notification-info]@@ -136,10 +137,10 @@(u/with-timer-ms[duration-ms-fn](when-let [wait-time (since-trigger-ms notification-info)]- (prometheus/observe! :metabase-notification/wait-duration-ms {:payload-type payload_type} wait-time))+ (analytics/observe! :metabase-notification/wait-duration-ms {:payload-type payload_type} wait-time))(try(log/info "Sending")- (prometheus/inc! :metabase-notification/concurrent-tasks)+ (analytics/inc! :metabase-notification/concurrent-tasks);; Guard against orphaned card notifications whose payload record was cascade-deleted.;; Only applies to persisted notifications (with a non-nil :payload_id); Pulse-converted;; notifications pass through :payload instead and have no :payload_id.@@ -180,18 +181,18 @@(log/errorf e "Error sending to channel %s" (handler->channel-name handler))))))(log/info "Done processing notification")))(do-after-notification-sent hydrated-notification notification-payload (some? skip-reason))- (prometheus/inc! :metabase-notification/send-ok {:payload-type payload_type}))))+ (analytics/inc! :metabase-notification/send-ok {:payload-type payload_type}))))(catch Exception e(log/error e "Failed to send")- (prometheus/inc! :metabase-notification/send-error {:payload-type payload_type})+ (analytics/inc! :metabase-notification/send-error {:payload-type payload_type})(throw e))(finally- (prometheus/dec! :metabase-notification/concurrent-tasks)+ (analytics/dec-gauge! :metabase-notification/concurrent-tasks)(when-let [run-id (task-history/current-run-id)](task-history/complete-task-run! run-id))))- (prometheus/observe! :metabase-notification/send-duration-ms {:payload-type payload_type} (duration-ms-fn))+ (analytics/observe! :metabase-notification/send-duration-ms {:payload-type payload_type} (duration-ms-fn))(when-let [total-time (since-trigger-ms notification-info)]- (prometheus/observe! :metabase-notification/total-duration-ms {:payload-type payload_type} total-time))+ (analytics/observe! :metabase-notification/total-duration-ms {:payload-type payload_type} total-time))nil)))(defn- cron->next-execution-timesdiff --git a/src/metabase/premium_features/token_check.clj b/src/metabase/premium_features/token_check.cljindex 704573749fb..c4fff1b23c8 100644--- a/src/metabase/premium_features/token_check.clj+++ b/src/metabase/premium_features/token_check.clj@@ -15,7 +15,7 @@[diehard.core :as dh][environ.core :refer [env]][java-time.api :as t]- [metabase.analytics.prometheus :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.app-db.core :as app-db][metabase.config.core :as config][metabase.events.core :as events]@@ -198,7 +198,7 @@(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) (do (analytics/inc-if-initialized! :metabase-token-check/attempt {:status :success})+ (http/success? resp) (do (analytics/inc! :metabase-token-check/attempt {:status :success})(some-> body json/decode+kw (assoc :canonical? true)))(<= 400 status 499) (or (some-> body json/decode+kw (assoc :canonical? true)){:valid false@@ -206,7 +206,7 @@:status "Unable to validate token":error-details "Token validation provided no response"});; exceptions are not cached.- :else (do (analytics/inc-if-initialized! :metabase-token-check/attempt {:status :failure})+ :else (do (analytics/inc! :metabase-token-check/attempt {:status :failure})(throw (ex-info "An unknown error occurred when validating token." {:status status:body body}))))))@@ -487,7 +487,7 @@(catch Exception e(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}))+ (analytics/inc! :metabase-token-check/attempt {:status :failure})){:valid false:canonical? false:status (tru "Unable to validate token")diff --git a/src/metabase/queries/models/card.clj b/src/metabase/queries/models/card.cljindex 3c16b6bc795..71795b0cb4d 100644--- a/src/metabase/queries/models/card.clj+++ b/src/metabase/queries/models/card.clj@@ -5,7 +5,7 @@[clojure.set :as set][honey.sql.helpers :as sql.helpers][medley.core :as m]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.api.common :as api][metabase.app-db.core :as app-db][metabase.audit-app.core :as audit]@@ -736,7 +736,7 @@(log/infof "Card %d has a blank :dataset_query - this indicates a Metabase issue. Legacy MBQL: %s"(:id card) (:legacy_query card))(let [uniques (swap! unique-cards-with-blank-dataset-query conj (:id card))]- (analytics/set! :metabase-card/unique-cards-failed-conversion (count uniques))))+ (analytics/set-gauge! :metabase-card/unique-cards-failed-conversion (count uniques))));; Always returns the original card.card)diff --git a/src/metabase/query_processor/middleware/catch_exceptions.clj b/src/metabase/query_processor/middleware/catch_exceptions.cljindex c9c54a57b89..6a0de6da57c 100644--- a/src/metabase/query_processor/middleware/catch_exceptions.clj+++ b/src/metabase/query_processor/middleware/catch_exceptions.clj@@ -3,7 +3,7 @@(:refer-clojure :exclude [some get-in])(:require[clojure.string :as str]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.driver :as driver][metabase.lib.schema.common :as lib.schema.common][metabase.query-processor.error-type :as qp.error-type]diff --git a/src/metabase/query_processor/middleware/metrics.clj b/src/metabase/query_processor/middleware/metrics.cljindex 7cf4bdf9fa4..cf5bf776c82 100644--- a/src/metabase/query_processor/middleware/metrics.clj+++ b/src/metabase/query_processor/middleware/metrics.clj@@ -2,7 +2,7 @@(:refer-clojure :exclude [select-keys some empty? not-empty])(:require[medley.core :as m]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.lib.options :as lib.options]diff --git a/src/metabase/query_processor/middleware/process_userland_query.clj b/src/metabase/query_processor/middleware/process_userland_query.cljindex 71a20d400b2..7615810265f 100644--- a/src/metabase/query_processor/middleware/process_userland_query.clj+++ b/src/metabase/query_processor/middleware/process_userland_query.clj@@ -8,7 +8,8 @@(:refer-clojure :exclude [every? empty? get-in])(:require[java-time.api :as t]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.events.core :as events][metabase.lib.computed :as lib.computed][metabase.queries.models.query :as query]@@ -53,7 +54,7 @@(defn- save-execution-metadata!"Save a `QueryExecution` row containing `execution-info`. Done asynchronously when a query is finished."[execution-info]- (let [execution-info' (analytics/include-sdk-info execution-info)]+ (let [execution-info' (analytics.core/include-sdk-info execution-info)](qp.util/with-execute-async;; 1. Asynchronously save QueryExecution, update query average execution time etc. using the Agent/pooledExecutor;; pool, which is a fixed pool of size `nthreads + 2`. This way we don't spin up a ton of threads doing unimportant@@ -176,7 +177,7 @@rff :- ::qp.schema/rff];; Update a gauge metric with the present number of queries in the WeakHashMap it maintains.;; This has to live somewhere and while processing each query seems like a natural place.- (analytics/set! :metabase.query-processor/computed-weak-map-queries (lib.computed/weak-map-population))+ (analytics/set-gauge! :metabase.query-processor/computed-weak-map-queries (lib.computed/weak-map-population))(if-not (qp.util/userland-query? query)(qp query rff)(let [query (assoc-in query [:info :query-hash] (qp.util/query-hash query))diff --git a/src/metabase/query_processor/pipeline.clj b/src/metabase/query_processor/pipeline.cljindex 06829405d87..d80cb9742f2 100644--- a/src/metabase/query_processor/pipeline.clj+++ b/src/metabase/query_processor/pipeline.clj@@ -1,7 +1,7 @@(ns metabase.query-processor.pipeline(:require[clojure.core.async :as a]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.driver :as driver][metabase.query-processor.error-type :as qp.error-type][metabase.util.i18n :as i18n]diff --git a/src/metabase/query_processor/streaming.clj b/src/metabase/query_processor/streaming.cljindex 727f02ed9a3..6a46b7a6a9f 100644--- a/src/metabase/query_processor/streaming.clj+++ b/src/metabase/query_processor/streaming.clj@@ -2,7 +2,7 @@(:refer-clojure :exclude [every? some mapv not-empty])(:require[clojure.string :as str]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.lib.core :as lib][metabase.lib.schema.common :as lib.schema.common][metabase.models.visualization-settings :as mb.viz]@@ -258,7 +258,7 @@(assert (not (instance? ManyToManyChannel result)) "QP should not return a core.async channel.")(when (or (instance? Throwable result)(= (:status result) :failed))- (prometheus/inc! :metabase-export/errors {:format (name export-format)})+ (analytics/inc! :metabase-export/errors {:format (name export-format)})(streaming-response/write-error! os result export-format (status-code result))))))))))(defn transforming-query-responsediff --git a/src/metabase/request/util.clj b/src/metabase/request/util.cljindex 1c8ed2e4a1c..20c4c698f37 100644--- a/src/metabase/request/util.clj+++ b/src/metabase/request/util.clj@@ -4,7 +4,7 @@[clj-http.client :as http][clojure.string :as str][java-time.api :as t]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.config.core :as config][metabase.embedding.util :as embed.util][metabase.request.settings :as request.settings]diff --git a/src/metabase/search/api.clj b/src/metabase/search/api.cljindex 7b5064b7450..33adbe3d4a2 100644--- a/src/metabase/search/api.clj+++ b/src/metabase/search/api.clj@@ -2,7 +2,7 @@(:require[clojure.string :as str][java-time.api :as t]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.api.common :as api][metabase.api.macros :as api.macros][metabase.api.open-api :as open-api]diff --git a/src/metabase/search/appdb/index.clj b/src/metabase/search/appdb/index.cljindex 7c413d6c45d..8ee38a990af 100644--- a/src/metabase/search/appdb/index.clj+++ b/src/metabase/search/appdb/index.clj@@ -3,7 +3,7 @@[clojure.set :as set][clojure.string :as str][honey.sql.helpers :as sql.helpers]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.app-db.core :as mdb][metabase.config.core :as config][metabase.search.appdb.specialization.api :as specialization]@@ -328,7 +328,7 @@(log/trace "indexed documents for " <>)(when active-updated(try- (analytics/set! :metabase-search/appdb-index-size (t2/count (name active-updated)))+ (analytics/set-gauge! :metabase-search/appdb-index-size (t2/count (name active-updated)))(catch Exception e(log/warnf e "Unable to measure active search index size (%s)" active-updated))))))))](if reindexing?@@ -362,9 +362,9 @@(into {}))(when (active-table)(try- (analytics/set! :metabase-search/appdb-index-size (:count (t2/query-one {:select [[:%count.* :count]]- :from [(active-table)]- :limit 1})))+ (analytics/set-gauge! :metabase-search/appdb-index-size (:count (t2/query-one {:select [[:%count.* :count]]+ :from [(active-table)]+ :limit 1})))(catch Exception e;; No point tracking the size of the newer index table, since we won't have modified it.(when-not (table-not-found-exception? e)diff --git a/src/metabase/search/core.clj b/src/metabase/search/core.cljindex 7f678e795b8..04475b43b27 100644--- a/src/metabase/search/core.clj+++ b/src/metabase/search/core.clj@@ -1,8 +1,8 @@(ns metabase.search.core"NOT the API namespace for the search module!! See [[metabase.search]] instead."(:require- [metabase.analytics.core :as analytics]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.lib-be.core :as lib-be][metabase.search.config :as search.config][metabase.search.engine :as search.engine]@@ -51,30 +51,30 @@to-tsquery-exprweighted-tsvector])-(defmethod analytics/known-labels :metabase-search/index-updates+(defmethod analytics.core/known-labels :metabase-search/index-updates[_](for [model (keys (search.spec/specifications))]{:model model}))-(defmethod analytics/known-labels :metabase-search/index-reindexes+(defmethod analytics.core/known-labels :metabase-search/index-reindexes[_](for [model (keys (search.spec/specifications))]{:model model}))-(defmethod analytics/known-labels :metabase-search/engine-default+(defmethod analytics.core/known-labels :metabase-search/engine-default[_]- (analytics/known-labels :metabase-search/engine-active))+ (analytics.core/known-labels :metabase-search/engine-active))-(defmethod analytics/known-labels :metabase-search/engine-active+(defmethod analytics.core/known-labels :metabase-search/engine-active[_](for [e (search.engine/known-engines)]{:engine (name e)}))-(defmethod analytics/initial-value :metabase-search/engine-default+(defmethod analytics.core/initial-value :metabase-search/engine-default[_ {:keys [engine]}](if (= engine (name (search.engine/default-engine))) 1 0))-(defmethod analytics/initial-value :metabase-search/engine-active+(defmethod analytics.core/initial-value :metabase-search/engine-active[_ {:keys [engine]}](if (search.engine/supported-engine? (keyword "search.engine" engine)) 1 0))@@ -101,7 +101,7 @@(if (seq report)(do(analytics/inc! :metabase-search/index-reindex-ms duration)- (prometheus/observe! :metabase-search/index-reindex-duration-ms duration)+ (analytics/observe! :metabase-search/index-reindex-duration-ms duration)(doseq [[model cnt] report](analytics/inc! :metabase-search/index-reindexes {:model model} cnt))(log/infof "Index initialized in %.0fms %s" duration (sort-by (comp - val) report))@@ -124,7 +124,7 @@(search.engine/reindex! e opts)))duration (u/since-ms timer)](analytics/inc! :metabase-search/index-reindex-ms duration)- (prometheus/observe! :metabase-search/index-reindex-duration-ms duration)+ (analytics/observe! :metabase-search/index-reindex-duration-ms duration)(doseq [[model cnt] report](analytics/inc! :metabase-search/index-reindexes {:model model} cnt))(log/infof "Done reindexing in %.0fms %s" duration (sort-by (comp - val) report))diff --git a/src/metabase/search/ingestion.clj b/src/metabase/search/ingestion.cljindex ca0e6cbba5f..3de9bfc049e 100644--- a/src/metabase/search/ingestion.clj+++ b/src/metabase/search/ingestion.clj@@ -3,8 +3,7 @@[clojure.string :as str][honey.sql.helpers :as sql.helpers][medley.core :as m]- [metabase.analytics.core :as analytics]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.app-db.core :as mdb][metabase.lib-be.core :as lib-be][metabase.search.engine :as search.engine]@@ -273,7 +272,7 @@duration (u/since-ms timer)](log/debugf "Updated search entries in %.0fms Updated: %s Deleted: %s" duration (sort-by (comp - val) update-report) (sort-by (comp - val) delete-report))(analytics/inc! :metabase-search/index-update-ms duration)- (prometheus/observe! :metabase-search/index-update-duration-ms duration)+ (analytics/observe! :metabase-search/index-update-duration-ms duration)(doseq [[model cnt] (merge-with + update-report delete-report)](analytics/inc! :metabase-search/index-updates {:model model} cnt))))))@@ -323,7 +322,7 @@{}))))(defn- track-queue-size! []- (analytics/set! :metabase-search/queue-size (.size queue)))+ (analytics/set-gauge! :metabase-search/queue-size (.size queue)))(defn- index-worker-exists? [](queue/listener-exists? listener-name))diff --git a/src/metabase/server/middleware/exceptions.clj b/src/metabase/server/middleware/exceptions.cljindex 6c5e1ea5158..32368142945 100644--- a/src/metabase/server/middleware/exceptions.clj+++ b/src/metabase/server/middleware/exceptions.clj@@ -3,7 +3,7 @@(:require[clojure.java.jdbc :as jdbc][clojure.string :as str]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.server.middleware.security :as mw.security][metabase.server.settings :as server.settings][metabase.util.i18n :refer [tru]]@@ -52,7 +52,7 @@{:message (.getMessage e)}other-info))](when (nil? status-code)- (prometheus/inc! :metabase-api/unhandled-errors))+ (analytics/inc! :metabase-api/unhandled-errors)){:status (or status-code 500):headers (mw.security/security-headers):body body}))diff --git a/src/metabase/server/statistics_handler.clj b/src/metabase/server/statistics_handler.cljindex e788ea72be1..1ebf6d34d22 100644--- a/src/metabase/server/statistics_handler.clj+++ b/src/metabase/server/statistics_handler.clj@@ -3,7 +3,7 @@Integrates directly with our prometheus integration, so we don't need to translate from one to another."(:require- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.util :as u])(:import(jakarta.servlet AsyncListener AsyncEvent)@@ -18,11 +18,11 @@[total-metric-name max-metric-name](let [stat-max (atom 0)](fn [value]- (prometheus/inc! total-metric-name value)+ (analytics/inc! total-metric-name value)(swap! stat-max (fn [current-max](let [new-max (max value current-max)](u/prog1 new-max- (prometheus/set! max-metric-name new-max))))))))+ (analytics/set-gauge! max-metric-name new-max))))))))(defn- stats-counter"Tracks a given stat as an atom nad logs total/max/current to prometheus. Returns a function that can be@@ -33,14 +33,14 @@(fn [inc-or-dec](assert (contains? #{:inc :dec} inc-or-dec) "Must call with :inc or :dec")(when (= :inc inc-or-dec)- (prometheus/inc! total-metric-name))+ (analytics/inc! total-metric-name))(letfn [(updater [{current-max :max :as current-stat}](let [new-current (update current-stat :current + (if (= inc-or-dec :inc) 1 -1))new-value (cond-> new-current(= :inc inc-or-dec) (assoc :max (max current-max (:current new-current))))](u/prog1 new-value- (prometheus/set! max-metric-name (:max new-value))- (prometheus/set! current-metric-name (:current new-value)))))]+ (analytics/set-gauge! max-metric-name (:max new-value))+ (analytics/set-gauge! current-metric-name (:current new-value)))))](swap! stat updater)))))(defn- inc!@@ -55,15 +55,15 @@[^Request base-request](let [response (.getResponse base-request)](if (.isCommitted response)- (prometheus/inc! :jetty/responses-total- (condp = (quot (.getStatus response) 100)- 1 {:code "1xx"}- 2 {:code "2xx"}- 3 {:code "3xx"}- 4 {:code "4xx"}- 5 {:code "5xx"}))- (prometheus/inc! :jetty/responses-total {:code "4xx"}))- (prometheus/inc! :jetty/responses-bytes-total (.getContentCount response))))+ (analytics/inc! :jetty/responses-total+ (condp = (quot (.getStatus response) 100)+ 1 {:code "1xx"}+ 2 {:code "2xx"}+ 3 {:code "3xx"}+ 4 {:code "4xx"}+ 5 {:code "5xx"}))+ (analytics/inc! :jetty/responses-total {:code "4xx"}))+ (analytics/inc! :jetty/responses-bytes-total (.getContentCount response))))(defn- on-complete-listener[request-counter async-counter request-time]@@ -71,7 +71,7 @@(onStartAsync [^AsyncEvent event](.. event getAsyncContext (addListener this)))(onTimeout [^AsyncEvent _]- (prometheus/inc! :jetty/expires-total))+ (analytics/inc! :jetty/expires-total))(onError [^AsyncEvent _])(onComplete [^AsyncEvent event](let [request (.. ^AsyncContextEvent event getHttpChannelState getBaseRequest)@@ -101,7 +101,7 @@(inc! requests));; resumed async request(u/prog1 (System/currentTimeMillis)- (prometheus/inc! :jetty/async-dispatches-total)))]+ (analytics/inc! :jetty/async-dispatches-total)))](try(.handle next path base-request request response)(finallydiff --git a/src/metabase/slackbot/api.clj b/src/metabase/slackbot/api.cljindex ef811c8da1c..f516f02055a 100644--- a/src/metabase/slackbot/api.clj+++ b/src/metabase/slackbot/api.clj@@ -3,7 +3,8 @@(:require[clojure.string :as str][malli.core :as mc]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.api.macros :as api.macros][metabase.channel.settings :as channel.settings][metabase.config.core :as config]@@ -190,13 +191,13 @@(str/join ", " skipped-files))})))(slackbot.streaming/send-response client event extra-history))))-(defmethod analytics/known-labels :metabase-slackbot/responses-generated [_]+(defmethod analytics.core/known-labels :metabase-slackbot/responses-generated [_][{:source "dm" :result "success"}{:source "dm" :result "error"}{:source "channel" :result "success"}{:source "channel" :result "error"}])-(defmethod analytics/known-labels :metabase-slackbot/file-uploads [_]+(defmethod analytics.core/known-labels :metabase-slackbot/file-uploads [_][{:result "success"} {:result "error"}])(defn- event-sourcediff --git a/src/metabase/slackbot/uploads.clj b/src/metabase/slackbot/uploads.cljindex 63b26d98a50..1ca51d03082 100644--- a/src/metabase/slackbot/uploads.clj+++ b/src/metabase/slackbot/uploads.clj@@ -3,7 +3,7 @@(:require[clojure.java.io :as io][clojure.string :as str]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.channel.settings :as channel.settings][metabase.slackbot.client :as slackbot.client][metabase.upload.core :as upload]diff --git a/src/metabase/sql_parsing/core.clj b/src/metabase/sql_parsing/core.cljindex a8e17231174..c26735719e3 100644--- a/src/metabase/sql_parsing/core.clj+++ b/src/metabase/sql_parsing/core.clj@@ -12,7 +12,7 @@(:require[clojure.string :as str][medley.core :as m]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.sql-parsing.common :as common][metabase.sql-parsing.pool :as python.pool][metabase.util :as u]diff --git a/src/metabase/sql_parsing/pool.clj b/src/metabase/sql_parsing/pool.cljindex 407995a84ec..5904898a255 100644--- a/src/metabase/sql_parsing/pool.clj+++ b/src/metabase/sql_parsing/pool.clj@@ -6,7 +6,7 @@[clojure.java.io :as io][clojure.java.shell :as shell][clojure.string :as str]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.sql-parsing.common :as common][metabase.util.files :as u.files][metabase.util.log :as log]diff --git a/src/metabase/sql_tools/metrics.clj b/src/metabase/sql_tools/metrics.cljindex 659372b89f3..2afcd8054b5 100644--- a/src/metabase/sql_tools/metrics.clj+++ b/src/metabase/sql_tools/metrics.clj@@ -5,7 +5,8 @@sql-tools public API calls with counters and histograms, labeled by parserbackend and operation name."(:require- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.util :as u]))(set! *warn-on-reflection* true)@@ -30,9 +31,9 @@operation operations]{:parser parser :operation operation})))-(defmethod analytics/known-labels :metabase-sql-tools/operations-total [_] operation-labels)-(defmethod analytics/known-labels :metabase-sql-tools/operations-completed [_] operation-labels)-(defmethod analytics/known-labels :metabase-sql-tools/operations-failed [_] operation-labels)+(defmethod analytics.core/known-labels :metabase-sql-tools/operations-total [_] operation-labels)+(defmethod analytics.core/known-labels :metabase-sql-tools/operations-completed [_] operation-labels)+(defmethod analytics.core/known-labels :metabase-sql-tools/operations-failed [_] operation-labels);;; -------------------------------------------------- Recording fns --------------------------------------------------diff --git a/src/metabase/sync/util.clj b/src/metabase/sync/util.cljindex c526fbbd91d..f59ed3fef30 100644--- a/src/metabase/sync/util.clj+++ b/src/metabase/sync/util.clj@@ -6,7 +6,7 @@[clojure.string :as str][java-time.api :as t][medley.core :as m]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.app-db.core :as mdb][metabase.driver :as driver][metabase.driver.util :as driver.u]@@ -283,7 +283,7 @@(partial do-with-error-handling (format "Error in sync step %s" message) f))))))result (sync-fn)](when (instance? Throwable result)- (prometheus/inc! :metabase-sync/failures {:driver (name (:engine database))}))+ (analytics/inc! :metabase-sync/failures {:driver (name (:engine database))}))result)))))(defmacro sync-operationdiff --git a/src/metabase/transforms/instrumentation.clj b/src/metabase/transforms/instrumentation.cljindex 04d7904abd2..0bb88143eda 100644--- a/src/metabase/transforms/instrumentation.clj+++ b/src/metabase/transforms/instrumentation.clj@@ -1,6 +1,6 @@(ns metabase.transforms.instrumentation(:require- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.util :as u][metabase.util.log :as log][metabase.util.malli :as mu]@@ -15,9 +15,9 @@[job-run-id :- [:maybe pos-int?][stage-type stage-label] :- ::stage](log/infof "Transform stage started: run-id=%d type=%s label=%s" job-run-id (name stage-type) (name stage-label))- (prometheus/inc! :metabase-transforms/stage-started- {:stage-type (name stage-type)- :stage-label (name stage-label)}))+ (analytics/inc! :metabase-transforms/stage-started+ {:stage-type (name stage-type)+ :stage-label (name stage-label)}))(mu/defn record-stage-completion!"Record the successful completion of a transform stage."@@ -27,8 +27,8 @@(log/infof "Transform stage completed: run-id=%d type=%s label=%s duration=%dms" job-run-id (name stage-type) (name stage-label) duration-ms)(let [labels {:stage-type (name stage-type):stage-label (name stage-label)}]- (prometheus/inc! :metabase-transforms/stage-completed labels)- (prometheus/observe! :metabase-transforms/stage-duration-ms labels duration-ms)))+ (analytics/inc! :metabase-transforms/stage-completed labels)+ (analytics/observe! :metabase-transforms/stage-duration-ms labels duration-ms)))(mu/defn record-stage-failure!"Record the failure of a transform stage."@@ -38,9 +38,9 @@(log/warnf "Transform stage failed: run-id=%d type=%s label=%s duration=%s" job-run-id (name stage-type) (name stage-label) (str duration-ms "ms"))(let [labels {:stage-type (name stage-type):stage-label (name stage-label)}]- (prometheus/inc! :metabase-transforms/stage-failed labels)+ (analytics/inc! :metabase-transforms/stage-failed labels)(when duration-ms- (prometheus/observe! :metabase-transforms/stage-duration-ms labels duration-ms))))+ (analytics/observe! :metabase-transforms/stage-duration-ms labels duration-ms))))(defn with-timing"Generic timing function that executes body while recording metrics.@@ -101,32 +101,32 @@(if rows (str " rows=" rows) "")))(let [labels {:stage-label (name stage-label)}](when bytes- (prometheus/observe! :metabase-transforms/data-transfer-bytes labels bytes))+ (analytics/observe! :metabase-transforms/data-transfer-bytes labels bytes))(when rows- (prometheus/observe! :metabase-transforms/data-transfer-rows labels rows))))+ (analytics/observe! :metabase-transforms/data-transfer-rows labels rows))))(defn record-job-start!"Record the start of a transform job run."[job-id run-method](log/infof "Transform job started: job-id=%d run-method=%s" job-id (name run-method))- (prometheus/inc! :metabase-transforms/job-runs-total- {:run-method (name run-method)}))+ (analytics/inc! :metabase-transforms/job-runs-total+ {:run-method (name run-method)}))(defn record-job-completion!"Record the successful completion of a transform job run."[job-id run-method duration-ms](log/infof "Transform job completed: job-id=%d run-method=%s duration=%dms" job-id (name run-method) duration-ms)(let [labels {:run-method (name run-method)}]- (prometheus/inc! :metabase-transforms/job-runs-completed labels)- (prometheus/observe! :metabase-transforms/job-run-duration-ms labels duration-ms)))+ (analytics/inc! :metabase-transforms/job-runs-completed labels)+ (analytics/observe! :metabase-transforms/job-run-duration-ms labels duration-ms)))(defn record-job-failure!"Record the failure of a transform job run."[job-id run-method duration-ms](log/warnf "Transform job failed: job-id=%d run-method=%s duration=%dms" job-id (name run-method) duration-ms)(let [labels {:run-method (name run-method)}]- (prometheus/inc! :metabase-transforms/job-runs-failed labels)- (prometheus/observe! :metabase-transforms/job-run-duration-ms labels duration-ms)))+ (analytics/inc! :metabase-transforms/job-runs-failed labels)+ (analytics/observe! :metabase-transforms/job-run-duration-ms labels duration-ms)))(defmacro with-job-timing"Execute body while timing a transform job run. Automatically records start, completion or failure.diff --git a/src/metabase/upload/impl.clj b/src/metabase/upload/impl.cljindex 35b026094b0..ffb5ced8a69 100644--- a/src/metabase/upload/impl.clj+++ b/src/metabase/upload/impl.clj@@ -8,7 +8,8 @@[flatland.ordered.map :as ordered-map][java-time.api :as t][medley.core :as m]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.api.common :as api][metabase.driver :as driver][metabase.driver.connection :as driver.conn]@@ -638,15 +639,15 @@:model-id (:id card):stats stats}})- (analytics/track-event! :snowplow/csvupload- (assoc stats- :event :csv-upload-successful- :model-id (:id card)))+ (analytics.core/track-event! :snowplow/csvupload+ (assoc stats+ :event :csv-upload-successful+ :model-id (:id card)))(assoc card :table-id (:id table)))(catch Throwable e(analytics/inc! :metabase-csv-upload/failed)- (analytics/track-event! :snowplow/csvupload (assoc (fail-stats filename file)- :event :csv-upload-failed))+ (analytics.core/track-event! :snowplow/csvupload (assoc (fail-stats filename file)+ :event :csv-upload-failed))(throw e)))))@@ -858,13 +859,13 @@:table-name (:name table):stats stats}})- (analytics/track-event! :snowplow/csvupload (assoc stats :event :csv-append-successful))+ (analytics.core/track-event! :snowplow/csvupload (assoc stats :event :csv-append-successful)){:row-count row-count})))(catch Throwable e(analytics/inc! :metabase-csv-upload/failed)- (analytics/track-event! :snowplow/csvupload (assoc (fail-stats filename file)- :event :csv-append-failed))+ (analytics.core/track-event! :snowplow/csvupload (assoc (fail-stats filename file)+ :event :csv-append-failed))(throw e)))))(defn- can-update-errordiff --git a/src/metabase/warehouses/models/database.clj b/src/metabase/warehouses/models/database.cljindex 840e4b3c2f0..17462d880df 100644--- a/src/metabase/warehouses/models/database.clj+++ b/src/metabase/warehouses/models/database.clj@@ -3,7 +3,7 @@[clojure.core.match :refer [match]][clojure.data :as data][medley.core :as m]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.api.common :as api][metabase.app-db.core :as mdb][metabase.audit-app.core :as audit]diff --git a/test/metabase/analytics/api_test.clj b/test/metabase/analytics/api_test.cljindex 7b70c144e23..f24bf1ad4aa 100644--- a/test/metabase/analytics/api_test.clj+++ b/test/metabase/analytics/api_test.clj@@ -11,3 +11,16 @@(mt/user-http-request :rasta :get 403 "analytics/anonymous-stats"))))(testing "Call successful for superusers"(is (map? (mt/user-http-request :crowberto :get 200 "analytics/anonymous-stats"))))))++(deftest ^:parallel internal-analytics-test+ (testing "POST /api/analytics/internal"+ (testing "accepts a valid batch of events"+ (is (nil? (mt/user-http-request :crowberto :post 204 "analytics/internal"+ {:events [{:op :inc :metric :test/counter :labels {:x "y"} :amount 1}+ {:op :observe :metric :test/histogram :amount 42}]}))))+ (testing "rejects invalid payload"+ (mt/user-http-request :rasta :post 400 "analytics/internal"+ {:events [{:op :bad :metric :test/counter}]}))+ (testing "rejects missing events key"+ (mt/user-http-request :rasta :post 400 "analytics/internal"+ {:not-events []}))))diff --git a/test/metabase/analytics/impl_test.cljs b/test/metabase/analytics/impl_test.cljsnew file mode 100644index 00000000000..1a5e20be95d--- /dev/null+++ b/test/metabase/analytics/impl_test.cljs@@ -0,0 +1,38 @@+(ns metabase.analytics.impl-test+ (:require+ [clojure.test :refer [deftest is testing]]+ [metabase.analytics.impl :as impl]))++(deftest ^:parallel enqueue-test+ (testing "appends event when under capacity"+ (is (= {:events [{:a 1} {:b 2}] :dropped 0}+ (#'impl/enqueue {:events [{:a 1}] :dropped 0} {:b 2} 10))))+ (testing "drops the oldest event when adding at capacity"+ (is (= {:events [{:b 2} {:c 3}] :dropped 1}+ (#'impl/enqueue {:events [{:a 1} {:b 2}] :dropped 0} {:c 3} 2))))+ (testing "accumulates the dropped count across overflows"+ (is (= {:events [{:b 2} {:c 3}] :dropped 6}+ (#'impl/enqueue {:events [{:a 1} {:b 2}] :dropped 5} {:c 3} 2)))))++(deftest ^:parallel take-pending-test+ (testing "returns buffered events and resets the state"+ (is (= [[{:a 1} {:b 2}] {:events [] :dropped 0}]+ (#'impl/take-pending {:events [{:a 1} {:b 2}] :dropped 0}))))+ (testing "returns an empty payload when nothing is buffered and nothing was dropped"+ (is (= [[] {:events [] :dropped 0}]+ (#'impl/take-pending {:events [] :dropped 0}))))+ (testing "prepends a dropped-count :inc event when drops have occurred"+ (is (= [[{:op :inc+ :metric :metabase-frontend/analytics-events-dropped+ :labels nil+ :amount 3}+ {:a 1}]+ {:events [] :dropped 0}]+ (#'impl/take-pending {:events [{:a 1}] :dropped 3}))))+ (testing "emits only the dropped-count event when the buffer is empty but drops occurred"+ (is (= [[{:op :inc+ :metric :metabase-frontend/analytics-events-dropped+ :labels nil+ :amount 2}]+ {:events [] :dropped 0}]+ (#'impl/take-pending {:events [] :dropped 2})))))diff --git a/test/metabase/analytics/llm_token_usage_test.clj b/test/metabase/analytics/llm_token_usage_test.cljindex 2c80e17a70b..f40df5a895b 100644--- a/test/metabase/analytics/llm_token_usage_test.clj+++ b/test/metabase/analytics/llm_token_usage_test.clj@@ -1,8 +1,8 @@(ns metabase.analytics.llm-token-usage-test(:require[clojure.test :refer :all]+ [metabase.analytics-interface.core :as analytics][metabase.analytics.llm-token-usage :as llm-token-usage]- [metabase.analytics.prometheus :as prometheus][metabase.analytics.snowplow-test :as snowplow-test][metabase.test :as mt]))@@ -81,9 +81,9 @@(defn- clear-llm-metrics! [];; mt/with-prometheus-system! is slow, so prefer to clear metrics between test cases- (prometheus/clear! :metabase-metabot/llm-input-tokens)- (prometheus/clear! :metabase-metabot/llm-output-tokens)- (prometheus/clear! :metabase-metabot/llm-tokens-per-call))+ (analytics/clear! :metabase-metabot/llm-input-tokens)+ (analytics/clear! :metabase-metabot/llm-output-tokens)+ (analytics/clear! :metabase-metabot/llm-tokens-per-call))(deftest track-prometheus!-test(mt/with-prometheus-system! [_ system]diff --git a/test/metabase/analytics/prometheus_test.clj b/test/metabase/analytics/prometheus_test.cljindex 0a628ce1bf2..772f03696e0 100644--- a/test/metabase/analytics/prometheus_test.clj+++ b/test/metabase/analytics/prometheus_test.clj@@ -5,7 +5,6 @@[clojure.string :as str][clojure.test :refer :all][iapetos.registry :as registry]- [metabase.analytics.core :as analytics][metabase.analytics.prometheus :as prometheus][metabase.search.core :as search][metabase.test :as mt]@@ -175,7 +174,7 @@(mt/with-prometheus-system! [_ _system](is (thrown-with-msg? RuntimeException#"error when updating metric"- (analytics/inc! :metabase-email/unknown-metric)))))+ (prometheus/inc! :metabase-email/unknown-metric)))))(testing "inc is recorded for known metrics"(mt/with-prometheus-system! [_ system](prometheus/inc! :metabase-email/messages)diff --git a/test/metabase/analytics_interface/core_test.cljc b/test/metabase/analytics_interface/core_test.cljcnew file mode 100644index 00000000000..57cd04e1017--- /dev/null+++ b/test/metabase/analytics_interface/core_test.cljc@@ -0,0 +1,120 @@+(ns metabase.analytics-interface.core-test+ (:require+ [clojure.test :refer [deftest is testing]]+ [metabase.analytics-interface.core :as analytics.interface]))++(defn- make-test-reporter [calls]+ (reify analytics.interface/Reporter+ (-inc! [_ metric labels amount]+ (swap! calls conj {:op :inc! :metric metric :labels labels :amount amount}))+ (-dec-gauge! [_ metric labels amount]+ (swap! calls conj {:op :dec-gauge! :metric metric :labels labels :amount amount}))+ (-set-gauge! [_ metric labels amount]+ (swap! calls conj {:op :set-gauge! :metric metric :labels labels :amount amount}))+ (-observe! [_ metric labels amount]+ (swap! calls conj {:op :observe! :metric metric :labels labels :amount amount}))+ (-clear! [_ metric]+ (swap! calls conj {:op :clear! :metric metric}))))++(defn- do-with-test-reporter! [thunk]+ (let [calls (atom [])+ original-reporter (analytics.interface/get-reporter)]+ (try+ (analytics.interface/set-reporter! (make-test-reporter calls))+ (thunk calls)+ (finally+ (analytics.interface/set-reporter! original-reporter)))))++(deftest ^:synchronized no-op-when-no-reporter-test+ (let [original-reporter (analytics.interface/get-reporter)]+ (try+ (analytics.interface/set-reporter! nil)+ (testing "inc! is a no-op when no reporter is set"+ (is (nil? (analytics.interface/inc! :test/counter {:label "a"} 1))))+ (testing "dec-gauge! is a no-op when no reporter is set"+ (is (nil? (analytics.interface/dec-gauge! :test/gauge {:label "a"} 1))))+ (testing "set-gauge! is a no-op when no reporter is set"+ (is (nil? (analytics.interface/set-gauge! :test/gauge {:label "a"} 10))))+ (testing "observe! is a no-op when no reporter is set"+ (is (nil? (analytics.interface/observe! :test/histogram {:label "a"} 42))))+ (testing "clear! is a no-op when no reporter is set"+ (is (nil? (analytics.interface/clear! :test/counter))))+ (finally+ (analytics.interface/set-reporter! original-reporter)))))++(deftest ^:synchronized delegates-to-reporter-test+ (do-with-test-reporter!+ (fn [calls]+ (testing "inc! delegates to reporter"+ (analytics.interface/inc! :test/counter {:label "a"} 5)+ (is (= [{:op :inc! :metric :test/counter :labels {:label "a"} :amount 5}]+ @calls)))+ (testing "dec-gauge! delegates to reporter"+ (reset! calls [])+ (analytics.interface/dec-gauge! :test/gauge {:label "a"} 3)+ (is (= [{:op :dec-gauge! :metric :test/gauge :labels {:label "a"} :amount 3}]+ @calls)))+ (testing "set-gauge! delegates to reporter"+ (reset! calls [])+ (analytics.interface/set-gauge! :test/gauge {:label "a"} 10)+ (is (= [{:op :set-gauge! :metric :test/gauge :labels {:label "a"} :amount 10}]+ @calls)))+ (testing "observe! delegates to reporter"+ (reset! calls [])+ (analytics.interface/observe! :test/histogram {:label "b"} 42)+ (is (= [{:op :observe! :metric :test/histogram :labels {:label "b"} :amount 42}]+ @calls)))+ (testing "clear! delegates to reporter"+ (reset! calls [])+ (analytics.interface/clear! :test/counter)+ (is (= [{:op :clear! :metric :test/counter}]+ @calls))))))++(deftest ^:synchronized inc!-arity-test+ (do-with-test-reporter!+ (fn [calls]+ (testing "1-arity: metric only, defaults to nil labels and amount 1"+ (analytics.interface/inc! :test/counter)+ (is (= [{:metric :test/counter :labels nil :amount 1}]+ (mapv #(dissoc % :op) @calls))))+ (testing "2-arity with number: metric + amount"+ (reset! calls [])+ (analytics.interface/inc! :test/counter 5)+ (is (= [{:metric :test/counter :labels nil :amount 5}]+ (mapv #(dissoc % :op) @calls))))+ (testing "2-arity with map: metric + labels"+ (reset! calls [])+ (analytics.interface/inc! :test/counter {:x "y"})+ (is (= [{:metric :test/counter :labels {:x "y"} :amount 1}]+ (mapv #(dissoc % :op) @calls)))))))++(deftest ^:synchronized dec-gauge!-arity-test+ (do-with-test-reporter!+ (fn [calls]+ (testing "1-arity: metric only, defaults to nil labels and amount 1"+ (analytics.interface/dec-gauge! :test/gauge)+ (is (= [{:metric :test/gauge :labels nil :amount 1}]+ (mapv #(dissoc % :op) @calls))))+ (testing "2-arity with number: metric + amount"+ (reset! calls [])+ (analytics.interface/dec-gauge! :test/gauge 3)+ (is (= [{:metric :test/gauge :labels nil :amount 3}]+ (mapv #(dissoc % :op) @calls))))+ (testing "2-arity with map: metric + labels"+ (reset! calls [])+ (analytics.interface/dec-gauge! :test/gauge {:x "y"})+ (is (= [{:metric :test/gauge :labels {:x "y"} :amount 1}]+ (mapv #(dissoc % :op) @calls)))))))++(deftest ^:synchronized set-gauge!-arity-test+ (do-with-test-reporter!+ (fn [calls]+ (testing "2-arity: metric + amount, nil labels"+ (analytics.interface/set-gauge! :test/gauge 10)+ (is (= [{:metric :test/gauge :labels nil :amount 10}]+ (mapv #(dissoc % :op) @calls))))+ (testing "3-arity: metric + labels + amount"+ (reset! calls [])+ (analytics.interface/set-gauge! :test/gauge {:x "y"} 20)+ (is (= [{:metric :test/gauge :labels {:x "y"} :amount 20}]+ (mapv #(dissoc % :op) @calls)))))))diff --git a/test/metabase/metabot/agent/core_test.clj b/test/metabase/metabot/agent/core_test.cljindex 56b569fc990..7e0a49018db 100644--- a/test/metabase/metabot/agent/core_test.clj+++ b/test/metabase/metabot/agent/core_test.clj@@ -1,7 +1,7 @@(ns metabase.metabot.agent.core-test(:require[clojure.test :refer :all]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.analytics.snowplow-test :as snowplow-test][metabase.lib.core :as lib][metabase.lib.test-metadata :as meta]@@ -482,11 +482,11 @@{:profile-id "internal"})))));; clear! is much faster than a new mt/with-prometheus-system!- (prometheus/clear! :metabase-metabot/agent-requests)- (prometheus/clear! :metabase-metabot/agent-iterations)- (prometheus/clear! :metabase-metabot/agent-errors)- (prometheus/clear! :metabase-metabot/agent-duration-ms)- (prometheus/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/agent-requests)+ (analytics/clear! :metabase-metabot/agent-iterations)+ (analytics/clear! :metabase-metabot/agent-errors)+ (analytics/clear! :metabase-metabot/agent-duration-ms)+ (analytics/clear! :metabase-metabot/llm-requests)(testing "records agent-errors on failure"(with-redefs [openrouter/openrouter (fn [_] (throw (ex-info "boom" {})))]diff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.cljindex f2a8a38d39e..58222ced015 100644--- a/test/metabase/metabot/self_test.clj+++ b/test/metabase/metabot/self_test.clj@@ -4,7 +4,7 @@[clojure.java.io :as io][clojure.string :as str][clojure.test :refer :all]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.analytics.snowplow-test :as snowplow-test][metabase.metabot.self :as self][metabase.metabot.self.core :as self.core]@@ -591,8 +591,8 @@(is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))));; mt/with-prometheus-system! is slow, so clear! metrics between tests rather than creating a fresh system- (prometheus/clear! :metabase-metabot/llm-requests)- (prometheus/clear! :metabase-metabot/llm-duration-ms)+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-duration-ms)(testing "increments llm-retries on transient failures, no errors on eventual success"(let [calls (atom 0)]@@ -611,9 +611,9 @@(assoc labels :error-type "ExceptionInfo"))))(is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels))))))- (prometheus/clear! :metabase-metabot/llm-requests)- (prometheus/clear! :metabase-metabot/llm-retries)- (prometheus/clear! :metabase-metabot/llm-duration-ms)+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-retries)+ (analytics/clear! :metabase-metabot/llm-duration-ms)(testing "increments llm-errors on non-retryable failure, no retries"(with-redefs [openrouter/openrouter@@ -628,9 +628,9 @@(assoc labels :error-type "ExceptionInfo"))))(is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- (prometheus/clear! :metabase-metabot/llm-requests)- (prometheus/clear! :metabase-metabot/llm-errors)- (prometheus/clear! :metabase-metabot/llm-duration-ms)+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-errors)+ (analytics/clear! :metabase-metabot/llm-duration-ms)(testing "increments llm-errors with :error-type llm-sse-error on inline SSE errors"(with-redefs [openrouter/openrouter@@ -675,8 +675,8 @@(assoc labels :error-type "ExceptionInfo"))))(is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- (prometheus/clear! :metabase-metabot/llm-requests)- (prometheus/clear! :metabase-metabot/llm-duration-ms)+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-duration-ms)(testing "increments llm-retries on transient failures, no errors on eventual success"(let [calls (atom 0)]@@ -693,9 +693,9 @@(assoc labels :error-type "ExceptionInfo"))))(is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- (prometheus/clear! :metabase-metabot/llm-requests)- (prometheus/clear! :metabase-metabot/llm-retries)- (prometheus/clear! :metabase-metabot/llm-duration-ms)+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-retries)+ (analytics/clear! :metabase-metabot/llm-duration-ms)(testing "increments llm-errors on non-retryable failure, no retries"(with-redefs [openrouter/openrouter@@ -707,9 +707,9 @@(assoc labels :error-type "ExceptionInfo"))))(is (pos? (:sum (mt/metric-value system :metabase-metabot/llm-duration-ms labels)))))- (prometheus/clear! :metabase-metabot/llm-requests)- (prometheus/clear! :metabase-metabot/llm-errors)- (prometheus/clear! :metabase-metabot/llm-duration-ms)+ (analytics/clear! :metabase-metabot/llm-requests)+ (analytics/clear! :metabase-metabot/llm-errors)+ (analytics/clear! :metabase-metabot/llm-duration-ms)(testing "increments llm-errors with :error-type llm-sse-error on inline SSE errors"(with-redefs [openrouter/openrouterdiff --git a/test/metabase/search/api_test.clj b/test/metabase/search/api_test.cljindex 00b599e486f..8904896e766 100644--- a/test/metabase/search/api_test.clj+++ b/test/metabase/search/api_test.clj@@ -5,7 +5,7 @@[clojure.set :as set][clojure.string :as str][clojure.test :refer :all]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.collections.models.collection :as collection][metabase.content-verification.models.moderation-review :as moderation-review][metabase.indexed-entities.models.model-index :as model-index]diff --git a/test/metabase/server/middleware/sdk_test.clj b/test/metabase/server/middleware/sdk_test.cljindex 40562d6a896..b3303a3f183 100644--- a/test/metabase/server/middleware/sdk_test.clj+++ b/test/metabase/server/middleware/sdk_test.clj@@ -2,7 +2,8 @@(:require[clojure.string :as str][clojure.test :refer [are deftest is testing]]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics]+ [metabase.analytics.core :as analytics.core][metabase.analytics.sdk :as sdk][metabase.test :as mt][metabase.util :as u]@@ -22,7 +23,7 @@(deftest bind-client-test(are [client](let [request (mock-request {:client client})- handler (analytics/embedding-mw+ handler (analytics.core/embedding-mw(fn [_ respond _] (respond {:status 200 :body client})))response (handler request identity identity)](is (= client@@ -33,7 +34,7 @@(deftest bind-client-version-test(are [version](let [request (mock-request {:version version})- handler (analytics/embedding-mw+ handler (analytics.core/embedding-mw(fn [_ respond _] (respond {:status 200 :body version})))response (handler request identity identity)](is (= version@@ -45,9 +46,9 @@(mt/with-prometheus-system! [_ system];; X-Metabase-Client header == "embedding-sdk-react" => SDK context(let [request (mock-request {:client @#'sdk/embedding-sdk-client})- good (analytics/embedding-mw (fn [_ respond _] (respond {:status 200})))- bad (analytics/embedding-mw (fn [_ respond _] (respond {:status 400})))- exception (analytics/embedding-mw (fn [_ _respond raise] (raise {})))]+ good (analytics.core/embedding-mw (fn [_ respond _] (respond {:status 200})))+ bad (analytics.core/embedding-mw (fn [_ respond _] (respond {:status 400})))+ exception (analytics.core/embedding-mw (fn [_ _respond raise] (raise {})))](good request identity identity)(is (= 1.0 (mt/metric-value system :metabase-sdk/response {:status "200"})))(bad request identity identity)@@ -62,9 +63,9 @@(mt/with-prometheus-system! [_ system];; X-Metabase-Client header == "embedding-iframe" => iframe context(let [request (mock-request {:client @#'sdk/embedding-iframe-client})- good (analytics/embedding-mw (fn [_ respond _] (respond {:status 200})))- bad (analytics/embedding-mw (fn [_ respond _] (respond {:status 400})))- exception (analytics/embedding-mw (fn [_ _respond raise] (raise {})))]+ good (analytics.core/embedding-mw (fn [_ respond _] (respond {:status 200})))+ bad (analytics.core/embedding-mw (fn [_ respond _] (respond {:status 400})))+ exception (analytics.core/embedding-mw (fn [_ _respond raise] (raise {})))](good request identity identity)(is (= 1.0 (mt/metric-value system :metabase-embedding-iframe/response {:status "200"})))(bad request identity identity)@@ -80,9 +81,9 @@(with-redefs [analytics/inc! (fn [k _] (swap! prometheus-standin update k (fnil inc 0)))];; has X-Metabase-Client header, but it's not the SDK, so we don't track it(let [request (mock-request {:client "my-client"})- good (analytics/embedding-mw (fn [_ respond _] (respond {:status 200})))- bad (analytics/embedding-mw (fn [_ respond _] (respond {:status 400})))- exception (analytics/embedding-mw (fn [_ _respond raise] (raise {})))]+ good (analytics.core/embedding-mw (fn [_ respond _] (respond {:status 200})))+ bad (analytics.core/embedding-mw (fn [_ respond _] (respond {:status 400})))+ exception (analytics.core/embedding-mw (fn [_ _respond raise] (raise {})))](good request identity identity)(is (= {} @prometheus-standin))(bad request identity identity)@@ -94,9 +95,9 @@(let [prometheus-standin (atom {})](with-redefs [analytics/inc! (fn [k _] (swap! prometheus-standin update k (fnil inc 0)))](let [request (mock-request {}) ;; <= no X-Metabase-Client header => no SDK context- good (analytics/embedding-mw (fn [_ respond _] (respond {:status 200})))- bad (analytics/embedding-mw (fn [_ respond _] (respond {:status 400})))- exception (analytics/embedding-mw (fn [_ _respond raise] (raise {})))]+ good (analytics.core/embedding-mw (fn [_ respond _] (respond {:status 200})))+ bad (analytics.core/embedding-mw (fn [_ respond _] (respond {:status 400})))+ exception (analytics.core/embedding-mw (fn [_ _respond raise] (raise {})))](good request identity identity)(is (= {} @prometheus-standin))(bad request identity identity)@@ -106,18 +107,18 @@(deftest include-analytics-is-idempotent(let [m (atom {})]- (analytics/with-client! ["client-C"]- (analytics/with-version! ["1.33.7"]+ (analytics.core/with-client! ["client-C"]+ (analytics.core/with-version! ["1.33.7"](is (= {:embedding_client "client-C":embedding_version "1.33.7"}- (analytics/include-sdk-info @m)))- (swap! m analytics/include-sdk-info)))+ (analytics.core/include-sdk-info @m)))+ (swap! m analytics.core/include-sdk-info)));; unset the vars:- (analytics/with-client! [nil]- (analytics/with-version! [nil]+ (analytics.core/with-client! [nil]+ (analytics.core/with-version! [nil](is (= {:embedding_client "client-C":embedding_version "1.33.7"} @m))(testing "the values in m are used when the vars are not set"(is (= {:embedding_client "client-C":embedding_version "1.33.7"}- (analytics/include-sdk-info @m))))))))+ (analytics.core/include-sdk-info @m))))))))diff --git a/test/metabase/sql_tools/metrics_test.clj b/test/metabase/sql_tools/metrics_test.cljindex e305904d36e..8d8804b6fc8 100644--- a/test/metabase/sql_tools/metrics_test.clj+++ b/test/metabase/sql_tools/metrics_test.clj@@ -1,7 +1,7 @@(ns metabase.sql-tools.metrics-test(:require[clojure.test :refer :all]- [metabase.analytics.prometheus :as prometheus]+ [metabase.analytics-interface.core :as analytics][metabase.analytics.prometheus-test :as prometheus-test][metabase.sql-tools.metrics :as metrics][metabase.test :as mt]))@@ -73,11 +73,11 @@(deftest pool-metrics-registered-test(testing "Pool metrics are registered in prometheus"(mt/with-prometheus-system! [_ system]- (prometheus/inc! :metabase-sql-parsing/context-timeouts)+ (analytics/inc! :metabase-sql-parsing/context-timeouts)(is (prometheus-test/approx= 1 (mt/metric-value system :metabase-sql-parsing/context-timeouts)))- (prometheus/inc! :metabase-sql-parsing/context-acquisitions)+ (analytics/inc! :metabase-sql-parsing/context-acquisitions)(is (prometheus-test/approx= 1 (mt/metric-value system :metabase-sql-parsing/context-acquisitions)))- (prometheus/inc! :metabase-sql-parsing/context-creations)+ (analytics/inc! :metabase-sql-parsing/context-creations)(is (prometheus-test/approx= 1 (mt/metric-value system :metabase-sql-parsing/context-creations)))- (prometheus/inc! :metabase-sql-parsing/context-disposals-expired)+ (analytics/inc! :metabase-sql-parsing/context-disposals-expired)(is (prometheus-test/approx= 1 (mt/metric-value system :metabase-sql-parsing/context-disposals-expired))))))diff --git a/test/metabase/upload/impl_test.clj b/test/metabase/upload/impl_test.cljindex a973dac8bf7..a7720ff30a4 100644--- a/test/metabase/upload/impl_test.clj+++ b/test/metabase/upload/impl_test.clj@@ -8,7 +8,7 @@[clojure.string :as str][clojure.test :refer :all][flatland.ordered.map :as ordered-map]- [metabase.analytics.core :as analytics]+ [metabase.analytics-interface.core :as analytics][metabase.analytics.snowplow-test :as snowplow-test][metabase.driver :as driver][metabase.driver.connection :as driver.conn]