Backfill traces from logs
Recipe — backfill traces from existing application logs.
Pattern
import json
from pathlib import Path
from layerlens import Stratix
client = Stratix()
def chunked_jsonl(records, chunk_path_template, max_bytes=45_000_000):
"""Split records into ≤45 MB JSONL files (50 MB hard limit; 45 MB safe)."""
chunk_idx, current_size, current_records = 0, 0, []
for r in records:
line = json.dumps(r)
if current_size + len(line) > max_bytes and current_records:
path = chunk_path_template.format(chunk_idx)
Path(path).write_text("\n".join(current_records))
yield path
chunk_idx += 1
current_size, current_records = 0, []
current_records.append(line)
current_size += len(line) + 1
if current_records:
path = chunk_path_template.format(chunk_idx)
Path(path).write_text("\n".join(current_records))
yield path
# Yield JSONL chunks from your log parser, upload each
for chunk_path in chunked_jsonl(parse_logs(), "backfill_{}.jsonl"):
result = client.traces.upload(chunk_path)
print(f"Uploaded {len(result.trace_ids)} traces from {chunk_path}")See also
Last updated
Was this helpful?