接入 OTel 链路追踪 + 镜像默认启用
抓取-解析链路原本只有日志,出现"抓回内容但解析不出预期字段"时定位慢。 接入 OpenTelemetry traces(FastAPI/httpx 自动 + 手写 fetch/parse span), 解析失败时把页面 HTML 作为 span event 上报,便于事后复现。 Dockerfile 默认开启,镜像一启动即导出到自建 OTLP endpoint。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -27,6 +27,7 @@ from app.scraping.services.site_session import SiteSession
|
||||
from app.shared.api import register_exception_handlers
|
||||
from app.shared.config import get_settings
|
||||
from app.shared.logging_setup import configure_logging
|
||||
from app.shared.telemetry import instrument_app, setup_telemetry, shutdown_telemetry
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -56,6 +57,9 @@ async def lifespan(app: FastAPI):
|
||||
app.state.container = container
|
||||
|
||||
configure_logging(container.settings)
|
||||
# telemetry 必须在创建任何 httpx.AsyncClient 之前完成 setup,否则 httpx
|
||||
# 自动 instrumentation 抓不到现有客户端的请求。
|
||||
setup_telemetry(container.settings, service_name="rakuten-scraping")
|
||||
logger.info("抓取服务启动:%s:%s", container.settings.app_host, container.settings.app_port)
|
||||
logger.info("日志级别:%s", container.settings.log_level)
|
||||
logger.info("当前环境:%s", container.settings.app_env)
|
||||
@@ -67,6 +71,7 @@ async def lifespan(app: FastAPI):
|
||||
await container.rakuma_session.close()
|
||||
await container.site_session.close()
|
||||
await container.browser_fallback.close()
|
||||
shutdown_telemetry()
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
@@ -76,6 +81,7 @@ def create_app() -> FastAPI:
|
||||
app.include_router(scrape_router)
|
||||
app.include_router(rakuma_router)
|
||||
register_exception_handlers(app)
|
||||
instrument_app(app)
|
||||
return app
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user