#!/usr/bin/env python3 import json import subprocess import time import urllib.error import urllib.parse import urllib.request from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def compose(*args): return subprocess.run( ["docker", "compose", "--env-file", ".env", *args], cwd=str(ROOT), capture_output=True, text=True, ) def main(): try: urllib.request.urlopen("http://127.0.0.1:8080/health/live", timeout=5).read() except urllib.error.HTTPError: pass time.sleep(10) end = int(time.time()) start = end - 1800 for query in ( '{service_name="messenger-api"}', '{service_name="messenger-worker"}', '{service_name=~".+"}', ): url = "http://127.0.0.1:3100/loki/api/v1/query_range?" + urllib.parse.urlencode( { "query": query, "start": str(start) + "000000000", "end": str(end) + "000000000", "limit": "20", } ) out = compose("exec", "-T", "loki", "wget", "-qO-", url) try: data = json.loads(out.stdout) streams = data.get("data", {}).get("result", []) lines = sum(len(item.get("values", [])) for item in streams) print(query, "streams", len(streams), "lines", lines) if streams and streams[0].get("values"): print("sample", streams[0]["values"][0][1][:240]) except Exception as exc: print(query, "err", exc, out.stdout[:200], out.stderr[:200]) print("collector tail:") print(compose("logs", "otel-collector", "--since", "2m").stdout[-2000:]) q = '{service_name="messenger-api"} |= "http_request"' url = "http://127.0.0.1:3100/loki/api/v1/query_range?" + urllib.parse.urlencode( { "query": q, "start": str(start) + "000000000", "end": str(end) + "000000000", "limit": "10", } ) out = compose("exec", "-T", "loki", "wget", "-qO-", url) data = json.loads(out.stdout) streams = data.get("data", {}).get("result", []) print("http_request streams", len(streams), "lines", sum(len(s.get("values", [])) for s in streams)) for stream in streams[:3]: for value in stream.get("values", [])[:2]: print(value[1][:400]) if __name__ == "__main__": main()