Skip to content

Commit

Permalink
Integration of Web UI (#14)
Browse files Browse the repository at this point in the history
* feat(api): bump v0.2.0 (#minor) app version

Signed-off-by: hayk96 <hayko5999@gmail.com>

---------

Signed-off-by: hayk96 <hayko5999@gmail.com>
  • Loading branch information
hayk96 authored May 2, 2024
1 parent e2beb0a commit 31ee8f1
Show file tree
Hide file tree
Showing 17 changed files with 1,370 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.2.0 / 2024-05-02

* [ENHANCEMENT] Added support of Web UI for better management of the Prometheus rules through UI. #14
* [CHANGE] Updated documentation

## 0.1.5 / 2024-02-25

* [ENHANCEMENT] Added support for exposing Prometheus metrics. The corresponding metrics are available under the path
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<li><a href="#api-model">API model</a></li>
<li><a href="#configuration">Configuration</a></li>
<li><a href="#grafana-dashboard">Grafana dashboard</a></li>
<li><a href="#web-ui">Web UI</a></li>
<li><a href="#roadmap">Roadmap</a></li>
<li><a href="#author-and-maintainer">Author and Maintainer</a></li>
<li><a href="#licence">License</a></li>
Expand Down Expand Up @@ -137,6 +138,8 @@ optional arguments:
rule files will be created with this suffix
--log.level {debug,info,warning,error}
only log messages with the given severity or above. One of: [debug, info, warning, error]
--web.enable-ui {true,false}
enable web management UI
required parameters:
--rule.path RULE.PATH
Expand All @@ -151,6 +154,14 @@ required parameters:
Grafana dashboard JSON model is available [here](https://github.com/hayk96/prometheus-api/tree/main/grafana/dashboard.json).
![](docs/images/dashboard.png)

<!--Web UI -->
## Web UI
The web user interface is disabled by default. As the prometheus-api project is specifically designed to extend the APIs
available to Prometheus, the UI serves as an optional add-on feature. It offers a convenient and flexible way to manage
Prometheus rules. To enable the web UI, the `--web.enable-ui=true` flag must be passed at the startup of the application.

![](docs/images/ui.png)

<!-- ROADMAP -->
## Roadmap

Expand Down
Binary file added docs/.DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion docs/examples/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ services:
- "./rules:/app/rules:rw"
command:
- --prom.addr=http://prometheus:9090
- --rule.path=/app/rules
- --rule.path=/app/rules
- --web.enable-ui=true
1 change: 1 addition & 0 deletions docs/examples/kubernetes/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ server:
- --prom.addr=http://localhost:9091
- --web.listen-address=0.0.0.0:9090
- --rule.path=/rules
- --web.enable-ui=true
ports:
- containerPort: 9090
volumeMounts:
Expand Down
Binary file added docs/images/.DS_Store
Binary file not shown.
Binary file modified docs/images/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from fastapi.middleware.cors import CORSMiddleware
from src.utils.schemas import rule_schema_status
from src.utils.arguments import arg_parser
from src.api.v1.api import api_router
Expand All @@ -9,6 +10,7 @@
import uvicorn
import sys


args = arg_parser()
prom_addr, rule_path = args.get("prom.addr"), args.get("rule.path")
host, port = args.get("web.listen_address").split(":")
Expand All @@ -28,6 +30,14 @@ def custom_openapi_wrapper():
app = FastAPI(swagger_ui_parameters={"defaultModelsExpandDepth": -1})
app.openapi = custom_openapi_wrapper
metrics(app)
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router)


Expand Down
3 changes: 2 additions & 1 deletion src/api/v1/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .. v1.endpoints import reverse_proxy, rules
from .. v1.endpoints import reverse_proxy, rules, web
from fastapi import APIRouter

api_router = APIRouter()
api_router.include_router(rules.router, prefix="/api/v1")
api_router.include_router(web.router, prefix="")
api_router.add_route("/{path:path}", reverse_proxy._reverse_proxy, ["GET", "POST", "PUT"])
40 changes: 40 additions & 0 deletions src/api/v1/endpoints/web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from starlette.responses import FileResponse
from fastapi.responses import HTMLResponse
from src.utils.arguments import arg_parser
from src.utils.log import logger
from fastapi import APIRouter
from fastapi import Request

router = APIRouter()

if arg_parser().get("web.enable_ui") == "true":
rules_management = "ui/rules-management"
logger.info("Starting web management UI")

@router.get("/", response_class=HTMLResponse,
description="Renders home page of this application",
include_in_schema=False)
async def homepage():
return FileResponse("ui/homepage/index.html")

@router.get("/rules-management",
description="Renders rules management HTML page of this application",
include_in_schema=False)
async def rules_management_page():
return FileResponse(f"{rules_management}/index.html")

@router.get(
"/rules-management/{path}",
description="Returns JavaScript and CSS files of the rules management page",
include_in_schema=False)
async def rules_management_files(path, request: Request):
if path in ["script.js", "style.css"]:
return FileResponse(f"{rules_management}/{path}")
sts, msg = "404", "Not Found"
logger.info(
msg=msg,
extra={
"status": sts,
"method": request.method,
"request_path": request.url.path})
return f"{sts} {msg}"
9 changes: 9 additions & 0 deletions src/utils/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ def arg_parser() -> dict:
help="only log messages with the given severity or above. One of: [debug, info, warning, error]"
)

parser.add_argument(
"--web.enable-ui",
required=False,
type=str,
default="false",
choices=["true", "false"],
help="enable web management UI"
)

return parser.parse_args().__dict__
2 changes: 1 addition & 1 deletion src/utils/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def openapi(app: FastAPI):
"providing additional features and addressing its limitations. "
"Running as a sidecar alongside the Prometheus server enables "
"users to extend the capabilities of the API.",
version="0.1.5",
version="0.2.0",
contact={
"name": "Hayk Davtyan",
"url": "https://hayk96.github.io",
Expand Down
Loading

0 comments on commit 31ee8f1

Please sign in to comment.