-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_logs.py
More file actions
334 lines (264 loc) · 9.46 KB
/
generate_logs.py
File metadata and controls
334 lines (264 loc) · 9.46 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python3
"""Generate synthetic log files compatible with log-chaos-visualizer.
Supports Pino, Winston, Loki, Promtail, Docker JSON logs, and plain text lines.
Useful for generating large test files (e.g. 20k, 50k, 100k, 200k lines).
Usage examples:
python generate_logs.py --lines 20000 --output public/data/generated-20000.log
python generate_logs.py --lines 50000 --output public/data/generated-50000.log --mix pino,winston,text
"""
from __future__ import annotations
import argparse
import json
import os
import random
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Callable, Iterable, Tuple
LOG_KINDS: Tuple[str, ...] = ("pino", "winston", "loki", "promtail", "docker", "text")
@dataclass
class GeneratorConfig:
kinds: Tuple[str, ...]
total_lines: int
seed: int | None
auto_seed: bool
def _rand_hostname() -> str:
return f"host-{random.randint(1, 10)}"
def _rand_service_name() -> str:
return random.choice(["auth-service", "api-gateway", "billing", "search", "worker"])
def _rand_environment() -> str:
return random.choice(["dev", "staging", "prod"])
def _rand_optional_environment() -> str | None:
# 70% chance to attach an environment to non-Loki logs
return _rand_environment() if random.random() < 0.7 else None
def _rand_pino_level() -> int:
return random.choice([10, 20, 30, 40, 50, 60])
def _rand_winston_level() -> str:
return random.choice(["silly", "debug", "verbose", "info", "warn", "error"])
def _rand_promtail_level() -> str:
return random.choice(["debug", "info", "warn", "error"])
def _rand_http_method() -> str:
return random.choice(["GET", "POST", "PUT", "PATCH", "DELETE"])
def _rand_path() -> str:
return random.choice(
[
"/api/login",
"/api/logout",
"/api/orders",
"/api/orders/123",
"/health",
"/metrics",
"/api/search?q=test",
]
)
def _rand_message() -> str:
base = random.choice(
[
"User logged in",
"User logged out",
"Order created",
"Order updated",
"Cache miss",
"Cache hit",
"Background job started",
"Background job finished",
"Database query executed",
]
)
extra = f"userId={random.randint(1, 1000)}"
return f"{base} {extra}"
def _rand_iso_timestamp() -> str:
now = datetime.now(timezone.utc)
delta = timedelta(seconds=random.randint(-3600, 0))
return (now + delta).isoformat()
def _rand_epoch_millis() -> int:
now = datetime.now(timezone.utc)
epoch = datetime(1970, 1, 1, tzinfo=timezone.utc)
return int((now - epoch).total_seconds() * 1000)
def generate_pino_line() -> str:
entry = {
"time": _rand_epoch_millis(),
"level": _rand_pino_level(),
"pid": random.randint(1000, 9999),
"hostname": _rand_hostname(),
"name": _rand_service_name(),
"msg": _rand_message(),
}
if random.random() < 0.7:
entry["req"] = {
"id": f"req-{random.randint(1_000_000, 9_999_999)}",
"method": _rand_http_method(),
"url": _rand_path(),
"remoteAddress": f"192.168.0.{random.randint(1, 254)}",
}
if random.random() < 0.7:
entry["res"] = {
"statusCode": random.choice([200, 201, 204, 400, 401, 403, 404, 500]),
"responseTimeMs": random.randint(1, 500),
}
env = _rand_optional_environment()
if random.random() < 0.4 or env is not None:
meta: dict[str, object] = {
"traceId": f"trace-{random.randint(1_000_000, 9_999_999)}",
"spanId": f"span-{random.randint(1_000_000, 9_999_999)}",
}
if env is not None:
meta["environment"] = env
entry["meta"] = meta
return json.dumps(entry, separators=(",", ":"))
def generate_winston_line() -> str:
entry: dict[str, object] = {
"timestamp": _rand_iso_timestamp(),
"level": _rand_winston_level(),
"message": _rand_message(),
}
if random.random() < 0.7:
meta: dict[str, object] = {
"requestId": f"req-{random.randint(1_000_000, 9_999_999)}",
"userId": random.randint(1, 1000),
}
env = _rand_optional_environment()
if env is not None:
meta["environment"] = env
entry["meta"] = meta
return json.dumps(entry, separators=(",", ":"))
def generate_loki_line() -> str:
entry = {
"ts": _rand_iso_timestamp(),
"labels": {
"job": "app-logs",
"instance": _rand_hostname(),
"app": _rand_service_name(),
"environment": _rand_environment(),
},
"line": _rand_message(),
}
return json.dumps(entry, separators=(",", ":"))
def generate_promtail_line() -> str:
entry: dict[str, object] = {
"ts": _rand_iso_timestamp(),
"level": _rand_promtail_level(),
"message": _rand_message(),
}
env = _rand_optional_environment()
if env is not None:
entry["environment"] = env
return json.dumps(entry, separators=(",", ":"))
def generate_docker_line() -> str:
env = _rand_optional_environment()
log_msg = _rand_message()
if env is not None:
log_msg = f"env={env} {log_msg}"
entry = {
"log": log_msg + "\n",
"stream": random.choice(["stdout", "stderr"]),
"time": _rand_iso_timestamp(),
}
return json.dumps(entry, separators=(",", ":"))
def generate_text_line() -> str:
level = random.choice(["INFO", "WARN", "ERROR", "DEBUG", "TRACE"])
env = _rand_optional_environment()
parts = [
level,
datetime.now(timezone.utc).isoformat(),
_rand_service_name() + ":",
]
if env is not None:
parts.append(f"env={env}")
parts.append(_rand_message())
return " ".join(parts)
GENERATOR_BY_KIND: dict[str, Callable[[], str]] = {
"pino": generate_pino_line,
"winston": generate_winston_line,
"loki": generate_loki_line,
"promtail": generate_promtail_line,
"docker": generate_docker_line,
"text": generate_text_line,
}
def generate_logs(config: GeneratorConfig, output_path: Path) -> None:
for kind in config.kinds:
if kind not in GENERATOR_BY_KIND:
raise ValueError(f"Unsupported log kind in mix: {kind!r}")
output_path.parent.mkdir(parents=True, exist_ok=True)
# Seeding strategy:
# - If seed is provided, use it (reproducible but still random-looking).
# - Else if auto_seed is True, seed from OS randomness.
# - Else, leave RNG state as-is (caller/environment controls it).
if config.seed is not None:
random.seed(config.seed)
elif config.auto_seed:
random.seed(os.urandom(32))
with output_path.open("w", encoding="utf-8") as f:
for idx in range(1, config.total_lines + 1):
kind = random.choice(config.kinds)
line = GENERATOR_BY_KIND[kind]()
if line.endswith("\n"):
line = line.rstrip("\n")
f.write(line + "\n")
if idx % 10000 == 0:
import sys
print(f"... generated {idx} lines", file=sys.stderr)
def parse_args(argv: Iterable[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Generate synthetic log files for log-chaos-visualizer.")
parser.add_argument("--lines", type=int, required=True, help="Number of log lines to generate.")
parser.add_argument(
"--output",
type=str,
required=True,
help="Path to output .log file (e.g. public/data/generated-20000.log).",
)
parser.add_argument(
"--mix",
type=str,
default=",".join(LOG_KINDS),
help=(
"Comma-separated list of log kinds to include. "
"Supported kinds: pino,winston,loki,promtail,docker,text. Default: all kinds."
),
)
parser.add_argument(
"--seed",
type=int,
default=None,
help=(
"Optional random seed for reproducible output. "
"If omitted, a random seed from the OS is used unless --no-auto-seed is set."
),
)
parser.add_argument(
"--no-auto-seed",
action="store_true",
help=(
"Do not automatically seed the RNG from OS randomness when --seed is omitted. "
"Use the current interpreter RNG state instead."
),
)
return parser.parse_args(argv)
def main(argv: Iterable[str] | None = None) -> int:
import sys
args = parse_args(argv)
if args.lines <= 0:
print("--lines must be a positive integer", file=sys.stderr)
return 1
kinds = tuple(k.strip() for k in args.mix.split(",") if k.strip())
if not kinds:
print("No log kinds specified in --mix", file=sys.stderr)
return 1
config = GeneratorConfig(
kinds=kinds,
total_lines=args.lines,
seed=args.seed,
auto_seed=not args.no_auto_seed,
)
output_path = Path(args.output)
print(
f"Generating {config.total_lines} lines into {output_path} "
f"with mix={','.join(config.kinds)} seed={config.seed} auto_seed={config.auto_seed}",
file=sys.stderr,
)
generate_logs(config, output_path)
print("Done.", file=sys.stderr)
return 0
if __name__ == "__main__": # pragma: no cover
import sys
raise SystemExit(main(sys.argv[1:]))