-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.py
More file actions
166 lines (128 loc) · 4 KB
/
input.py
File metadata and controls
166 lines (128 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import json
import re
from dataclasses import dataclass
from datetime import datetime
from datetime import timezone
from typing import Dict, Iterable, Optional, Tuple
# ---------- Data Models ----------
@dataclass(frozen=True)
class LogEvent:
timestamp: datetime
level: str
service: str
template: str
variables: Dict[str, str]
raw: str
request_id: Optional[str]
@dataclass
class ParseFailure:
raw: str
reason: str
# ---------- Metrics ----------
class IngestMetrics:
def __init__(self):
self.parsed = 0
self.failed = 0
self.failures_by_reason: Dict[str, int] = {}
def record_success(self):
self.parsed += 1
def record_failure(self, reason: str):
self.failed += 1
self.failures_by_reason[reason] = (
self.failures_by_reason.get(reason, 0) + 1
)
# ---------- Regex (text logs) ----------
TEXT_LOG_PATTERN = re.compile(
r"""
^(?P<timestamp>\S+)\s+
(?P<level>DEBUG|INFO|WARN|ERROR)\s+
(?P<service>[\w\-]+)\s+
(?P<message>.+)$
""",
re.VERBOSE,
)
NUMBER_PATTERN = re.compile(r"\b\d+\b")
UUID_PATTERN = re.compile(
r"\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-"
r"[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-"
r"[0-9a-fA-F]{12}\b"
)
# ---------- Normalization ----------
TIMEOUT_PATTERN = re.compile(r"timeout after \d+ms")
SLOW_RESPONSE_PATTERN = re.compile(r"slow response time=\d+ms")
def normalize_message(message: str) -> Tuple[str, Dict[str, str]]:
message = TIMEOUT_PATTERN.sub("timeout after <TIMEOUT>ms", message)
message = SLOW_RESPONSE_PATTERN.sub(
"slow response time=<LATENCY>ms", message
)
variables: Dict[str, str] = {}
def repl_uuid(match):
key = f"uuid_{len(variables)}"
variables[key] = match.group(0)
return "<UUID>"
def repl_num(match):
key = f"num_{len(variables)}"
variables[key] = match.group(0)
return "<NUM>"
message = UUID_PATTERN.sub(repl_uuid, message)
message = NUMBER_PATTERN.sub(repl_num, message)
return message, variables
# ---------- Parsers ----------
def parse_text_log(line: str) -> Optional[LogEvent]:
match = TEXT_LOG_PATTERN.match(line.strip())
if not match:
return None
try:
timestamp = datetime.fromisoformat(match.group("timestamp")).replace(tzinfo=timezone.utc)
except ValueError:
return None
template, variables = normalize_message(match.group("message"))
return LogEvent(
timestamp=timestamp,
level=match.group("level"),
service=match.group("service"),
template=template,
variables=variables,
raw=line.strip(),
request_id=None,
)
def parse_json_log(line: str) -> Optional[LogEvent]:
try:
data = json.loads(line)
except json.JSONDecodeError:
return None
required = {"timestamp", "level", "service", "message"}
if not required.issubset(data):
return None
try:
timestamp = datetime.fromisoformat(match.group("timestamp")).replace(tzinfo=timezone.utc)
except ValueError:
return None
template, variables = normalize_message(data["message"])
return LogEvent(
timestamp=timestamp,
level=data["level"],
service=data["service"],
template=template,
variables=variables,
raw=line.strip(),
request_id=data.get("request_id"),
)
# ---------- Ingest Pipeline ----------
class LogIngestor:
def __init__(self):
self.metrics = IngestMetrics()
def ingest(self, lines: Iterable[str]) -> Iterable[LogEvent]:
for line in lines:
event = self._parse_line(line)
if event:
self.metrics.record_success()
yield event
else:
self.metrics.record_failure("unrecognized_format")
def _parse_line(self, line: str) -> Optional[LogEvent]:
# Order matters: JSON first, then text
return (
parse_json_log(line)
or parse_text_log(line)
)