Добавлен сбор телеметрии на ВМ2
This commit is contained in:
@@ -5,10 +5,14 @@ import ssl
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
from enum import StrEnum
|
||||
from time import monotonic
|
||||
from typing import Any
|
||||
from urllib.parse import urljoin, urlsplit
|
||||
|
||||
import httpx
|
||||
from opentelemetry.trace import SpanKind
|
||||
|
||||
from app.telemetry import record_crm, record_retry, safe_span
|
||||
|
||||
|
||||
class CrmOutcome(StrEnum):
|
||||
@@ -49,6 +53,28 @@ class CrmClient:
|
||||
await self._client.aclose()
|
||||
|
||||
async def call(self, method: str, params: dict[str, Any], *, mutating: bool) -> CrmResult:
|
||||
started = monotonic()
|
||||
outcome = "error"
|
||||
operation = method.rsplit(".", 1)[-1]
|
||||
try:
|
||||
with safe_span(
|
||||
"bitrix_sync.crm.request",
|
||||
kind=SpanKind.CLIENT,
|
||||
attributes={"crm.operation": operation},
|
||||
):
|
||||
result = await self._call(method, params, mutating=mutating)
|
||||
outcome = result.outcome.value
|
||||
if result.outcome in {
|
||||
CrmOutcome.RETRY,
|
||||
CrmOutcome.UNCERTAIN,
|
||||
CrmOutcome.RATE_LIMITED,
|
||||
}:
|
||||
record_retry("crm")
|
||||
return result
|
||||
finally:
|
||||
record_crm(method, outcome, monotonic() - started)
|
||||
|
||||
async def _call(self, method: str, params: dict[str, Any], *, mutating: bool) -> CrmResult:
|
||||
if method not in ALLOWED_METHODS:
|
||||
raise ValueError("unapproved CRM method")
|
||||
url = urljoin(self._base_url, method + ".json")
|
||||
|
||||
Reference in New Issue
Block a user