跟踪分析metabase query_processor的每个middleware操作

metabase的query processor有很多步,为分析default-middleware中这些middleware ,可以写一个简单middleware用于跟踪,具体代码如下

(ns metabase.query-processor.middleware.querylog
  "Middleware for logging a query before it is processed.
   (Various other middleware functions log the query as well in different stages.)"
  (:require [clojure.tools.logging :as log]
            [metabase.util :as u]
            [clojure.pprint :refer [pprint]]))

(defn pprint-to-str
  {
    
    :style/indent 1}
  [x]
     (with-open [w (java.io.StringWriter.)]
       (pprint x w)
       (str w))) 

; (defn- log-query* [query]
;  (u/prog1 query
;    (log/info (u/format-color 'blue "\nQuery (before preprocessing): %s\n%s"  (u/pprint-to-str query)))))

(defn- log-query* [query]
  (u/prog1 query
    (log/info (u/format-color 'blue "\nQuery  :%s"  (pprint-to-str query)))))

(defn log-query
  "Middleware that logs the query that will be ran."
  [qp]
   (fn [query rff context]
   (    qp (log-query*  query)  (log-query*  rff) context) ))


修改default-middleware如下

  	#'mbql-to-native/mbql->native ;; call driver's sql execution
  	#'querylog/log-query ;; log before
  	#'check-features/check-features

输出如下:

2021-03-03 14:24:43,684 INFO middleware.permissions :: Checking query permissions. Current user perms set = %s #{"/collection/2/" "/db/3/" "/collection/1/" "/" "/collection/3/" "/collection/root/" "/db/1/"}
2021-03-03 14:24:43,799 INFO middleware.querylog :: 
Query  :{
    
    :type :native,
 :native {
    
    :query "select * from activity where user_id=?", :params ["1"]},
 :database 3,
 :middleware {
    
    :js-int-to-string? true, :add-default-userland-constraints? true},
 :info
 {
    
    :executed-by 1,
  :context :ad-hoc,
  :nested? false,
  :query-hash [-75, -51, -33, -57, -53, 6, -74, 110, -126, 83, -10, -23, -20, 13, -91, -46, 8, -15, 106, 112, 48, -78, -115, 68, -44, -125, -29, -17, -118, -10, 55, 94]},
 :constraints {
    
    :max-results 10000, :max-results-bare-rows 2000},
 :user-parameters [{
    
    :type :category, :target [:variable [:template-tag "current_user_id"]], :value "1"}],
 :query nil}

2021-03-03 14:24:43,811 INFO middleware.querylog :: 
Query  :#object[metabase.query_processor.middleware.limit$limit$fn__52796$fn__52797 0x6fe95fd7 "metabase.query_processor.middleware.limit$limit$fn__52796$fn__52797@6fe95fd7"]

虽然可以跟踪query,rff及context,但我们一般只需要关注query的变化。

猜你喜欢

转载自blog.csdn.net/weixin_40455124/article/details/114332760