vitess.io/vitess@v0.16.2/go/vt/vtgate/querylog.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vtgate
    18  
    19  import (
    20  	"net/http"
    21  	"sync"
    22  
    23  	"vitess.io/vitess/go/streamlog"
    24  )
    25  
    26  var (
    27  	// QueryLogHandler is the debug UI path for exposing query logs
    28  	QueryLogHandler = "/debug/querylog"
    29  
    30  	// QueryLogzHandler is the debug UI path for exposing query logs
    31  	QueryLogzHandler = "/debug/querylogz"
    32  
    33  	// QueryzHandler is the debug UI path for exposing query plan stats
    34  	QueryzHandler = "/debug/queryz"
    35  
    36  	// QueryLogger enables streaming logging of queries
    37  	QueryLogger   *streamlog.StreamLogger
    38  	queryLoggerMu sync.Mutex
    39  )
    40  
    41  func SetQueryLogger(logger *streamlog.StreamLogger) {
    42  	queryLoggerMu.Lock()
    43  	defer queryLoggerMu.Unlock()
    44  	QueryLogger = logger
    45  }
    46  
    47  func initQueryLogger(vtg *VTGate) error {
    48  	SetQueryLogger(streamlog.New("VTGate", queryLogBufferSize))
    49  	QueryLogger.ServeLogs(QueryLogHandler, streamlog.GetFormatter(QueryLogger))
    50  
    51  	http.HandleFunc(QueryLogzHandler, func(w http.ResponseWriter, r *http.Request) {
    52  		ch := QueryLogger.Subscribe("querylogz")
    53  		defer QueryLogger.Unsubscribe(ch)
    54  		querylogzHandler(ch, w, r)
    55  	})
    56  
    57  	http.HandleFunc(QueryzHandler, func(w http.ResponseWriter, r *http.Request) {
    58  		queryzHandler(vtg.executor, w, r)
    59  	})
    60  
    61  	if queryLogToFile != "" {
    62  		_, err := QueryLogger.LogToFile(queryLogToFile, streamlog.GetFormatter(QueryLogger))
    63  		if err != nil {
    64  			return err
    65  		}
    66  	}
    67  
    68  	return nil
    69  }