Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import socket
import threading
from contextlib import closing
from wsgiref.simple_server import make_server
from wsgiref.simple_server import make_server, WSGIRequestHandler

from . import core
try:
Expand All @@ -18,7 +18,6 @@
from urlparse import parse_qs, urlparse
except ImportError:
# Python 3
unicode = str
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
from urllib.request import build_opener, Request, HTTPHandler
Expand All @@ -45,13 +44,18 @@ def prometheus_app(environ, start_response):
return prometheus_app


class _SilentHandler(WSGIRequestHandler):
"""WSGI handler that does not log requests."""

def log_message(self, format, *args):
"""Log nothing."""


def start_wsgi_server(port, addr='', registry=core.REGISTRY):
"""Starts a WSGI server for prometheus metrics as a daemon thread."""
class PrometheusMetricsServer(threading.Thread):
def run(self):
httpd = make_server(addr, port, make_wsgi_app(registry))
httpd.serve_forever()
t = PrometheusMetricsServer()
app = make_wsgi_app(registry)
httpd = make_server(addr, port, app, handler_class=_SilentHandler)
t = threading.Thread(target=httpd.serve_forever)
t.daemon = True
t.start()

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


class MetricsHandler(BaseHTTPRequestHandler):
"""HTTP handler that gives metrics from ``core.REGISTRY``."""

def do_GET(self):
registry = core.REGISTRY
params = parse_qs(urlparse(self.path).query)
Expand All @@ -92,18 +98,17 @@ def do_GET(self):
self.wfile.write(output)

def log_message(self, format, *args):
return
"""Log nothing."""


class _ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
"""Thread per request HTTP server."""


def start_http_server(port, addr=''):
"""Starts an HTTP server for prometheus metrics as a daemon thread"""
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
pass
class PrometheusMetricsServer(threading.Thread):
def run(self):
httpd = ThreadingSimpleServer((addr, port), MetricsHandler)
httpd.serve_forever()
t = PrometheusMetricsServer()
httpd = _ThreadingSimpleServer((addr, port), MetricsHandler)
t = threading.Thread(target=httpd.serve_forever)
t.daemon = True
t.start()

Expand Down Expand Up @@ -263,6 +268,7 @@ def _use_gateway(method, gateway, job, registry, grouping_key, timeout, handler)
handler(url=url, method=method, timeout=timeout,
headers=headers, data=data)()


def instance_ip_grouping_key():
'''Grouping key with instance set to the IP Address of this host.'''
with closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as s:
Expand Down