Skip to content

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

Define additional metrics for release monitoring (#71237)

Chris Truter76b01b772bbf
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index f6bc2fe3084..95c01a7b100 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -970,6 +970,13 @@
types
util}}
+ frontend-errors
+ {:team "UX West"
+ :api #{metabase.frontend-errors.api}
+ :uses #{analytics
+ api
+ util}}
+
geojson
{:team "UX West"
:api #{metabase.geojson.api
@@ -1947,7 +1954,8 @@
metabase.sync.schedules
metabase.sync.task.sync-databases
metabase.sync.util}
- :uses #{analyze
+ :uses #{analytics
+ analyze
api
app-db
audit-app
diff --git a/frontend/src/metabase/ErrorBoundary.tsx b/frontend/src/metabase/ErrorBoundary.tsx
index 94aa5e34e3e..55d99903dd9 100644
--- a/frontend/src/metabase/ErrorBoundary.tsx
+++ b/frontend/src/metabase/ErrorBoundary.tsx
@@ -7,6 +7,7 @@ import type {
import { Component, forwardRef } from "react";
import { SmallGenericError } from "metabase/common/components/ErrorPages";
+import { FrontendErrorsApi } from "metabase/services";
interface ErrorBoundaryProps extends PropsWithChildren {
onError?: (errorInfo: ErrorInfo) => void;
@@ -35,6 +36,7 @@ class ErrorBoundaryInner extends Component<
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error(error, errorInfo);
+ FrontendErrorsApi.report({ type: "component-crash" }).catch(() => {});
// if we don't provide a specific onError action, the component will display a generic error message
if (this.props.onError) {
this.props.onError(errorInfo);
diff --git a/frontend/src/metabase/palette/components/Palette.unit.spec.tsx b/frontend/src/metabase/palette/components/Palette.unit.spec.tsx
index 79e5523ab79..797a406cd1d 100644
--- a/frontend/src/metabase/palette/components/Palette.unit.spec.tsx
+++ b/frontend/src/metabase/palette/components/Palette.unit.spec.tsx
@@ -76,19 +76,16 @@ describe("command palette", () => {
await userEvent.type(input, "dark mode");
await userEvent.click(await screen.findByText("Toggle dark/light mode"));
- expect(
- await fetchMock.callHistory
- .lastCall(/\/api\/setting\/color-scheme/)
- ?.request?.json(),
- ).toEqual({ value: "dark" });
+ const calls = () =>
+ fetchMock.callHistory.calls(/\/api\/setting\/color-scheme/);
+
+ expect(await calls().at(-1)?.request?.json()).toEqual({ value: "dark" });
await userEvent.click(await screen.findByText("Toggle dark/light mode"));
- expect(
- await fetchMock.callHistory
- .lastCall(/\/api\/setting\/color-scheme/)
- ?.request?.json(),
- ).toEqual({ value: "auto" });
+ expect(await calls().at(-1)?.request?.json()).toEqual({
+ value: "auto",
+ });
});
it("should preserve user navigation selection when search results load", async () => {
diff --git a/frontend/src/metabase/services.js b/frontend/src/metabase/services.js
index 6618626845d..f4c4de00396 100644
--- a/frontend/src/metabase/services.js
+++ b/frontend/src/metabase/services.js
@@ -367,6 +367,10 @@ export const UtilApi = {
},
};
+export const FrontendErrorsApi = {
+ report: POST("/api/frontend-errors"),
+};
+
export const ActionsApi = {
execute: POST("/api/action/:id/execute"),
prefetchValues: GET("/api/action/:id/execute"),
diff --git a/frontend/src/metabase/visualizations/components/ChartRenderingErrorBoundary/ChartRenderingErrorBoundary.tsx b/frontend/src/metabase/visualizations/components/ChartRenderingErrorBoundary/ChartRenderingErrorBoundary.tsx
index 3a637cd43c9..bede81bd555 100644
--- a/frontend/src/metabase/visualizations/components/ChartRenderingErrorBoundary/ChartRenderingErrorBoundary.tsx
+++ b/frontend/src/metabase/visualizations/components/ChartRenderingErrorBoundary/ChartRenderingErrorBoundary.tsx
@@ -1,5 +1,7 @@
import { Component } from "react";
+import { FrontendErrorsApi } from "metabase/services";
+
interface ChartRenderingErrorBoundaryProps {
onRenderError: (errorMessage: string) => void;
children: React.ReactNode;
@@ -11,6 +13,7 @@ export class ChartRenderingErrorBoundary extends Component<ChartRenderingErrorBo
}
componentDidCatch(error: any) {
+ FrontendErrorsApi.report({ type: "chart-render-error" }).catch(() => {});
this.props.onRenderError(error.message || error);
}
diff --git a/frontend/test/jest-setup-env.js b/frontend/test/jest-setup-env.js
index 847347361ae..1888a25675b 100644
--- a/frontend/test/jest-setup-env.js
+++ b/frontend/test/jest-setup-env.js
@@ -11,6 +11,9 @@ Object.assign(navigator, {
beforeEach(() => {
fetchMock.mockGlobal();
+ // Always mock the error reporting endpoint — it's fire-and-forget
+ // and should never affect test behavior
+ fetchMock.post("path:/api/frontend-errors", 200);
});
afterEach(async () => {
diff --git a/src/metabase/analytics/prometheus.clj b/src/metabase/analytics/prometheus.clj
index aa0d8743fdf..a7c0a89852c 100644
--- a/src/metabase/analytics/prometheus.clj
+++ b/src/metabase/analytics/prometheus.clj
@@ -593,7 +593,20 @@
{:description "Full agent loop duration (ms)"
:labels [:profile-id]
;; 100ms -> 10 minutes
- :buckets [100 500 1000 5000 10000 30000 60000 120000 300000 600000]})])
+ :buckets [100 500 1000 5000 10000 30000 60000 120000 300000 600000]})
+
+ ;; release dashboard metrics
+ (prometheus/counter :metabase-sync/failures
+ {:description "Number of sync operation failures."
+ :labels [:driver]})
+ (prometheus/counter :metabase-api/unhandled-errors
+ {:description "Number of unhandled API errors (500s without explicit status code)."})
+ (prometheus/counter :metabase-frontend/errors
+ {:description "Number of frontend errors reported by the browser."
+ :labels [:type]})
+ (prometheus/counter :metabase-export/errors
+ {:description "Number of errors during data export."
+ :labels [:format]})])
(defn- quartz-collectors
[]
diff --git a/src/metabase/api_routes/routes.clj b/src/metabase/api_routes/routes.clj
index 52ab6067d90..a7f5df1b133 100644
--- a/src/metabase/api_routes/routes.clj
+++ b/src/metabase/api_routes/routes.clj
@@ -24,6 +24,7 @@
[metabase.documents.api]
[metabase.eid-translation.api]
[metabase.embedding-rest.api]
+ [metabase.frontend-errors.api]
[metabase.geojson.api]
[metabase.glossary.api]
[metabase.indexed-entities.api]
@@ -81,6 +82,7 @@
metabase.data-studio.api/keep-me
metabase.documents.api/keep-me
metabase.eid-translation.api/keep-me
+ metabase.frontend-errors.api/keep-me
metabase.geojson.api/keep-me
metabase.glossary.api/keep-me
metabase.indexed-entities.api/keep-me
@@ -170,6 +172,7 @@
"/email" metabase.channel.api/email-routes
"/embed" (+message-only-exceptions metabase.embedding-rest.api/embedding-routes)
"/field" (+auth metabase.warehouse-schema-rest.api/field-routes)
+ "/frontend-errors" 'metabase.frontend-errors.api
"/geojson" 'metabase.geojson.api
"/glossary" (+auth 'metabase.glossary.api)
"/google" (+auth metabase.sso.api/google-auth-routes)
diff --git a/src/metabase/frontend_errors/api.clj b/src/metabase/frontend_errors/api.clj
new file mode 100644
index 00000000000..c755ec9557c
--- /dev/null
+++ b/src/metabase/frontend_errors/api.clj
@@ -0,0 +1,23 @@
+(ns metabase.frontend-errors.api
+ (:require
+ [metabase.analytics.prometheus :as prometheus]
+ [metabase.api.common :as api]
+ [metabase.api.macros :as api.macros]
+ [metabase.util.malli.registry :as mr]))
+
+(mr/def ::frontend-error-type
+ [:enum "component-crash" "chart-render-error"])
+
+#_{:clj-kondo/ignore [:metabase/validate-defendpoint-has-response-schema]}
+(api.macros/defendpoint :post "/"
+ "Endpoint for the frontend to report errors. Increments a Prometheus counter
+ with the given `type` label."
+ [_route-params
+ _query-params
+ {:keys [type]} :- [:map [:type ::frontend-error-type]]]
+ (prometheus/inc! :metabase-frontend/errors {:type type})
+ api/generic-204-no-content)
+
+;;; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+;;; !! Endpoints in this namespace do not currently require auth! Keep this in mind when adding new ones. !!
+;;; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
diff --git a/src/metabase/query_processor/streaming.clj b/src/metabase/query_processor/streaming.clj
index 217965b98ab..727f02ed9a3 100644
--- a/src/metabase/query_processor/streaming.clj
+++ b/src/metabase/query_processor/streaming.clj
@@ -2,6 +2,7 @@
(:refer-clojure :exclude [every? some mapv not-empty])
(:require
[clojure.string :as str]
+ [metabase.analytics.prometheus :as prometheus]
[metabase.lib.core :as lib]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.models.visualization-settings :as mb.viz]
@@ -257,6 +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)})
(streaming-response/write-error! os result export-format (status-code result))))))))))
(defn transforming-query-response
diff --git a/src/metabase/server/middleware/exceptions.clj b/src/metabase/server/middleware/exceptions.clj
index dafc0c5d30d..6c5e1ea5158 100644
--- a/src/metabase/server/middleware/exceptions.clj
+++ b/src/metabase/server/middleware/exceptions.clj
@@ -3,6 +3,7 @@
(:require
[clojure.java.jdbc :as jdbc]
[clojure.string :as str]
+ [metabase.analytics.prometheus :as prometheus]
[metabase.server.middleware.security :as mw.security]
[metabase.server.settings :as server.settings]
[metabase.util.i18n :refer [tru]]
@@ -50,6 +51,8 @@
(Throwable->map e)
{:message (.getMessage e)}
other-info))]
+ (when (nil? status-code)
+ (prometheus/inc! :metabase-api/unhandled-errors))
{:status (or status-code 500)
:headers (mw.security/security-headers)
:body body}))
diff --git a/src/metabase/sync/util.clj b/src/metabase/sync/util.clj
index ca51d85beb7..0275b0c7d89 100644
--- a/src/metabase/sync/util.clj
+++ b/src/metabase/sync/util.clj
@@ -6,6 +6,7 @@
[clojure.string :as str]
[java-time.api :as t]
[medley.core :as m]
+ [metabase.analytics.prometheus :as prometheus]
[metabase.driver :as driver]
[metabase.driver.util :as driver.u]
[metabase.events.core :as events]
@@ -270,15 +271,19 @@
{:run_type run-type
:entity_type :database
:entity_id (u/the-id database)})
- ((with-duplicate-ops-prevented
- operation database
- (with-sync-events
- operation database
- (with-start-and-finish-logging
- message
- (with-db-logging-disabled
- (sync-in-context database
- (partial do-with-error-handling (format "Error in sync step %s" message) f)))))))))))
+ (let [sync-fn (with-duplicate-ops-prevented
+ operation database
+ (with-sync-events
+ operation database
+ (with-start-and-finish-logging
+ message
+ (with-db-logging-disabled
+ (sync-in-context database
+ (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))}))
+ result)))))
(defmacro sync-operation
"Perform the operations in `body` as a sync operation, which wraps the code in several special macros that do things
diff --git a/test/metabase/frontend_errors/api_test.clj b/test/metabase/frontend_errors/api_test.clj
new file mode 100644
index 00000000000..6c163fe985d
--- /dev/null
+++ b/test/metabase/frontend_errors/api_test.clj
@@ -0,0 +1,22 @@
+(ns metabase.frontend-errors.api-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.test :as mt]))
+
+(deftest post-frontend-errors-test
+ (testing "POST /api/frontend-errors increments the counter for component-crash and returns 204"
+ (mt/with-prometheus-system! [_ system]
+ (let [initial (mt/metric-value system :metabase-frontend/errors {:type "component-crash"})]
+ (is (nil? (mt/user-http-request :rasta :post 204 "frontend-errors" {:type "component-crash"})))
+ (is (< initial (mt/metric-value system :metabase-frontend/errors {:type "component-crash"}))))))
+
+ (testing "POST /api/frontend-errors with type=chart-render-error tracks separately"
+ (mt/with-prometheus-system! [_ system]
+ (let [initial (mt/metric-value system :metabase-frontend/errors {:type "chart-render-error"})]
+ (mt/user-http-request :rasta :post 204 "frontend-errors" {:type "chart-render-error"})
+ (is (< initial (mt/metric-value system :metabase-frontend/errors {:type "chart-render-error"}))))))
+
+ (testing "POST /api/frontend-errors rejects unknown type values"
+ (is (= {:errors {:type "enum of component-crash, chart-render-error"}}
+ (select-keys (mt/user-http-request :rasta :post 400 "frontend-errors" {:type "bogus"})
+ [:errors])))))
diff --git a/test/metabase/server/middleware/exceptions_test.clj b/test/metabase/server/middleware/exceptions_test.clj
index dcbb5f5c3c2..166918b5d60 100644
--- a/test/metabase/server/middleware/exceptions_test.clj
+++ b/test/metabase/server/middleware/exceptions_test.clj
@@ -193,3 +193,16 @@
(fn [e] (reset! captured-exception e)))
(is (= test-exception @captured-exception)
"Exception should be routed to raise handler"))))
+
+(deftest unhandled-error-increments-prometheus-counter-test
+ (testing "An unhandled exception (no status-code in ex-data) increments :metabase-api/unhandled-errors"
+ (mt/with-prometheus-system! [_ system]
+ (let [initial (mt/metric-value system :metabase-api/unhandled-errors)]
+ (mw.exceptions/api-exception-response (Exception. "boom"))
+ (is (< initial (mt/metric-value system :metabase-api/unhandled-errors))))))
+
+ (testing "An exception with an explicit status-code does NOT increment the counter"
+ (mt/with-prometheus-system! [_ system]
+ (let [initial (mt/metric-value system :metabase-api/unhandled-errors)]
+ (mw.exceptions/api-exception-response (ex-info "not found" {:status-code 404}))
+ (is (= initial (mt/metric-value system :metabase-api/unhandled-errors)))))))
diff --git a/test/metabase/sync/util_test.clj b/test/metabase/sync/util_test.clj
index 4aaea4abec6..affb6139da6 100644
--- a/test/metabase/sync/util_test.clj
+++ b/test/metabase/sync/util_test.clj
@@ -363,3 +363,13 @@
(is (= "complete" (:initial_sync_status (get-inactive-table)))))))
(a/close! syncing-chan)
(a/close! completed-chan))))))
+
+(deftest sync-failure-increments-prometheus-counter-test
+ (testing "When a sync operation fails, the :metabase-sync/failures counter is incremented"
+ (mt/with-prometheus-system! [_ system]
+ (mt/with-temp [:model/Database db {:engine :h2}]
+ (let [initial (mt/metric-value system :metabase-sync/failures {:driver "h2"})]
+ (sync-util/do-sync-operation
+ :sync db "test sync failure"
+ (fn [] (throw (Exception. "sync boom"))))
+ (is (< initial (mt/metric-value system :metabase-sync/failures {:driver "h2"}))))))))