Changeset View
Changeset View
Standalone View
Standalone View
swh/counters/api/server.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import os | import os | ||||
from typing import Any, Dict | from typing import Any, Dict, Optional | ||||
from swh.core import config | from swh.core import config | ||||
from swh.core.api import RPCServerApp | from swh.core.api import RPCServerApp | ||||
from swh.counters import get_counters, get_history | from swh.counters import get_counters, get_history | ||||
from swh.counters.interface import CountersInterface, HistoryInterface | from swh.counters.interface import CountersInterface, HistoryInterface | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
app = None | app: Optional[RPCServerApp] = None | ||||
def make_app(config: Dict[str, Any]) -> RPCServerApp: | def make_app(config: Dict[str, Any]) -> RPCServerApp: | ||||
"""Initialize the remote api application. | """Initialize the remote api application. | ||||
""" | """ | ||||
app = RPCServerApp( | app = RPCServerApp( | ||||
__name__, | __name__, | ||||
backend_class=CountersInterface, | backend_class=CountersInterface, | ||||
backend_factory=lambda: get_counters(**config["counters"]), | backend_factory=lambda: get_counters(**config["counters"]), | ||||
) | ) | ||||
app.add_backend_class( | |||||
backend_class=HistoryInterface, | |||||
backend_factory=lambda: get_history(**config["history"]), | |||||
) | |||||
handler = logging.StreamHandler() | handler = logging.StreamHandler() | ||||
app.logger.addHandler(handler) | app.logger.addHandler(handler) | ||||
app.config["counters"] = get_counters(**config["counters"]) | app.config["counters"] = get_counters(**config["counters"]) | ||||
if "history" in config: | |||||
app.add_backend_class( | |||||
backend_class=HistoryInterface, | |||||
backend_factory=lambda: get_history(**config["history"]), | |||||
) | |||||
app.config["history"] = get_history(**config["history"]) | |||||
vlorentz: hmm... this means we'll get a 404 if there's an option missing in the config. I'd rather have… | |||||
ardumontUnsubmitted Not Done Inline ActionsWhat happens if we install the route line 43 in this conditional here? ardumont: What happens if we install the route line 43 in this conditional here? | |||||
vsellierAuthorUnsubmitted Done Inline Actions
I wrote that initially but make a rollback with no real reasons. I will try but it should be ok vsellier: > What happens if we install the route line 43 in this conditional here?
I wrote that… | |||||
app.add_url_rule("/", "index", index) | app.add_url_rule("/", "index", index) | ||||
app.add_url_rule("/metrics", "metrics", get_metrics) | app.add_url_rule("/metrics", "metrics", get_metrics) | ||||
app.add_url_rule( | |||||
"/counters_history/<filename>", "history", get_history_file_content | |||||
) | |||||
return app | return app | ||||
def index(): | def index(): | ||||
return "SWH Counters API server" | return "SWH Counters API server" | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | for collection in counters.get_counters(): | ||||
line = 'swh_archive_object_total{col="value", object_type="%s"} %s' % ( | line = 'swh_archive_object_total{col="value", object_type="%s"} %s' % ( | ||||
collection_name, | collection_name, | ||||
value, | value, | ||||
) | ) | ||||
response.append(line) | response.append(line) | ||||
response.append("") | response.append("") | ||||
return "\n".join(response) | return "\n".join(response) | ||||
def get_history_file_content(filename: str): | |||||
assert app is not None | |||||
return app.config["history"].get_history(filename) |
hmm... this means we'll get a 404 if there's an option missing in the config. I'd rather have an explicit error, even if it's a 500.