Skip to content

Commit

Permalink
Enable graceful shutdown for start_{http,wsgi}_server (#999)
Browse files Browse the repository at this point in the history
Signed-off-by: Antti Rasinen <[email protected]>
  • Loading branch information
arsatiki authored Feb 1, 2024
1 parent 9dd6b0d commit b9edc43
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/content/exporting/http/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -226,6 +226,8 @@ class TmpServer(ThreadingWSGIServer):
t.daemon = True
t.start()

return httpd, t


start_http_server = start_wsgi_server

Expand Down

0 comments on commit b9edc43

Please sign in to comment.