Skip to content

Commit

Permalink
馃 backported "Allow disabling health check logging via env var" (#42423)
Browse files Browse the repository at this point in the history
Co-authored-by: Noah Moss <[email protected]>
  • Loading branch information
metabase-bot[bot] and noahmoss committed May 13, 2024
1 parent 708926f commit 9e8fc83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/metabase/server/middleware/log.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
[metabase.db.connection :as mdb.connection]
[metabase.driver.sql-jdbc.execute.diagnostic
:as sql-jdbc.execute.diagnostic]
[metabase.models.setting :refer [defsetting]]
[metabase.server :as server]
[metabase.server.request.util :as request.u]
[metabase.util :as u]
[metabase.util.i18n :refer [trs]]
[metabase.util.i18n :as i18n :refer [deferred-tru trs]]
[metabase.util.log :as log]
[toucan2.core :as t2])
(:import
Expand Down Expand Up @@ -190,11 +191,24 @@
;; Actual middleware. Determines whether request should be logged, and, if so, creates the info dictionary and hands
;; off to functions above.

(defsetting health-check-logging-enabled
(deferred-tru "Whether to log health check requests from session middleware.")
:type :boolean
:default true
:visibility :internal
:export? false)

(defn- logging-disabled-uris
"The set of URIs that should not be logged."
[]
(cond-> #{"/api/util/logs"}
(not (health-check-logging-enabled)) (conj "/api/health")))

(defn- should-log-request? [{:keys [uri], :as request}]
;; don't log calls to /health or /util/logs because they clutter up the logs (especially the window in admin) with
;; useless lines
(and (request.u/api-call? request)
(not (#{"/api/util/logs"} uri))))
(not ((logging-disabled-uris) uri))))

(defn log-api-call
"Logs info about request such as status code, number of DB calls, and time taken to complete."
Expand Down
9 changes: 9 additions & 0 deletions test/metabase/server/middleware/log_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[clojure.test :refer :all]
[metabase.server.middleware.log :as mw.log]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]))

(use-fixtures :once (fixtures/initialize :db))
Expand All @@ -20,3 +21,11 @@
(testing `log/stats
(is (re= #"^App DB connections:.*"
(#'mw.log/stats (fn [] {:info true}))))))

(deftest should-log-request?-test
(testing "Health check logging can be disabled via env var"
(mt/with-temp-env-var-value! [mb-health-check-logging-enabled true]
(is (#'mw.log/should-log-request? {:uri "/api/health"})))

(mt/with-temp-env-var-value! [mb-health-check-logging-enabled false]
(is (not (#'mw.log/should-log-request? {:uri "/api/health"}))))))

0 comments on commit 9e8fc83

Please sign in to comment.