Skip to content

Commit

Permalink
Merge branch 'feature/fastapi' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
motlib committed Jun 2, 2024
2 parents 4e7d2d0 + ccb77d4 commit 2dc0517
Show file tree
Hide file tree
Showing 12 changed files with 823 additions and 306 deletions.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ paho-mqtt = "*"
pydantic = "*"
pyyaml = "*"
logfmter = "*"
fastapi = "*"
uvicorn = "*"

[dev-packages]
black = "*"
Expand Down
910 changes: 767 additions & 143 deletions Pipfile.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions promqtt/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""FastAPI endpoint implementation"""

from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

from .app import prom_exp

api = FastAPI()


@api.get("/metrics")
async def get_metrics() -> PlainTextResponse:
"""Return the prometheus metrics data"""

return PlainTextResponse(prom_exp.render())
5 changes: 5 additions & 0 deletions promqtt/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Application context objects"""

from .promexp import PrometheusExporter

prom_exp = PrometheusExporter()
13 changes: 11 additions & 2 deletions promqtt/cfgmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@

from pydantic import BaseModel, ConfigDict, Field, model_validator

from .httpsrv.config import HttpServerConfig
from .promexp.types import MetricTypeEnum

# pylint: disable=too-few-public-methods
# pylit: disable=too-few-public-methods


class HttpServerConfig(BaseModel):
"""HTTP server related settings"""

model_config = ConfigDict(extra="forbid")

interface: str = Field("0.0.0.0", description="Interface address to listen on")

port: int = Field(8086, description="Port number")


class MqttModel(BaseModel):
Expand Down
9 changes: 0 additions & 9 deletions promqtt/httpsrv/__init__.py

This file was deleted.

18 changes: 0 additions & 18 deletions promqtt/httpsrv/config.py

This file was deleted.

49 changes: 0 additions & 49 deletions promqtt/httpsrv/httpd.py

This file was deleted.

35 changes: 0 additions & 35 deletions promqtt/httpsrv/reqhdlr.py

This file was deleted.

27 changes: 0 additions & 27 deletions promqtt/httpsrv/route.py

This file was deleted.

37 changes: 18 additions & 19 deletions promqtt/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""Application main implementation"""

import json
import logging
import os
import signal
import sys
from pathlib import Path
from threading import Thread

import uvicorn
import yaml
from logfmter import Logfmter
from logfmter.formatter import Logfmter

from .api import api
from .app import prom_exp
from .cfgmodel import MetricTypeEnum, PromqttConfig
from .httpsrv import HttpServer, Route
from .metadata import APPNAME, VERSION
from .promexp import PrometheusExporter
from .promqtt import MqttClient, MqttPrometheusBridge
Expand Down Expand Up @@ -77,7 +79,7 @@ def load_config(filename: Path) -> PromqttConfig:
return cfg


def main():
def main() -> None:
"""Application main function"""

# Set up logging
Expand All @@ -90,27 +92,24 @@ def main():
signal.signal(signal.SIGTERM, sigterm_handler)

# load configuration

cfgfile = Path(os.environ.get("PROMQTT_CONFIG", "promqtt.yml"))
cfg = load_config(cfgfile)

promexp = PrometheusExporter()
export_build_info(promexp, VERSION)

# Intialize and start the HTTP server
export_build_info(prom_exp, VERSION)

routes = [
Route("/metrics", "text/plain", promexp.render),
Route("/cfg", "application/json", lambda: json.dumps(cfg.dict(), indent=4)),
]
tmc = MqttPrometheusBridge(prom_exp, cfg=cfg)

httpsrv = HttpServer(cfg=cfg.http, routes=routes)
httpsrv.start_server_thread()
mqttclient = MqttClient(prom_exp, cfg.mqtt, tmc)
thread = Thread(target=mqttclient.loop_forever, daemon=True)
thread.start()

tmc = MqttPrometheusBridge(promexp, cfg=cfg)

mqttclient = MqttClient(promexp, cfg.mqtt, tmc)
mqttclient.loop_forever()
uvicorn.run(
api,
host=cfg.http.interface,
port=cfg.http.port,
log_level="info",
log_config=None,
)


if __name__ == "__main__":
Expand Down
9 changes: 5 additions & 4 deletions promqtt/promqtt/mqttclt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from typing import Any

import paho.mqtt.client as mqtt
from paho.mqtt.enums import CallbackAPIVersion
from paho.mqtt.enums import CallbackAPIVersion, MQTTErrorCode

from ..cfgmodel import MetricTypeEnum, MqttModel
from ..cfgmodel import MqttModel
from ..promexp import PrometheusExporter
from ..promexp.types import MetricTypeEnum
from .msg import Message

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -75,7 +76,7 @@ def _on_connect(
client: mqtt.Client,
userdata: Any,
flags: dict,
result: mqtt.MQTTErrorCode,
result: MQTTErrorCode,
) -> None:
"""Callback function called by the MQTT client to inform about establishing a
connection to a broker."""
Expand All @@ -101,7 +102,7 @@ def _on_connect(
)

def _on_disconnect(
self, client: mqtt.Client, userdata: Any, result: mqtt.MQTTErrorCode
self, client: mqtt.Client, userdata: Any, result: MQTTErrorCode
) -> None:
"""Callback function called by the MQTT client when the connection to a broker
is terminated."""
Expand Down

0 comments on commit 2dc0517

Please sign in to comment.