Skip to content

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

Improve logging around notifications (#70099)

Ngoc Khuat3be69174e6c7
diff --git a/src/metabase/analytics/prometheus.clj b/src/metabase/analytics/prometheus.clj
index d21ea10788e..a7b3e3060ae 100644
--- a/src/metabase/analytics/prometheus.clj
+++ b/src/metabase/analytics/prometheus.clj
@@ -434,6 +434,15 @@
:labels [:payload-type :channel-type]})
(prometheus/gauge :metabase-notification/concurrent-tasks
{:description "Number of concurrent notification sends."})
+ (prometheus/counter :metabase-notification/template-render
+ {:description "Number of notification template render attempts."
+ :labels [:template-type :channel-type]})
+ (prometheus/counter :metabase-notification/template-create
+ {:description "Number of notification templates created."
+ :labels [:channel-type]})
+ (prometheus/counter :metabase-notification/template-update
+ {:description "Number of notification templates updated."
+ :labels [:channel-type]})
(prometheus/counter :metabase-gsheets/connection-creation-began
{:description "How many times the instance has initiated a Google Sheets connection creation."})
(prometheus/counter :metabase-gsheets/connection-creation-error
diff --git a/src/metabase/channel/impl/email.clj b/src/metabase/channel/impl/email.clj
index 06c64759578..c23107fbf06 100644
--- a/src/metabase/channel/impl/email.clj
+++ b/src/metabase/channel/impl/email.clj
@@ -3,6 +3,7 @@
[clojure.string :as str]
[hiccup.core :refer [html]]
[medley.core :as m]
+ [metabase.analytics.prometheus :as prometheus]
[metabase.channel.core :as channel]
[metabase.channel.email :as email]
[metabase.channel.email.logo :as email.logo]
@@ -30,6 +31,11 @@
(set! *warn-on-reflection* true)
+(defmethod prometheus/known-labels :metabase-notification/template-render [_]
+ (for [template-type [:email/handlebars-text :email/handlebars-resource]]
+ {:template-type template-type
+ :channel-type :channel/email}))
+
(def ^:private EmailMessage
[:map
[:subject :string]
@@ -96,16 +102,22 @@
(defn- render-body
[{:keys [details] :as _template} payload]
- (case (keyword (:type details))
- :email/handlebars-resource
- (handlebars/render (:path details) payload)
+ (let [template-type (keyword (:type details))]
+ (prometheus/inc! :metabase-notification/template-render
+ {:template-type template-type
+ :channel-type :channel/email})
+ (case template-type
+ :email/handlebars-resource
+ (handlebars/render (:path details) payload)
- :email/handlebars-text
- (handlebars/render-string (:body details) payload)
+ :email/handlebars-text
+ (do
+ (log/debugf "Rendering user-provided template body=%s" (pr-str (:body details)))
+ (handlebars/render-string (:body details) payload))
- (do
- (log/warnf "Unknown email template type: %s" (:type details))
- nil)))
+ (do
+ (log/warnf "Unknown email template type: %s" (:type details))
+ nil))))
(defn- render-message-body
[template message-context attachments]
diff --git a/src/metabase/channel/models/channel.clj b/src/metabase/channel/models/channel.clj
index d9a2fa48a89..6af32a35752 100644
--- a/src/metabase/channel/models/channel.clj
+++ b/src/metabase/channel/models/channel.clj
@@ -1,10 +1,13 @@
(ns ^{:added "0.51.0"} metabase.channel.models.channel
(:require
[malli.core :as mc]
+ [metabase.analytics.prometheus :as prometheus]
+ [metabase.api.common :as api]
[metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
[metabase.permissions.core :as perms]
[metabase.util :as u]
+ [metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
@@ -113,6 +116,26 @@
[channel-template]
(mu/validate-throw ::ChannelTemplate channel-template))
+(defn- user-provided-template?
+ "Returns true if the template details represent a user-provided inline template (handlebars-text)
+ as opposed to a built-in resource template."
+ [details]
+ (= :email/handlebars-text (keyword (:type details))))
+
+(defn- log-template-change!
+ "Log template creation or update with relevant details for observability."
+ [action {:keys [channel_type details] :as _instance}]
+ (let [template-type (keyword (:type details))]
+ (if (user-provided-template? details)
+ (log/infof "ChannelTemplate %s: channel_type=%s template_type=%s user_id=%s body=%s"
+ (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})))
+
(t2/define-before-insert :model/ChannelTemplate
[instance]
(check-valid-channel-template instance)
@@ -123,6 +146,22 @@
(check-valid-channel-template instance)
instance)
+(t2/define-after-insert :model/ChannelTemplate
+ [instance]
+ (log-template-change! :create instance)
+ instance)
+
+(t2/define-after-update :model/ChannelTemplate
+ [instance]
+ (log-template-change! :update instance)
+ instance)
+
+;; 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 mi/can-write? :model/ChannelTemplate
[& _]
(or (mi/superuser?)
diff --git a/test/metabase/channel/impl/email_test.clj b/test/metabase/channel/impl/email_test.clj
index 46507ec3d35..a9f7c057242 100644
--- a/test/metabase/channel/impl/email_test.clj
+++ b/test/metabase/channel/impl/email_test.clj
@@ -93,3 +93,39 @@
(is (= 1 (count result)))
(is (string? rendered-content))
(is (str/includes? rendered-content "http://example.com/dashboard/42?state=CA&state=NY&state=NJ#scrollTo=456")))))))))
+
+(deftest render-body-prometheus-metric-test
+ (testing "rendering a user-provided template increments the template-render counter"
+ (mt/with-prometheus-system! [_ system]
+ (let [template {:details {:type :email/handlebars-text
+ :subject "Test"
+ :body "Hello {{name}}"}}]
+ (#'email.impl/render-body template {:name "World"})
+ (is (= 1.0 (mt/metric-value system :metabase-notification/template-render
+ {:template-type :email/handlebars-text
+ :channel-type :channel/email}))))))
+
+ (testing "rendering a resource template also increments the counter with the correct label"
+ (mt/with-prometheus-system! [_ system]
+ (let [template {:details {:type :email/handlebars-resource
+ :subject "Test"
+ :path "metabase/channel/email/notification_card.hbs"}}]
+ ;; render-body will throw if the payload doesn't match the template, but the metric
+ ;; fires before the render, so we just need to not blow up
+ (try (#'email.impl/render-body template {})
+ (catch Exception _))
+ (is (= 1.0 (mt/metric-value system :metabase-notification/template-render
+ {:template-type :email/handlebars-resource
+ :channel-type :channel/email})))))))
+
+(deftest render-body-logging-test
+ (testing "rendering a user-provided template logs the template body at debug level"
+ (mt/with-log-messages-for-level [messages :debug]
+ (let [template {:details {:type :email/handlebars-text
+ :subject "Test"
+ :body "Hello {{name}}"}}]
+ (#'email.impl/render-body template {:name "World"})
+ (is (some (fn [{:keys [message]}]
+ (and (re-find #"Rendering user-provided template" message)
+ (re-find #"Hello" message)))
+ (messages)))))))
diff --git a/test/metabase/channel/models/channel_test.clj b/test/metabase/channel/models/channel_test.clj
index 807683e3916..c7fc8ee5b14 100644
--- a/test/metabase/channel/models/channel_test.clj
+++ b/test/metabase/channel/models/channel_test.clj
@@ -1,6 +1,7 @@
(ns metabase.channel.models.channel-test
(:require
[clojure.test :refer :all]
+ [metabase.channel.models.channel] ;; ensure known-labels are loaded
[metabase.notification.test-util :as notification.tu]
[metabase.test :as mt]
[metabase.util.encryption :as encryption]
@@ -63,3 +64,46 @@
(insert! {:details {:type "email/handlebars-resource"
:subject "Hello {{name}}"
:path nil}}))))))))
+
+(deftest channel-template-create-prometheus-metric-test
+ (testing "creating a ChannelTemplate increments the template-create prometheus counter"
+ (mt/with-prometheus-system! [_ system]
+ (mt/with-model-cleanup [:model/ChannelTemplate]
+ (t2/insert-returning-instance! :model/ChannelTemplate
+ {:channel_type :channel/email
+ :name "Test Template"
+ :details {:type :email/handlebars-text
+ :subject "Hello {{name}}"
+ :body "Welcome {{name}}"}})
+ (is (= 1.0 (mt/metric-value system :metabase-notification/template-create
+ {:channel-type :channel/email})))))))
+
+(deftest channel-template-update-prometheus-metric-test
+ (testing "updating a ChannelTemplate increments the template-update prometheus counter"
+ (mt/with-prometheus-system! [_ system]
+ (mt/with-temp [:model/ChannelTemplate {id :id} {:channel_type :channel/email
+ :name "Test Template"
+ :details {:type :email/handlebars-text
+ :subject "Hello"
+ :body "Original body"}}]
+ (t2/update! :model/ChannelTemplate id {:details {:type :email/handlebars-text
+ :subject "Hello"
+ :body "Updated body"}})
+ (is (= 1.0 (mt/metric-value system :metabase-notification/template-update
+ {:channel-type :channel/email})))))))
+
+(deftest channel-template-create-logging-test
+ (testing "creating a user-provided template logs the template body"
+ (mt/with-log-messages-for-level [messages :info]
+ (mt/with-model-cleanup [:model/ChannelTemplate]
+ (t2/insert-returning-instance! :model/ChannelTemplate
+ {:channel_type :channel/email
+ :name "Test Template"
+ :details {:type :email/handlebars-text
+ :subject "Hello"
+ :body "Secret {{password}}"}})
+ (is (some (fn [{:keys [message]}]
+ (and (re-find #"ChannelTemplate create" message)
+ (re-find #"handlebars-text" message)
+ (re-find #"Secret" message)))
+ (messages)))))))