浏览代码

Remove Prometheus tracking (#4766)

## Summary
- remove `prometheus.py`
- drop Prometheus middleware from `main.py`
- stop installing `prometheus_client` and exposing the Prometheus port
in `fly.dockerfile`
- stop copying the removed file in `release.dockerfile`

---------

Co-authored-by: Falko Schindler <falko@zauberzeug.com>
Rodja Trappe 1 天之前
父节点
当前提交
be0ae72aa9
共有 5 个文件被更改,包括 2 次插入50 次删除
  1. 0 2
      fly.dockerfile
  2. 0 4
      fly.toml
  3. 1 4
      main.py
  4. 0 39
      prometheus.py
  5. 1 1
      release.dockerfile

+ 0 - 2
fly.dockerfile

@@ -12,7 +12,6 @@ RUN pip install \
     matplotlib \
     pandas \
     plotly \
-    prometheus_client \
     pyecharts \
     pytest \
     requests \
@@ -41,7 +40,6 @@ RUN sed -i "/\[tool.poetry\]/,/]/s/version = .*/version = \"$VERSION\"/" pyproje
 RUN pip install .
 
 EXPOSE 8080
-EXPOSE 9062
 
 COPY fly-entrypoint.sh /entrypoint.sh
 

+ 0 - 4
fly.toml

@@ -53,7 +53,3 @@ swap_size_mb = 2048
     path = "/status"
     protocol = "http"
     tls_skip_verify = false
-
-[[metrics]]
-  path = "/"
-  port = 9062

+ 1 - 4
main.py

@@ -7,13 +7,10 @@ from fastapi import HTTPException, Request
 from fastapi.responses import RedirectResponse
 from starlette.middleware.sessions import SessionMiddleware
 
-import prometheus
 from nicegui import app, ui
 from website import anti_scroll_hack, documentation, fly, main_page, svg
 
-prometheus.start_monitor(app)
-
-# session middleware is required for demo in documentation and prometheus
+# session middleware is required for demo in documentation
 app.add_middleware(SessionMiddleware, secret_key=os.environ.get('NICEGUI_SECRET_KEY', ''))
 
 on_fly = fly.setup()

+ 0 - 39
prometheus.py

@@ -1,39 +0,0 @@
-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)

+ 1 - 1
release.dockerfile

@@ -32,7 +32,7 @@ RUN python -m pip install \
 
 WORKDIR /app
 
-COPY main.py README.md prometheus.py ./
+COPY main.py README.md ./
 COPY examples ./examples
 COPY website ./website
 RUN mkdir /resources