fix: 🐛 formatting for loki
CI / Build and Push (push) Successful in 8s
CI / SonarQube (push) Successful in 17s

This commit is contained in:
2026-07-10 20:06:04 +01:00
parent ea3eab85e6
commit 8cf10a5f0f
+52 -13
View File
@@ -33,34 +33,73 @@ logger = logging.getLogger(__name__)
app = Flask(__name__) app = Flask(__name__)
def discord_payload_to_log_lines(payload: dict) -> list[str]: def clean_markdown(text: str) -> str:
"""Extract log lines from a Discord-style webhook payload (content + embeds).""" """Remove Discord markdown blocks to make text more readable."""
lines = [] if not isinstance(text, str):
if not payload: return ""
return lines return text.replace("```json\n", "").replace("```\n", "").replace("```", "").strip()
def discord_payload_to_log_lines(payload: dict) -> list[str]:
"""Extract log lines from a Discord-style webhook payload.
Formats the entire payload as a single JSON string for Loki, parsing embedded JSON if possible.
"""
if not payload:
return []
result = {}
content = payload.get("content") content = payload.get("content")
if isinstance(content, str) and content.strip(): if isinstance(content, str) and content.strip():
lines.append(content.strip()) cleaned = clean_markdown(content)
try:
result["content"] = json.loads(cleaned)
except json.JSONDecodeError:
result["content"] = cleaned
embeds_out = []
for embed in payload.get("embeds") or []: for embed in payload.get("embeds") or []:
if not isinstance(embed, dict): if not isinstance(embed, dict):
continue continue
e_out = {}
title = embed.get("title") title = embed.get("title")
if isinstance(title, str) and title.strip(): if isinstance(title, str) and title.strip():
lines.append(f"embed: {title.strip()}") e_out["title"] = title.strip()
description = embed.get("description") description = embed.get("description")
if isinstance(description, str) and description.strip(): if isinstance(description, str) and description.strip():
lines.append(f"embed: {description.strip()}") cleaned = clean_markdown(description)
try:
e_out["description"] = json.loads(cleaned)
except json.JSONDecodeError:
e_out["description"] = cleaned
fields_out = {}
for field in embed.get("fields") or []: for field in embed.get("fields") or []:
if not isinstance(field, dict): if not isinstance(field, dict):
continue continue
name = field.get("name") or "" name = field.get("name")
value = field.get("value") or "" value = field.get("value")
if isinstance(name, str) and isinstance(value, str): if isinstance(name, str) and isinstance(value, str) and name.strip():
lines.append(f"field: {name.strip()} | {value.strip()}") cleaned_val = clean_markdown(value)
try:
fields_out[name.strip()] = json.loads(cleaned_val)
except json.JSONDecodeError:
fields_out[name.strip()] = cleaned_val
return lines if fields_out:
e_out["fields"] = fields_out
if e_out:
embeds_out.append(e_out)
if embeds_out:
result["embeds"] = embeds_out
if not result:
return []
return [json.dumps(result)]
def push_to_loki(log_lines: list[str]) -> None: def push_to_loki(log_lines: list[str]) -> None: