logutil.py
1 # Copyright 2026 Alibaba Group Holding Ltd. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 """Structured ``renew_*`` keys for ``logging`` ``extra`` and message suffix lines.""" 16 17 from __future__ import annotations 18 19 from typing import Any 20 21 RENEW_SOURCE_SERVER_PROXY = "server_proxy" 22 RENEW_SOURCE_REDIS_QUEUE = "redis_queue" 23 24 RENEW_EVENT_SUCCEEDED = "renew_succeeded" 25 RENEW_EVENT_FAILED = "renew_failed" 26 RENEW_EVENT_TASK_FAILED = "renew_task_failed" 27 RENEW_EVENT_WORKERS_STARTED = "workers_started" 28 RENEW_EVENT_WORKERS_NOT_STARTED = "workers_not_started" 29 RENEW_EVENT_REDIS_CONNECTED = "redis_connected" 30 31 32 def _renew_extra( 33 *, 34 event: str, 35 source: str, 36 sandbox_id: str | None = None, 37 skip_reason: str | None = None, 38 **fields: Any, 39 ) -> dict[str, Any]: 40 out: dict[str, Any] = { 41 "renew_event": event, 42 "renew_source": source, 43 } 44 if sandbox_id is not None: 45 out["renew_sandbox_id"] = sandbox_id 46 if skip_reason is not None: 47 out["renew_skip_reason"] = skip_reason 48 for k, v in fields.items(): 49 if v is not None: 50 key = k if k.startswith("renew_") else f"renew_{k}" 51 out[key] = v 52 return out 53 54 55 def renew_bundle(**kwargs: Any) -> tuple[str, dict[str, Any]]: 56 """``(k=v line, extra dict)`` for one log call.""" 57 extra = _renew_extra(**kwargs) 58 line = " ".join(f"{k}={extra[k]!s}" for k in sorted(extra.keys())) 59 return line, extra