pt-galera-log-explainer: Add pxc operator auto detection

This commit is contained in:
Yoann La Cancellera
2024-01-26 11:52:07 +01:00
parent b15cdf2d68
commit d1459ce0bc
8 changed files with 2387 additions and 8 deletions

View File

@@ -0,0 +1,23 @@
package main
import (
"os/exec"
"github.com/percona/percona-toolkit/src/go/pt-galera-log-explainer/types"
"github.com/rs/zerolog/log"
)
// areOperatorFiles will assume every files are from k8s if one is found
func areOperatorFiles(paths []string) bool {
for _, path := range paths {
cmd := exec.Command(CLI.GrepCmd, "-q", "-a", "-m", "1", "^"+types.OperatorLogPrefix, path)
err := cmd.Run()
if err == nil {
return true
}
log.Debug().Err(err).Str("path", path).Msg("operator detection result")
}
return false
}

View File

@@ -95,7 +95,7 @@ func prepareGrepArgument(regexes types.RegexMap) string {
// I'm not adding pxcoperator map the same way others are used, because they do not have the same formats and same place // I'm not adding pxcoperator map the same way others are used, because they do not have the same formats and same place
// it needs to be put on the front so that it's not 'merged' with the '{"log":"' json prefix // it needs to be put on the front so that it's not 'merged' with the '{"log":"' json prefix
// this is to keep things as close as '^' as possible to keep doing prefix searches // this is to keep things as close as '^' as possible to keep doing prefix searches
grepRegex += "((" + strings.Join(regex.PXCOperatorMap.Compile(), "|") + ")|^{\"log\":\"" grepRegex += "((" + strings.Join(regex.PXCOperatorMap.Compile(), "|") + ")|^" + types.OperatorLogPrefix
regexes.Merge(regex.PXCOperatorMap) regexes.Merge(regex.PXCOperatorMap)
} }
if CLI.Since != nil { if CLI.Since != nil {

View File

@@ -60,13 +60,29 @@ func main() {
) )
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.WarnLevel) zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) log.Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: CLI.NoColor, FormatTimestamp: func(_ interface{}) string { return "" }})
if CLI.Verbosity == types.Debug { if CLI.Verbosity == types.Debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel) zerolog.SetGlobalLevel(zerolog.DebugLevel)
} }
utils.SkipColor = CLI.NoColor utils.SkipColor = CLI.NoColor
var paths []string
switch kongcli.Command() {
case "list <paths>":
paths = CLI.List.Paths
case "ctx <paths>":
paths = CLI.Ctx.Paths
case "conflicts <paths>":
paths = CLI.Conflicts.Paths
}
if !CLI.PxcOperator && areOperatorFiles(paths) {
CLI.PxcOperator = true
log.Info().Msg("Detected logs coming from Percona XtraDB Cluster Operator, enabling --pxc-operator")
}
translate.AssumeIPStable = !CLI.PxcOperator translate.AssumeIPStable = !CLI.PxcOperator
err := kongcli.Run() err := kongcli.Run()

View File

@@ -104,6 +104,12 @@ func TestMain(t *testing.T) {
path: "tests/logs/operator_split/*", path: "tests/logs/operator_split/*",
}, },
{
name: "operator_auto_ambiguous_ips_list_all_no_color",
cmd: []string{"list", "--all", "--no-color"},
path: "tests/logs/operator_ambiguous_ips/*",
},
{ {
name: "conflict_conflicts", name: "conflict_conflicts",
cmd: []string{"conflicts"}, cmd: []string{"conflicts"},

View File

@@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/percona/percona-toolkit/src/go/pt-galera-log-explainer/types"
"github.com/percona/percona-toolkit/src/go/pt-galera-log-explainer/utils" "github.com/percona/percona-toolkit/src/go/pt-galera-log-explainer/utils"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@@ -93,11 +94,9 @@ func NoDatesRegex(skipLeadingCircumflex bool) string {
return "^(?![0-9]{4})" return "^(?![0-9]{4})"
} }
const k8sprefix = `{"log":"`
func SearchDateFromLog(logline string) (time.Time, string, bool) { func SearchDateFromLog(logline string) (time.Time, string, bool) {
if logline[:len(k8sprefix)] == k8sprefix { if logline[:len(types.OperatorLogPrefix)] == types.OperatorLogPrefix {
logline = logline[len(k8sprefix):] logline = logline[len(types.OperatorLogPrefix):]
} }
for _, layout := range DateLayouts { for _, layout := range DateLayouts {
if len(logline) < len(layout) { if len(logline) < len(layout) {

View File

@@ -50,7 +50,7 @@ var PXCOperatorMap = types.RegexMap{
"RegexGcacheScan": &types.LogRegex{ "RegexGcacheScan": &types.LogRegex{
// those "operators" regexes do not have the log prefix added implicitly. It's not strictly needed, but // those "operators" regexes do not have the log prefix added implicitly. It's not strictly needed, but
// it will help to avoid catching random piece of log out of order // it will help to avoid catching random piece of log out of order
Regex: regexp.MustCompile(k8sprefix + ".*GCache::RingBuffer initial scan"), Regex: regexp.MustCompile(types.OperatorLogPrefix + ".*GCache::RingBuffer initial scan"),
Handler: func(submatches map[string]string, logCtx types.LogCtx, log string, date time.Time) (types.LogCtx, types.LogDisplayer) { Handler: func(submatches map[string]string, logCtx types.LogCtx, log string, date time.Time) (types.LogCtx, types.LogDisplayer) {
return logCtx, types.SimpleDisplayer("recovering gcache") return logCtx, types.SimpleDisplayer("recovering gcache")
}, },

View File

@@ -1,5 +1,9 @@
package types package types
const (
OperatorLogPrefix = `{"log":"`
)
type OperatorMetadata struct { type OperatorMetadata struct {
PodName string PodName string
Deployment string Deployment string