diff --git a/docs/content/exporting/http/_index.md b/docs/content/exporting/http/_index.md index b074a3bf..71edc7e3 100644 --- a/docs/content/exporting/http/_index.md +++ b/docs/content/exporting/http/_index.md @@ -18,6 +18,15 @@ start_http_server(8000) Visit [http://localhost:8000/](http://localhost:8000/) to view the metrics. +The function will return the HTTP server and thread objects, which can be used +to shutdown the server gracefully: + +```python +server, t = start_http_server(8000) +server.shutdown() +t.join() +``` + To add Prometheus exposition to an existing HTTP server, see the `MetricsHandler` class which provides a `BaseHTTPRequestHandler`. It also serves as a simple example of how to write a custom endpoint. diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index f2b7442b..30194dd8 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -210,7 +210,7 @@ def start_wsgi_server( client_capath: Optional[str] = None, protocol: int = ssl.PROTOCOL_TLS_SERVER, client_auth_required: bool = False, -) -> None: +) -> Tuple[WSGIServer, threading.Thread]: """Starts a WSGI server for prometheus metrics as a daemon thread.""" class TmpServer(ThreadingWSGIServer): @@ -226,6 +226,8 @@ class TmpServer(ThreadingWSGIServer): t.daemon = True t.start() + return httpd, t + start_http_server = start_wsgi_server