mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-11-22 09:39:30 +00:00
this project is actively using optional imports, which leads to duplicate handling here and there. This commit implements logic in special method to reduce complexity and simplify tests paths
69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
#
|
|
# Copyright (c) 2021-2026 ahriman team.
|
|
#
|
|
# This file is part of ahriman
|
|
# (see https://github.com/arcan1s/ahriman).
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
from aiohttp.typedefs import Middleware
|
|
from aiohttp.web import HTTPNotFound, Request, Response, StreamResponse, middleware
|
|
|
|
from ahriman.core.module_loader import optional_module
|
|
from ahriman.web.middlewares import HandlerType
|
|
|
|
|
|
__all__ = [
|
|
"metrics",
|
|
"metrics_handler",
|
|
]
|
|
|
|
|
|
aiohttp_openmetrics = optional_module("aiohttp_openmetrics")
|
|
|
|
|
|
async def metrics(request: Request) -> Response:
|
|
"""
|
|
handler for returning metrics
|
|
|
|
Args:
|
|
request(Request): request object
|
|
|
|
Returns:
|
|
Response: response object
|
|
|
|
Raises:
|
|
HTTPNotFound: endpoint is disabled
|
|
"""
|
|
if not aiohttp_openmetrics:
|
|
raise HTTPNotFound
|
|
return await aiohttp_openmetrics.metrics(request)
|
|
|
|
|
|
def metrics_handler() -> Middleware:
|
|
"""
|
|
middleware for metrics support
|
|
|
|
Returns:
|
|
Middleware: middleware function to handle server metrics
|
|
"""
|
|
if aiohttp_openmetrics:
|
|
return aiohttp_openmetrics.metrics_middleware
|
|
|
|
@middleware
|
|
async def handle(request: Request, handler: HandlerType) -> StreamResponse:
|
|
return await handler(request)
|
|
|
|
return handle
|