Track and analyze each middleware operation of metabase query_processor

The query processor of metabase has many steps. To analyze the middleware in the default-middleware, you can write a simple middleware for tracking. The specific code is as follows

(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) ))


Modify the default-middleware as follows

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

The output is as follows:

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"]

Although query, rff and context can be tracked, we generally only need to pay attention to the change of query.

Guess you like

Origin blog.csdn.net/weixin_40455124/article/details/114332760