Реализовать observability для лабораторной 6

This commit is contained in:
2026-11-20 17:55:23 +03:00
parent 58f3d4c457
commit 9e6428de05
50 changed files with 3573 additions and 98 deletions
+36 -12
View File
@@ -12,9 +12,13 @@ from minio import Minio
from minio.error import InvalidResponseError, S3Error
from urllib3 import PoolManager, Retry, Timeout
from urllib3.exceptions import HTTPError
from opentelemetry import trace
from opentelemetry.trace import SpanKind
from ..domain.errors import Unavailable
tracer = trace.get_tracer('messenger.s3', '1.6.0')
def _unavailable(error: Exception) -> Unavailable:
return Unavailable('объектное хранилище недоступно')
@@ -77,7 +81,11 @@ class S3Storage:
async def ping(self) -> None:
try:
exists = await asyncio.to_thread(self._client.bucket_exists, self.bucket)
with tracer.start_as_current_span(
's3 bucket exists', kind=SpanKind.CLIENT,
attributes={'db.system': 's3', 's3.bucket.name': self.bucket},
):
exists = await asyncio.to_thread(self._client.bucket_exists, self.bucket)
except Exception as error:
self._translate(error)
if not exists:
@@ -86,20 +94,28 @@ class S3Storage:
async def put(self, key: str, source: BinaryIO, length: int, content_type: str) -> None:
source.seek(0)
try:
await asyncio.to_thread(
self._client.put_object,
self.bucket,
key,
source,
length,
content_type,
)
with tracer.start_as_current_span(
's3 put object', kind=SpanKind.CLIENT,
attributes={'db.system': 's3', 's3.bucket.name': self.bucket},
):
await asyncio.to_thread(
self._client.put_object,
self.bucket,
key,
source,
length,
content_type,
)
except Exception as error:
self._translate(error)
async def get(self, key: str) -> ObjectStream:
try:
response = await asyncio.to_thread(self._client.get_object, self.bucket, key)
with tracer.start_as_current_span(
's3 get object', kind=SpanKind.CLIENT,
attributes={'db.system': 's3', 's3.bucket.name': self.bucket},
):
response = await asyncio.to_thread(self._client.get_object, self.bucket, key)
except Exception as error:
self._translate(error)
return ObjectStream(response)
@@ -118,7 +134,11 @@ class S3Storage:
async def exists(self, key: str) -> bool:
try:
await asyncio.to_thread(self._client.stat_object, self.bucket, key)
with tracer.start_as_current_span(
's3 stat object', kind=SpanKind.CLIENT,
attributes={'db.system': 's3', 's3.bucket.name': self.bucket},
):
await asyncio.to_thread(self._client.stat_object, self.bucket, key)
return True
except Exception as error:
try:
@@ -129,7 +149,11 @@ class S3Storage:
async def remove(self, key: str) -> None:
try:
await asyncio.to_thread(self._client.remove_object, self.bucket, key)
with tracer.start_as_current_span(
's3 remove object', kind=SpanKind.CLIENT,
attributes={'db.system': 's3', 's3.bucket.name': self.bucket},
):
await asyncio.to_thread(self._client.remove_object, self.bucket, key)
except Exception as error:
self._translate(error)