Skip to content

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

Log propagation for notifications (#62286)

John Swanson86b21a98f031
diff --git a/src/metabase/notification/send.clj b/src/metabase/notification/send.clj
index 92326aac6eb..b9eebcc6445 100644
--- a/src/metabase/notification/send.clj
+++ b/src/metabase/notification/send.clj
@@ -369,7 +369,8 @@
(pos? (queue-size queue))))
(try
(when-let [notification (take-notification-with-timeout! queue 1000)]
- (send-notification-sync! notification))
+ (log/with-restored-context-from-meta notification
+ (send-notification-sync! notification)))
(catch InterruptedException _
(log/warn "Notification worker interrupted, shutting down")
(throw (InterruptedException.)))
@@ -385,7 +386,7 @@
(if-not (.get shutdown-flag)
(do
(ensure-enough-workers!)
- (put-notification! queue notification)
+ (put-notification! queue (log/with-context-meta notification))
::ok)
(do
(log/infof "Rejecting notification with id %d as the workers are being shutdown" (:id notification))
diff --git a/src/metabase/util/log.clj b/src/metabase/util/log.clj
index 6e141affbb4..cd8b9d66007 100644
--- a/src/metabase/util/log.clj
+++ b/src/metabase/util/log.clj
@@ -332,3 +332,17 @@
(with-exception-context ~context-map
~@body))
:cljs ~@body))
+
+(defn with-context-meta
+ "Given a map, returns the map with the `::context` set. Used for propagation of log context across threads."
+ [m]
+ (vary-meta m assoc ::context (->> (ThreadContext/getImmutableContext)
+ (keep (fn [[k v]] (when (str/starts-with? k "mb-")
+ [(str/replace k "mb-" "") v])))
+ (into {}))))
+
+(defmacro with-restored-context-from-meta
+ "Given a map presumably containing metadata from `with-context-meta`, sets the current ThreadContext"
+ [m & body]
+ `(with-context (::context (meta ~m))
+ ~@body))