Persist a request log entry without breaking request handling.
(
*,
provider: str,
model: str,
source_info: RequestSourceInfo,
success: bool,
started_at: float,
status_code: int = 200,
first_token_time: float = 0.0,
input_tokens: int = 0,
output_tokens: int = 0,
cache_creation_tokens: int = 0,
cache_read_tokens: int = 0,
total_tokens: Optional[int] = None,
error_message: Optional[str] = None,
)
| 133 | |
| 134 | |
| 135 | async def write_request_log( |
| 136 | *, |
| 137 | provider: str, |
| 138 | model: str, |
| 139 | source_info: RequestSourceInfo, |
| 140 | success: bool, |
| 141 | started_at: float, |
| 142 | status_code: int = 200, |
| 143 | first_token_time: float = 0.0, |
| 144 | input_tokens: int = 0, |
| 145 | output_tokens: int = 0, |
| 146 | cache_creation_tokens: int = 0, |
| 147 | cache_read_tokens: int = 0, |
| 148 | total_tokens: Optional[int] = None, |
| 149 | error_message: Optional[str] = None, |
| 150 | ) -> None: |
| 151 | """Persist a request log entry without breaking request handling.""" |
| 152 | duration = max(0.0, time.perf_counter() - started_at) |
| 153 | try: |
| 154 | dao = get_request_log_dao() |
| 155 | await dao.add_log( |
| 156 | provider=provider, |
| 157 | endpoint=source_info.endpoint, |
| 158 | source=source_info.source, |
| 159 | protocol=source_info.protocol, |
| 160 | client_name=source_info.client_name, |
| 161 | model=model, |
| 162 | status_code=status_code, |
| 163 | success=success, |
| 164 | duration=duration, |
| 165 | first_token_time=first_token_time, |
| 166 | input_tokens=input_tokens, |
| 167 | output_tokens=output_tokens, |
| 168 | cache_creation_tokens=cache_creation_tokens, |
| 169 | cache_read_tokens=cache_read_tokens, |
| 170 | total_tokens=total_tokens, |
| 171 | error_message=error_message, |
| 172 | ) |
| 173 | except Exception as exc: |
| 174 | logger.error(f"写入请求日志失败: {exc}") |
| 175 | |
| 176 | |
| 177 | def _openai_payload_has_output(payload: Dict[str, Any]) -> bool: |
no test coverage detected