问题描述
我在实现 Opencensus、登录 Python 和 FastAPI 时遇到问题。我想在 Azure 中记录对 Application Insights 的传入请求,因此我在 the Microsoft docs 和 this Github post 之后的代码中添加了一个 FastAPI 中间件:
propagator = TraceContextPropagator()
@app.middleware('http')
async def middleware_opencensus(request: Request,call_next):
tracer = Tracer(
span_context=propagator.from_headers(request.headers),exporter=AzureExporter(connection_string=os.environ['APPLICATION_INSIGHTS_CONNECTION_STRING']),sampler=AlwaysOnSampler(),propagator=propagator)
with tracer.span('main') as span:
span.span_kind = SpanKind.SERVER
tracer.add_attribute_to_current_span(HTTP_HOST,request.url.hostname)
tracer.add_attribute_to_current_span(HTTP_METHOD,request.method)
tracer.add_attribute_to_current_span(HTTP_PATH,request.url.path)
tracer.add_attribute_to_current_span(HTTP_ROUTE,request.url.path)
tracer.add_attribute_to_current_span(HTTP_URL,str(request.url))
response = await call_next(request)
tracer.add_attribute_to_current_span(HTTP_STATUS_CODE,response.status_code)
return response
这在本地运行时非常有效,并且所有对 api 的传入请求都记录到 Application Insights。然而,由于实施了 Opencensus,当部署在 Azure 上的容器实例中时,几天后(大约 3 天)就会出现一个问题,看起来像是发生了一些递归日志记录问题(每秒 30.000 条日志!),即声明 Queue is full. Dropping telemetry
,在疯狂记录几个小时后最终崩溃之前:
我们定义日志处理程序的 logger.py
文件如下:
import logging.config
import os
import tqdm
from pathlib import Path
from opencensus.ext.azure.log_exporter import AzureLogHandler
class TqdmLoggingHandler(logging.Handler):
"""
Class for enabling logging during a process with a tqdm progress bar.
Using this handler logs will be put above the progress bar,pushing the
process bar down instead of replacing it.
"""
def __init__(self,level=logging.NOTSET):
super().__init__(level)
self.formatter = logging.Formatter(fmt='%(asctime)s <%(name)s> %(levelname)s: %(message)s',datefmt='%d-%m-%Y %H:%M:%s')
def emit(self,record):
try:
msg = self.format(record)
tqdm.tqdm.write(msg)
self.flush()
except (KeyboardInterrupt,SystemExit):
raise
except:
self.handleError(record)
logging_conf_path = Path(__file__).parent
logging.config.fileConfig(logging_conf_path / 'logging.conf')
logger = logging.getLogger(__name__)
logger.addHandler(TqdmLoggingHandler(logging.DEBUG)) # Add tqdm handler to root logger to replace the stream handler
if os.getenv('APPLICATION_INSIGHTS_CONNECTION_STRING'):
logger.addHandler(AzureLogHandler(connection_string=os.environ['APPLICATION_INSIGHTS_CONNECTION_STRING']))
warning_level_loggers = ['urllib3','requests']
for lgr in warning_level_loggers:
logging.getLogger(lgr).setLevel(logging.WARNING)
有没有人知道这个问题的原因是什么,或者有人遇到过类似的问题?由于日志记录量过快,我不知道“第一个”错误日志是什么。
如果需要其他信息,请告诉我。
提前致谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)