add level 3 and 4
This commit is contained in:
+31
-15
@@ -20,6 +20,9 @@ from .routes import router
|
||||
from .attachments import router as attachment_router
|
||||
from .storage.s3 import S3Settings, S3Store
|
||||
from .storage.postgres import PostgresStore
|
||||
from . import telemetry as tel
|
||||
|
||||
tel.setup("messenger-api")
|
||||
|
||||
|
||||
def logger(name: str) -> logging.Logger:
|
||||
@@ -51,9 +54,20 @@ async def lifespan(application: FastAPI):
|
||||
raise ValueError("UPLOAD_CONCURRENCY must be 1..8")
|
||||
application.state.upload_slot = asyncio.Semaphore(concurrency)
|
||||
application.state.accepting_requests = True
|
||||
async def sample_pool():
|
||||
while True:
|
||||
pool = store.pool
|
||||
tel.state.update(pool_used=pool.get_size()-pool.get_idle_size(), pool_capacity=pool.get_max_size(), pool_waiting=len(pool._queue._getters))
|
||||
await asyncio.sleep(1)
|
||||
sampler = asyncio.create_task(sample_pool())
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
sampler.cancel()
|
||||
try:
|
||||
await sampler
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
application.state.accepting_requests = False
|
||||
application.state.s3.client.close()
|
||||
await store.close()
|
||||
@@ -74,21 +88,23 @@ async def add_request_id(request: Request, call_next) -> Response:
|
||||
request.state.request_id = str(uuid4())
|
||||
started_at = time.monotonic()
|
||||
status_code = 500
|
||||
try:
|
||||
response = await call_next(request)
|
||||
status_code = response.status_code
|
||||
response.headers["X-Request-Id"] = request.state.request_id
|
||||
response.headers["X-Instance-Id"] = request.app.state.settings.instance_id
|
||||
return response
|
||||
finally:
|
||||
access_logger.info(
|
||||
"request_id=%s method=%s path=%s status=%s elapsed_ms=%.3f",
|
||||
request.state.request_id,
|
||||
request.method,
|
||||
request.url.path,
|
||||
status_code,
|
||||
(time.monotonic() - started_at) * 1000,
|
||||
)
|
||||
with tel.tracer.start_as_current_span("HTTP request", context=tel.extract(dict(request.headers)), kind=tel.SpanKind.SERVER, record_exception=False, set_status_on_exception=False) as span:
|
||||
try:
|
||||
response = await call_next(request)
|
||||
status_code = response.status_code
|
||||
response.headers["X-Request-Id"] = request.state.request_id
|
||||
response.headers["X-Instance-Id"] = request.app.state.settings.instance_id
|
||||
return response
|
||||
finally:
|
||||
route = getattr(request.scope.get("route"), "path", "unmatched")
|
||||
attrs = {"method": request.method, "route": route, "status_class": f"{status_code // 100}xx"}
|
||||
span.update_name(f"{request.method} {route}")
|
||||
span.set_attributes({"http.request.method": request.method, "http.route": route, "http.response.status_code": status_code})
|
||||
if status_code >= 500:
|
||||
span.set_status(tel.Status(tel.StatusCode.ERROR))
|
||||
tel.http_requests.add(1, attrs)
|
||||
tel.http_duration.record(time.monotonic() - started_at, attrs)
|
||||
tel.log.info("http.request", extra={"request_id": request.state.request_id, "method": request.method, "route": route, "status": status_code})
|
||||
|
||||
|
||||
def error_response(
|
||||
|
||||
Reference in New Issue
Block a user