8000 Make WSGI metrics server not log requests (#203) · sonlinux/client_python@6abff16 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6abff16

Browse files
bz2brian-brazil
authored andcommitted
Make WSGI metrics server not log requests (prometheus#203)
Logging using start_http_server() and start_wsgi_server() is now consistent. Also simplifies and documents the server creation logic without otherwise changing behaviour.
1 parent d301e2f commit 6abff16

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

prometheus_client/exposition.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import socket
88
import threading
99
from contextlib import closing
10-
from wsgiref.simple_server import make_server
10+
from wsgiref.simple_server import make_server, WSGIRequestHandler
1111

1212
from . import core
1313
try:
@@ -18,7 +18,6 @@
1818
from urlparse import parse_qs, urlparse
1919
except ImportError:
2020
# Python 3
21-
unicode = str
2221
from http.server import BaseHTTPRequestHandler, HTTPServer
2322
from socketserver import ThreadingMixIn
2423
from urllib.request import build_opener, Request, HTTPHandler
@@ -45,13 +44,18 @@ def prometheus_app(environ, start_response):
4544
return prometheus_app
4645

4746

47+
class _SilentHandler(WSGIRequestHandler):
48+
"""WSGI handler that does not log requests."""
49+
50+
def log_message(self, format, *args):
51+
"""Log nothing."""
52+
53+
4854
def start_wsgi_server(port, addr='', registry=core.REGISTRY):
4955
"""Starts a WSGI server for prometheus metrics as a daemon thread."""
50-
class PrometheusMetricsServer(threading.Thread):
51-
def run(self):
52-
httpd = make_server(addr, port, make_wsgi_app(registry))
53-
httpd.serve_forever()
54-
t = PrometheusMetricsServer()
56+
app = make_wsgi_app(registry)
57+
httpd = make_server(addr, port, app, handler_class=_SilentHandler)
58+
t = threading.Thread(target=httpd.serve_forever)
5559
t.daemon = True
5660
t.start()
5761

@@ -76,6 +80,8 @@ def generate_latest(registry=core.REGISTRY):
7680

7781

7882
class MetricsHandler(BaseHTTPRequestHandler):
83+
"""HTTP handler that gives metrics from ``core.REGISTRY``."""
84+
7985
def do_GET(self):
8086
registry = core.REGISTRY
8187
params = parse_qs(urlparse(self.path).query)
@@ -92,18 +98,17 @@ def do_GET(self):
9298
self.wfile.write(output)
9399

94100
def log_message(self, format, *args):
95-
return
101+
"""Log nothing."""
102+
103+
104+
class _ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
105+
"""Thread per request HTTP server."""
96106

97107

98108
def start_http_server(port, addr=''):
99109
"""Starts an HTTP server for prometheus metrics as a daemon thread"""
100-
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
101-
pass
102-
class PrometheusMetricsServer(threading.Thread):
103-
def run(self):
104-
httpd = ThreadingSimpleServer((addr, port), MetricsHandler)
105-
httpd.serve_forever()
106-
t = PrometheusMetricsServer()
110+
httpd = _ThreadingSimpleServer((addr, port), MetricsHandler)
111+
t = threading.Thread(target=httpd.serve_forever)
107112
t.daemon = True
108113
t.start()
109114

@@ -263,6 +268,7 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
263268
handler(url=url, method=method, timeout=timeout,
264269
headers=headers, data=data)()
265270

271+
266272
def instance_ip_grouping_key():
267273
'''Grouping key with instance set to the IP Address of this host.'''
268274
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as s:

0 commit comments

Comments
 (0)
0