Silent WSGI server like a prometheus server

Add a silent handler class to avoid the oslo-metrics prometheus server
to log every request it receive.

This handler will be enabled by default thanks to the new
wsgi_silent_server option.

As visible in [1], even prometheus client default server is using this
trick to avoid too much verbosity.

This makes oslo-metrics behave much more like a regular prometheus
exporter (e.g. node-exporter).

[1] c89624f784/prometheus_client/exposition.py (L233)

Change-Id: I4a57e10a1b0057372acb8115ccad936aecd6573a
Signed-off-by: Arnaud Morin <arnaud.morin@gmail.com>
This commit is contained in:
Guillaume Espanel
2023-05-09 16:36:54 +02:00
committed by Arnaud Morin
parent 276d34db0e
commit 68a6f51dca
2 changed files with 24 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ import socket
import sys
import threading
from wsgiref.simple_server import make_server
from wsgiref.simple_server import WSGIRequestHandler
from oslo_config import cfg
from oslo_log import log as logging
@@ -37,6 +38,10 @@ oslo_metrics_configs = [
help='Port number to expose metrics in prometheus format.'),
cfg.IntOpt('metrics_socket_perm', default=0o660,
help='Permission set to the unix domain socket file'),
cfg.BoolOpt('wsgi_silent_server', default=True,
help='Whether to silence the WSGI server. If disabled, the '
'WSGI server will print all requests it receives on '
'STDOUT. This could be very verbose.'),
]
cfg.CONF.register_opts(oslo_metrics_configs, group='oslo_metrics')
@@ -85,6 +90,13 @@ class MetricsListener():
self.start = False
class _SilentHandler(WSGIRequestHandler):
"""WSGI handler that does not log requests."""
def log_message(self, format, *args):
"""Log nothing."""
httpd = None
@@ -109,7 +121,11 @@ def main():
app = make_wsgi_app()
try:
global httpd
httpd = make_server('', CONF.oslo_metrics.prometheus_port, app)
if cfg.CONF.oslo_metrics.wsgi_silent_server:
httpd = make_server('', CONF.oslo_metrics.prometheus_port, app,
handler_class=_SilentHandler)
else:
httpd = make_server('', CONF.oslo_metrics.prometheus_port, app)
signal.signal(signal.SIGTERM, handle_sigterm)
httpd.serve_forever()
except KeyboardInterrupt:

View File

@@ -0,0 +1,7 @@
---
other:
- |
Add a new option ``wsgi_silent_server`` to enable/disable STDOUT logs on
oslo.metrics wsgi prometheus exporter.
By default this new option will be set to ``True``, which means logs are
**not** going to be printed anymore.