feat(worker): supervise VPN connections and recover batched telemetry
This commit is contained in:
@@ -29,6 +29,7 @@ PIPELINE_EVENT_MEASUREMENT: Final = "missioncore_pipeline_event"
|
||||
QUERY_SCHEMA: Final = "missioncore.telemetry-query/v1"
|
||||
SAFE_IDENTIFIER: Final = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?$")
|
||||
MAX_PAYLOAD_BYTES: Final = 1024 * 1024
|
||||
MAX_MESSAGE_METRICS: Final = 512
|
||||
MAX_TAGS: Final = 256
|
||||
MAX_SERIES_KEY_BYTES: Final = 4096
|
||||
LATEST_TTL_SECONDS: Final = 300
|
||||
@@ -218,6 +219,28 @@ def _normalize(topic: str, payload: bytes) -> tuple[object, ...]:
|
||||
)
|
||||
|
||||
|
||||
def normalize_message(topic: str, payload: bytes) -> list[tuple]:
|
||||
"""Accept both original single samples and Telegraf's topic batch envelope.
|
||||
|
||||
Validate the complete bounded message before admitting any of its samples.
|
||||
Every sample retains its own source timestamp and series identity.
|
||||
"""
|
||||
if len(payload) > MAX_PAYLOAD_BYTES:
|
||||
raise ValueError("telemetry message is too large")
|
||||
document = json.loads(payload)
|
||||
if not isinstance(document, dict) or "metrics" not in document:
|
||||
return [_normalize(topic, payload)]
|
||||
metrics = document["metrics"]
|
||||
if (
|
||||
set(document) != {"metrics"}
|
||||
or not isinstance(metrics, list)
|
||||
or not 1 <= len(metrics) <= MAX_MESSAGE_METRICS
|
||||
or any(not isinstance(metric, dict) or "metrics" in metric for metric in metrics)
|
||||
):
|
||||
raise ValueError("invalid telemetry batch")
|
||||
return [_normalize(topic, json.dumps(metric).encode("utf-8")) for metric in metrics]
|
||||
|
||||
|
||||
class LatestTelemetry:
|
||||
"""Bounded volatile latest-value cache; never a time-series archive."""
|
||||
|
||||
@@ -516,9 +539,9 @@ def main() -> None:
|
||||
|
||||
def on_message(_client, _userdata, message):
|
||||
try:
|
||||
row = _normalize(message.topic, message.payload)
|
||||
if cache.put(row):
|
||||
runtime_health.mark_ingested()
|
||||
for row in normalize_message(message.topic, message.payload):
|
||||
if cache.put(row):
|
||||
runtime_health.mark_ingested()
|
||||
except (ValueError, UnicodeDecodeError, TypeError, OverflowError):
|
||||
# Never log rejected payloads or emit an unbounded line per bad sample.
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user