Skip to content

Commit b9edc43

Browse files
authored
Enable graceful shutdown for start_{http,wsgi}_server (#999)
Signed-off-by: Antti Rasinen <antti.rasinen@relexsolutions.com>
1 parent 9dd6b0d commit b9edc43

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

docs/content/exporting/http/_index.md

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ start_http_server(8000)
1818

1919
Visit [http://localhost:8000/](http://localhost:8000/) to view the metrics.
2020

21+
The function will return the HTTP server and thread objects, which can be used
22+
to shutdown the server gracefully:
23+
24+
```python
25+
server, t = start_http_server(8000)
26+
server.shutdown()
27+
t.join()
28+
```
29+
2130
To add Prometheus exposition to an existing HTTP server, see the `MetricsHandler` class
2231
which provides a `BaseHTTPRequestHandler`. It also serves as a simple example of how
2332
to write a custom endpoint.

prometheus_client/exposition.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def start_wsgi_server(
210210
client_capath: Optional[str] = None,
211211
protocol: int = ssl.PROTOCOL_TLS_SERVER,
212212
client_auth_required: bool = False,
213-
) -> None:
213+
) -> Tuple[WSGIServer, threading.Thread]:
214214
"""Starts a WSGI server for prometheus metrics as a daemon thread."""
215215

216216
class TmpServer(ThreadingWSGIServer):
@@ -226,6 +226,8 @@ class TmpServer(ThreadingWSGIServer):
226226
t.daemon = True
227227
t.start()
228228

229+
return httpd, t
230+
229231

230232
start_http_server = start_wsgi_server
231233

0 commit comments

Comments
 (0)