You've already forked FrameTour-RenderWorker
埋点
This commit is contained in:
86
util/oss.py
86
util/oss.py
@ -3,6 +3,8 @@ import os
|
||||
|
||||
import requests
|
||||
|
||||
from telemetry import get_tracer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@ -13,20 +15,29 @@ def upload_to_oss(url, file_path):
|
||||
:param str file_path: 文件路径
|
||||
:return bool: 是否成功
|
||||
"""
|
||||
max_retries = 5
|
||||
retries = 0
|
||||
while retries < max_retries:
|
||||
try:
|
||||
with open(file_path, 'rb') as f:
|
||||
response = requests.put(url, data=f, timeout=60) # 设置超时时间为1分钟
|
||||
if response.status_code == 200:
|
||||
return True
|
||||
except requests.exceptions.Timeout:
|
||||
retries += 1
|
||||
logger.warning(f"Upload timed out. Retrying {retries}/{max_retries}...")
|
||||
except Exception as e:
|
||||
logger.warning(f"Upload failed. Retrying {retries}/{max_retries}...")
|
||||
retries += 1
|
||||
tracer = get_tracer(__name__)
|
||||
with tracer.start_as_current_span("upload_to_oss") as span:
|
||||
span.set_attribute("file.url", url)
|
||||
span.set_attribute("file.path", file_path)
|
||||
max_retries = 5
|
||||
retries = 0
|
||||
while retries < max_retries:
|
||||
try:
|
||||
with tracer.start_as_current_span("upload_to_oss.request") as req_span:
|
||||
req_span.set_attribute("http.method", "PUT")
|
||||
req_span.set_attribute("http.url", url)
|
||||
with open(file_path, 'rb') as f:
|
||||
response = requests.put(url, data=f, timeout=60) # 设置超时时间为1分钟
|
||||
req_span.set_attribute("http.status_code", response.status_code)
|
||||
if response.status_code == 200:
|
||||
return True
|
||||
except requests.exceptions.Timeout:
|
||||
retries += 1
|
||||
logger.warning(f"Upload timed out. Retrying {retries}/{max_retries}...")
|
||||
except Exception as e:
|
||||
retries += 1
|
||||
span.set_attribute("oss.error", str(e))
|
||||
logger.warning(f"Upload failed. Retrying {retries}/{max_retries}...")
|
||||
return False
|
||||
|
||||
|
||||
@ -37,23 +48,32 @@ def download_from_oss(url, file_path):
|
||||
:param Union[LiteralString, str, bytes] file_path: 文件路径
|
||||
:return bool: 是否成功
|
||||
"""
|
||||
logging.info("download_from_oss: %s", url)
|
||||
file_dir, file_name = os.path.split(file_path)
|
||||
if file_dir:
|
||||
if not os.path.exists(file_dir):
|
||||
os.makedirs(file_dir)
|
||||
max_retries = 5
|
||||
retries = 0
|
||||
while retries < max_retries:
|
||||
try:
|
||||
response = requests.get(url, timeout=15) # 设置超时时间
|
||||
with open(file_path, 'wb') as f:
|
||||
f.write(response.content)
|
||||
return True
|
||||
except requests.exceptions.Timeout:
|
||||
retries += 1
|
||||
logger.warning(f"Download timed out. Retrying {retries}/{max_retries}...")
|
||||
except Exception as e:
|
||||
logger.warning(f"Download failed. Retrying {retries}/{max_retries}...")
|
||||
retries += 1
|
||||
tracer = get_tracer(__name__)
|
||||
with tracer.start_as_current_span("download_from_oss") as span:
|
||||
span.set_attribute("file.url", url)
|
||||
span.set_attribute("file.path", file_path)
|
||||
logging.info("download_from_oss: %s", url)
|
||||
file_dir, file_name = os.path.split(file_path)
|
||||
if file_dir:
|
||||
if not os.path.exists(file_dir):
|
||||
os.makedirs(file_dir)
|
||||
max_retries = 5
|
||||
retries = 0
|
||||
while retries < max_retries:
|
||||
try:
|
||||
with tracer.start_as_current_span("download_from_oss.request") as req_span:
|
||||
req_span.set_attribute("http.method", "GET")
|
||||
req_span.set_attribute("http.url", url)
|
||||
response = requests.get(url, timeout=15) # 设置超时时间
|
||||
req_span.set_attribute("http.status_code", response.status_code)
|
||||
with open(file_path, 'wb') as f:
|
||||
f.write(response.content)
|
||||
return True
|
||||
except requests.exceptions.Timeout:
|
||||
retries += 1
|
||||
logger.warning(f"Download timed out. Retrying {retries}/{max_retries}...")
|
||||
except Exception as e:
|
||||
retries += 1
|
||||
span.set_attribute("oss.error", str(e))
|
||||
logger.warning(f"Download failed. Retrying {retries}/{max_retries}...")
|
||||
return False
|
||||
|
Reference in New Issue
Block a user