vitess.io/vitess@v0.16.2/go/vt/sqlparser/truncate_query.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 sqlparser
    18  
    19  import (
    20  	"github.com/spf13/pflag"
    21  
    22  	"vitess.io/vitess/go/vt/servenv"
    23  )
    24  
    25  var (
    26  	// truncateUILen truncate queries in debug UIs to the given length. 0 means unlimited.
    27  	truncateUILen = 512
    28  
    29  	// truncateErrLen truncate queries in error logs to the given length. 0 means unlimited.
    30  	truncateErrLen = 0
    31  )
    32  
    33  func registerQueryTruncationFlags(fs *pflag.FlagSet) {
    34  	fs.IntVar(&truncateUILen, "sql-max-length-ui", truncateUILen, "truncate queries in debug UIs to the given length (default 512)")
    35  	fs.IntVar(&truncateErrLen, "sql-max-length-errors", truncateErrLen, "truncate queries in error logs to the given length (default unlimited)")
    36  }
    37  
    38  func init() {
    39  	for _, cmd := range []string{
    40  		"vtgate",
    41  		"vttablet",
    42  		"vtcombo",
    43  		"vtctld",
    44  		"vtctl",
    45  		"vtexplain",
    46  		"vtbackup",
    47  		"vttestserver",
    48  		"vtbench",
    49  	} {
    50  		servenv.OnParseFor(cmd, registerQueryTruncationFlags)
    51  	}
    52  }
    53  
    54  // GetTruncateErrLen is a function used to read the value of truncateErrLen
    55  func GetTruncateErrLen() int {
    56  	return truncateErrLen
    57  }
    58  
    59  // SetTruncateErrLen is a function used to override the value of truncateErrLen
    60  // It is only meant to be used from tests and not from production code.
    61  func SetTruncateErrLen(errLen int) {
    62  	truncateErrLen = errLen
    63  }
    64  
    65  func truncateQuery(query string, max int) string {
    66  	sql, comments := SplitMarginComments(query)
    67  
    68  	if max == 0 || len(sql) <= max {
    69  		return comments.Leading + sql + comments.Trailing
    70  	}
    71  
    72  	return comments.Leading + sql[:max-12] + " [TRUNCATED]" + comments.Trailing
    73  }
    74  
    75  // TruncateForUI is used when displaying queries on various Vitess status pages
    76  // to keep the pages small enough to load and render properly
    77  func TruncateForUI(query string) string {
    78  	return truncateQuery(query, truncateUILen)
    79  }
    80  
    81  // TruncateForLog is used when displaying queries as part of error logs
    82  // to avoid overwhelming logging systems with potentially long queries and
    83  // bind value data.
    84  func TruncateForLog(query string) string {
    85  	return truncateQuery(query, truncateErrLen)
    86  }