Skip to content

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

log attribution (#68906)

dpsutton604a1d430110
diff --git a/bin/build/src/build/uberjar.clj b/bin/build/src/build/uberjar.clj
index f381b31308f..0d7f2fe0a5b 100644
--- a/bin/build/src/build/uberjar.clj
+++ b/bin/build/src/build/uberjar.clj
@@ -125,6 +125,9 @@
(defn- copy-resources! [basis]
(u/step "Copy resources"
+ ;; put module config file on classpath for log team attribution
+ (b/copy-file {:src ".clj-kondo/config/modules/config.edn"
+ :target "resources/metabase/config/modules.edn"})
(doseq [path (all-paths basis)]
(u/step (format "Copy resources from %s" path)
(b/copy-dir {:target-dir class-dir
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj b/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
index b8fb0b070db..930945a9d65 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
@@ -23,24 +23,12 @@
(set! *warn-on-reflection* true)
-(defn- running-from-jar?
- "Returns true iff we are running from a jar.
-
- .getResource will return a java.net.URL, and those start with \"jar:\" if and only if the app is running from a jar.
-
- More info: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html"
- []
- (= "jar" (.. (Thread/currentThread)
- getContextClassLoader
- (getResource ".keep-me")
- getProtocol)))
-
(defn- get-jar-path
"Returns the path to the currently running jar file.
More info: https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file"
[]
- (assert (running-from-jar?) "Can only get-jar-path when running from a jar.")
+ (assert (config/jar?) "Can only get-jar-path when running from a jar.")
(-> (class {})
(.getProtectionDomain)
(.getCodeSource)
@@ -210,7 +198,7 @@
(let [ia-dir (instance-analytics-plugin-dir plugins-dir)]
(when (fs/exists? (u.files/relative-path ia-dir))
(fs/delete-tree (u.files/relative-path ia-dir)))
- (if (running-from-jar?)
+ (if (config/jar?)
(let [path-to-jar (get-jar-path)]
(log/info "The app is running from a jar, starting copy...")
(log/info (str "Copying " path-to-jar "::" jar-resource-path " -> " plugins-dir))
diff --git a/src/metabase/config/core.clj b/src/metabase/config/core.clj
index 9eac6945112..5408f5fb42f 100644
--- a/src/metabase/config/core.clj
+++ b/src/metabase/config/core.clj
@@ -61,6 +61,7 @@
:max-session-age "20160" ; session length in minutes (14 days)
:mb-colorize-logs (str (not is-windows?)) ; since PowerShell and cmd.exe don't support ANSI color escape codes or emoji,
:mb-emoji-in-logs (str (not is-windows?)) ; disable them by default when running on Windows. Otherwise they're enabled
+ :mb-log-team-attribution "false"
:mb-qp-cache-backend "db"
:mb-jetty-async-response-timeout (str (* 10 60 1000))}) ; 10m
@@ -105,6 +106,18 @@
;; This allows us to run Cypress E2E tests with the production-like behavior.
(def ^Boolean is-e2e? "Are we running Cypress E2E tests against the production-ready code?" (= :e2e run-mode))
+(defn jar?
+ "Returns true iff we are running from a jar.
+
+ .getResource will return a java.net.URL, and those start with \"jar:\" if and only if the app is running from a jar.
+
+ More info: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html"
+ []
+ (= "jar" (.. (Thread/currentThread)
+ getContextClassLoader
+ (getResource ".keep-me")
+ getProtocol)))
+
;;; Version stuff
(defn- version-info-from-properties-file []
diff --git a/src/metabase/util/log.clj b/src/metabase/util/log.clj
index f73e4b57586..9946a4d70a1 100644
--- a/src/metabase/util/log.clj
+++ b/src/metabase/util/log.clj
@@ -3,6 +3,8 @@
The interface is the same as [[clojure.tools.logging]]."
(:require
+ [clojure.edn :as edn]
+ [clojure.java.io :as io]
[clojure.pprint :as pprint]
[clojure.string :as str]
^{:clj-kondo/ignore [:discouraged-namespace]}
@@ -103,35 +105,79 @@
`(with-thread-context-fn ~context-map
(fn [] ~@body)))
+(let [config (-> (if (config/jar?)
+ (io/resource "metabase/config/modules.edn")
+ (io/file ".clj-kondo/config/modules/config.edn"))
+ slurp edn/read-string :metabase/modules)
+ first-segment (fn first-segment [ns-sym]
+ (-> (str/split (name ns-sym) #"\.") second))
+ chop (fn chop [ns-sym]
+ (if (str/starts-with? (name ns-sym) "metabase-enterprise")
+ (symbol "enterprise" (first-segment ns-sym))
+ (symbol (first-segment ns-sym))))]
+ (defn ns->team*
+ "Chops the namespace symbol to look up which team owns this namespace in the module config. Assumes that the first
+ segment of the namespace is a module. Should be memoized for speed."
+ [ns-sym]
+ (if ('#{metabase.server.middleware.log} ns-sym)
+ ::skip
+ (-> ns-sym chop config :team))))
+
+(let [attribution (if (config/config-bool :mb-log-team-attribution)
+ (memoize ns->team*)
+ (constantly nil))]
+ (defn ns->team
+ "Returns a string of the team for a namespace symbol, or nil. We skip a few chatty namespaces like
+ `metabase.server.middleware.log` which logs requests."
+ [logger-ns]
+ (let [team (attribution logger-ns)]
+ (when (and team (not (identical? team ::skip)))
+ team))))
+
(defn- tools-logp
"Macro helper for [[logp]] in CLJ."
[logger-ns level x more]
- `(let [logger# (clojure.tools.logging.impl/get-logger clojure.tools.logging/*logger-factory* ~logger-ns)]
+ `(let [logger-ns# ~logger-ns
+ logger# (clojure.tools.logging.impl/get-logger clojure.tools.logging/*logger-factory* ~logger-ns)]
(when (clojure.tools.logging.impl/enabled? logger# ~level)
- (let [x# ~x]
- (if (instance? Throwable x#)
- (let [d# (get-exception-data x#)]
- (with-thread-context d#
- (clojure.tools.logging/log* logger# ~level x# ~(if (nil? more)
- ""
- `(print-str ~@more)))))
- (clojure.tools.logging/log* logger# ~level nil (print-str x# ~@more)))))))
+ (let [team# (ns->team (ns-name logger-ns#))]
+ (try
+ (when team#
+ (ThreadContext/put "mb-team" team#)) ;; <---- new
+ (let [x# ~x]
+ (if (instance? Throwable x#)
+ (let [d# (get-exception-data x#)]
+ (with-thread-context d#
+ (clojure.tools.logging/log* logger# ~level x# ~(if (nil? more)
+ ""
+ `(print-str ~@more)))))
+ (clojure.tools.logging/log* logger# ~level nil (print-str x# ~@more))))
+ (finally (when team# (ThreadContext/remove "mb-team")))))))) ;; <------ new cleanup
(defn- tools-logf
"Macro helper for [[logf]] in CLJ."
[logger-ns level x more]
(if (and (instance? String x) (nil? more))
;; Simple case: just a String and no args.
- `(let [logger# (clojure.tools.logging.impl/get-logger clojure.tools.logging/*logger-factory* ~logger-ns)]
+ `(let [logger-ns# ~logger-ns
+ logger# (clojure.tools.logging.impl/get-logger clojure.tools.logging/*logger-factory* logger-ns#)]
(when (clojure.tools.logging.impl/enabled? logger# ~level)
- (clojure.tools.logging/log* logger# ~level nil ~x)))
- ;; Full case, with formatting.
- `(let [logger# (clojure.tools.logging.impl/get-logger clojure.tools.logging/*logger-factory* ~logger-ns)]
+ (let [team# (ns->team (ns-name logger-ns#))]
+ (try
+ (when team# (ThreadContext/put "mb-team" team#))
+ (clojure.tools.logging/log* logger# ~level nil ~x)
+ (finally (when team# (ThreadContext/remove "mb-team"))))))) ;; Full case, with formatting.
+ `(let [logger-ns# ~logger-ns
+ logger# (clojure.tools.logging.impl/get-logger clojure.tools.logging/*logger-factory* logger-ns#)]
(when (clojure.tools.logging.impl/enabled? logger# ~level)
- (let [x# ~x]
- (if (instance? Throwable x#)
- (clojure.tools.logging/log* logger# ~level x# (format ~@more))
- (clojure.tools.logging/log* logger# ~level nil (format x# ~@more))))))))
+ (let [x# ~x
+ team# (ns->team (ns-name logger-ns#))]
+ (try
+ (when team# (ThreadContext/put "mb-team" team#))
+ (if (instance? Throwable x#)
+ (clojure.tools.logging/log* logger# ~level x# (format ~@more))
+ (clojure.tools.logging/log* logger# ~level nil (format x# ~@more)))
+ (finally (when team# (ThreadContext/remove "mb-team")))))))))
;;; ------------------------------------------------ Internal macros -------------------------------------------------
(defmacro logp