-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enhance diag tool log parser and fix python setup (#3303)
- Loading branch information
1 parent
8ffda7c
commit cfc86ce
Showing
11 changed files
with
128 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
errors: | ||
other: | ||
patterns: | ||
- "at com.*openmldb" | ||
- "At .*OpenMLDB" | ||
- "Caused by" | ||
- "java.*Exception" | ||
- "Exception in" | ||
- "ERROR" | ||
description: "just print errs" | ||
zk_conn_err: | ||
patterns: | ||
- "fail to init zk handler with hosts" | ||
description: "Error: fail to init zk handler with hosts" | ||
solution: "zk_conn_err" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,80 @@ | ||
import os | ||
import re | ||
import requests | ||
import warnings | ||
import yaml | ||
|
||
from diagnostic_tool.connector import Connector | ||
from diagnostic_tool.server_checker import StatusChecker | ||
|
||
def log_parser(log): | ||
log_lines = log.split("\n") | ||
error_patterns = [ | ||
re.compile(r"at com.*openmldb"), | ||
re.compile(r"At .*OpenMLDB"), | ||
re.compile(r"Caused by"), | ||
re.compile(r"java.*Exception"), | ||
re.compile(r"Exception in"), | ||
re.compile(r"ERROR"), | ||
] | ||
|
||
error_messages = [] | ||
skip_flag = 0 | ||
|
||
for line in log_lines: | ||
for pattern in error_patterns: | ||
match = pattern.search(line) | ||
if match: | ||
error_messages.append(line) | ||
skip_flag = 1 | ||
break | ||
CONF_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "common_err.yml") | ||
|
||
|
||
class LogParser: | ||
def __init__(self, log_conf_file=CONF_FILE) -> None: | ||
self.conf_file = log_conf_file | ||
self._load_conf() | ||
|
||
def _load_conf(self): | ||
self.errs = yaml.safe_load(open(self.conf_file))["errors"] | ||
|
||
def parse_log(self, log: str): | ||
log_rows = log.split("\n") | ||
# solution results | ||
solution_results = [] | ||
# skip irrelevant rows | ||
skip_flag = False | ||
for row in log_rows: | ||
result = self._parse_row(row) | ||
if result: | ||
if result != "null": | ||
solution_results.append(result) | ||
skip_flag = True | ||
continue | ||
# print "..." if some lines are skipped | ||
else: | ||
if skip_flag: | ||
print("...") | ||
skip_flag = False | ||
if solution_results: | ||
print("Solutions".center(50, "=")) | ||
print(*solution_results, sep="\n") | ||
|
||
def _parse_row(self, row): | ||
for name, value in self.errs.items(): | ||
for pattern in value['patterns']: | ||
if re.search(pattern, row): | ||
print(row) | ||
if "solution" in self.errs[name]: | ||
solution = ErrSolution(self.errs[name]) | ||
result = solution() | ||
return result | ||
return "null" | ||
|
||
def update_conf_file(self, log_conf_url): | ||
response = requests.get(log_conf_url) | ||
if response.status_code == 200: | ||
with open(self.conf_file, "w") as f: | ||
f.write(response.text) | ||
else: | ||
if skip_flag: | ||
error_messages.append("...") | ||
skip_flag = 0 | ||
warnings.warn("log parser configuration update failed") | ||
self._load_conf() | ||
|
||
|
||
class ErrSolution: | ||
def __init__(self, err) -> None: | ||
self.desc = err["description"] | ||
self.solution = err["solution"] | ||
self.result = "" | ||
|
||
def __call__(self, *args, **kwargs): | ||
getattr(self, self.solution)() | ||
return self.result | ||
|
||
return error_messages | ||
def zk_conn_err(self): | ||
self.result += "\n" + self.desc | ||
self.result += "\nChecking zk connection..." | ||
conn = Connector() | ||
checker = StatusChecker(conn) | ||
assert checker._get_components(show=False), "Failed to connect to zk" | ||
self.result += "\nSuccessfully checked zk connection. It may be caused by `Too many connections` in zk server. Please check zk server log." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
import os | ||
import re | ||
from absl import flags | ||
|
||
import pytest | ||
from diagnostic_tool.parser import log_parser | ||
from diagnostic_tool.parser import LogParser | ||
from .case_conf import OpenMLDB_ZK_CLUSTER | ||
|
||
err_log_list = [os.path.join("off_err_logs", err_log) for err_log in os.listdir("off_err_logs")] | ||
logs_path = os.path.join(os.path.dirname(__file__), "off_err_logs") | ||
err_log_list = [os.path.join(logs_path, err_log) for err_log in os.listdir(logs_path)] | ||
|
||
|
||
@pytest.mark.parametrize("err_log", err_log_list) | ||
def test_pattern_logs(err_log): | ||
flags.FLAGS['cluster'].parse(OpenMLDB_ZK_CLUSTER) | ||
flags.FLAGS['sdk_log'].parse(False) | ||
print("in", err_log) | ||
with open(err_log, "r") as f: | ||
log = f.read() | ||
err_lines = log_parser(log) | ||
print(*err_lines, sep="\n") | ||
parser = LogParser() | ||
parser.update_conf_file("https://openmldb.ai/download/diag/common_err.yml") | ||
parser.parse_log(log) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters