def create_prometheus_server(registry, port): """custom start method to fix problem descrbied in https://github.com/prometheus/client_python/issues/155""" app = exposition.make_wsgi_app(registry) httpd = exposition.make_server("", port, app, exposition.ThreadingWSGIServer, handler_class=exposition._SilentHandler) t = threading.Thread(target=httpd.serve_forever) t.daemon = True t.start() return httpd
def start_prometheus_server(port, prom_registry=registry.REGISTRY): app = exposition.make_wsgi_app(prom_registry) httpd = exposition.make_server( "", port, app, exposition.ThreadingWSGIServer, handler_class=exposition._SilentHandler # pylint: disable=protected-access ) t = threading.Thread(target=httpd.serve_forever) t.start() return httpd
def build_app() -> Flask: """ Creating Flask application in order to serve all Prometheus metrics :return: Flask """ curr_app = Flask("gordoserver_prometheus") registry = create_registry() curr_app.wsgi_app = DispatcherMiddleware( # type: ignore curr_app.wsgi_app, {"/metrics": make_wsgi_app(registry)} ) @curr_app.route("/healthcheck") def health_check(): return "", 200 return curr_app
def main(): logging.basicConfig(level=logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument("listenPort") parser.add_argument("host") parser.add_argument("user") parser.add_argument("password") args = parser.parse_args() collector = FRITZBoxCollector(args.host, args.user, args.password) REGISTRY.register(collector) app = make_wsgi_app() httpd = make_server('', int(args.listenPort), app) logger.info('FRITZ!Box Collector started') try: httpd.serve_forever() except KeyboardInterrupt: logger.info('FRITZ!Box Collector terminated')
def metrics(): return make_wsgi_app(current_app.prom_init.registry)
from wsgiref.simple_server import make_server, WSGIServer from prometheus_client import multiprocess from prometheus_client.exposition import make_wsgi_app from prometheus_client.multiprocess_exporter import start_archiver_thread from prometheus_client.registry import CollectorRegistry """ An entrypoint for the a multiprocess exporter using Python's built-in wsgiref implementation The reference wsgi implementation is not pre-fork, making it more suited for the InMemoryCollector than Gunicorn """ CLEANUP_INTERVAL = 5.0 registry = CollectorRegistry() multiprocess.InMemoryCollector(registry) app = make_wsgi_app(registry) parser = argparse.ArgumentParser(description="Starts a multiprocess prometheus exporter, running on wsgiref") parser.add_argument("--port", type=int, required=True) args = parser.parse_args() port = args.port class ExporterHttpServer(WSGIServer): """ An equivalent of the on_starting hook if running the multiprocess exporter without Gunicorn """ def server_activate(self): # WSGIServer is still an old-style class in python 2.7, preventing use of super() WSGIServer.server_activate(self) start_archiver_thread()
import adafruit_mcp3xxx.mcp3008 as MCP import board import busio import digitalio from adafruit_mcp3xxx.analog_in import AnalogIn from flask import Flask from prometheus_client.exposition import make_wsgi_app from prometheus_client.metrics import Gauge from werkzeug.middleware.dispatcher import DispatcherMiddleware # ----- temperature ----- spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI) cs = digitalio.DigitalInOut(board.D5) mcp = MCP.MCP3008(spi, cs) chan = AnalogIn(mcp, MCP.P0) # ----- end ----- # ----- web ----- app = Flask(__name__) random_gauge = Gauge("random_value", "A Random Value") temp = Gauge("temperature", "temp temperature") temp.set_function(str(chan.voltage)) reporting = DispatcherMiddleware(app, {'/metrics': make_wsgi_app()}) # ----- end -----