代码拉取完成,页面将自动刷新
import inspect
import logging
import uuid
from fastapi import FastAPI, Request, Response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
EXCLUDED_USER_AGENTS = {'bot', 'spider', 'crawler', 'monitor', 'curl',
'wget', 'python-requests', 'kuma', 'health check'}
def start_monitor(app: FastAPI) -> None:
try:
import prometheus_client
except ModuleNotFoundError:
logging.info('Prometheus not installed, skipping monitoring')
return
visits = prometheus_client.Counter('nicegui_page_visits', 'Number of real page visits',
['path', 'session', 'origin'])
class PrometheusMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
if 'id' not in request.session:
request.session['id'] = str(uuid.uuid4())
response = await call_next(request)
if response.headers.get('x-nicegui-content') == 'page':
agent = request.headers.get('user-agent', 'unknown').lower()
# ignore monitoring, web crawlers and the like
if not any(s in agent for s in EXCLUDED_USER_AGENTS):
origin_url = request.headers.get('referer', 'unknown')
visits.labels(request.get('path'), request.session['id'], origin_url).inc()
return response
if inspect.stack()[-2].filename.endswith('spawn.py'):
prometheus_client.start_http_server(9062)
app.add_middleware(PrometheusMiddleware)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。