forked from mtusi2026/mtusi2026lab6
Собрать наблюдаемый стенд, чтобы причину деградации API и worker было видно по метрикам, логам и трассам.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import threading
|
||||
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
|
||||
from app import telemetry
|
||||
from app.settings import settings
|
||||
|
||||
_client = None
|
||||
_lock = threading.Lock()
|
||||
|
||||
|
||||
def init():
|
||||
global _client
|
||||
_client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=settings.s3_endpoint,
|
||||
aws_access_key_id=settings.s3_access_key,
|
||||
aws_secret_access_key=settings.s3_secret_key,
|
||||
region_name=settings.s3_region,
|
||||
config=Config(
|
||||
signature_version="s3v4",
|
||||
s3={"addressing_style": "path"},
|
||||
connect_timeout=2,
|
||||
read_timeout=10,
|
||||
retries={"max_attempts": 2, "mode": "standard"},
|
||||
),
|
||||
)
|
||||
try:
|
||||
_client.head_bucket(Bucket=settings.s3_bucket)
|
||||
return
|
||||
except ClientError as exc:
|
||||
code = str(exc.response.get("Error", {}).get("Code", ""))
|
||||
if code not in {"404", "NoSuchBucket", "NotFound"}:
|
||||
raise
|
||||
try:
|
||||
_client.create_bucket(Bucket=settings.s3_bucket)
|
||||
except ClientError as exc:
|
||||
code = str(exc.response.get("Error", {}).get("Code", ""))
|
||||
if code not in {"BucketAlreadyOwnedByYou", "BucketAlreadyExists"}:
|
||||
raise
|
||||
_warm()
|
||||
|
||||
|
||||
def _warm():
|
||||
"""Read one existing object so the first user download is not a cold volume open."""
|
||||
try:
|
||||
listed = _client.list_objects_v2(Bucket=settings.s3_bucket, MaxKeys=1)
|
||||
except (BotoCoreError, ClientError, OSError):
|
||||
return
|
||||
contents = listed.get("Contents") or []
|
||||
if not contents:
|
||||
return
|
||||
try:
|
||||
body = _client.get_object(Bucket=settings.s3_bucket, Key=contents[0]["Key"])["Body"]
|
||||
body.read()
|
||||
except (BotoCoreError, ClientError, OSError):
|
||||
return
|
||||
|
||||
|
||||
def ready():
|
||||
try:
|
||||
with _lock:
|
||||
_client.head_bucket(Bucket=settings.s3_bucket)
|
||||
return True
|
||||
except (BotoCoreError, ClientError, OSError):
|
||||
return False
|
||||
|
||||
|
||||
def put(key, data, content_type, variant):
|
||||
with telemetry.tracer().start_as_current_span(
|
||||
"s3.put",
|
||||
attributes={"s3.operation": "put", "variant": variant},
|
||||
):
|
||||
with _lock:
|
||||
_client.put_object(Bucket=settings.s3_bucket, Key=key, Body=data, ContentType=content_type)
|
||||
|
||||
|
||||
def get(key, variant):
|
||||
with telemetry.tracer().start_as_current_span(
|
||||
"s3.get",
|
||||
attributes={"s3.operation": "get", "variant": variant},
|
||||
):
|
||||
with _lock:
|
||||
response = _client.get_object(Bucket=settings.s3_bucket, Key=key)
|
||||
return response["Body"].read()
|
||||
|
||||
|
||||
def delete(key):
|
||||
with _lock:
|
||||
_client.delete_object(Bucket=settings.s3_bucket, Key=key)
|
||||
Reference in New Issue
Block a user