Debian Codegraph @

docs/content/exporting/http/_index.md

raw symbols SCIP
1 ---
2 title: HTTP/HTTPS
3 weight: 1
4 ---
5
6 # HTTP
7
8 Metrics are usually exposed over HTTP, to be read by the Prometheus server.
9
10 The easiest way to do this is via `start_http_server`, which will start a HTTP
11 server in a daemon thread on the given port:
12
13 ```python
14 from prometheus_client import start_http_server
15
16 start_http_server(8000)
17 ```
18
19 Visit [http://localhost:8000/](http://localhost:8000/) to view the metrics.
20
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
30 To add Prometheus exposition to an existing HTTP server, see the `MetricsHandler` class
31 which provides a `BaseHTTPRequestHandler`. It also serves as a simple example of how
32 to write a custom endpoint.
33
34 # HTTPS
35
36 By default, the prometheus client will accept only HTTP requests from Prometheus.
37 To enable HTTPS, `certfile` and `keyfile` need to be provided. The certificate is
38 presented to Prometheus as a server certificate during the TLS handshake, and
39 the private key in the key file must belong to the public key in the certificate.
40
41 When HTTPS is enabled, you can enable mutual TLS (mTLS) by setting `client_auth_required=True`
42 (i.e. Prometheus is required to present a client certificate during TLS handshake) and the
43 client certificate including its hostname is validated against the CA certificate chain.
44
45 `client_cafile` can be used to specify a certificate file containing a CA certificate
46 chain that is used to validate the client certificate. `client_capath` can be used to
47 specify a certificate directory containing a CA certificate chain that is used to
48 validate the client certificate. If neither of them is provided, a default CA certificate
49 chain is used (see Python [ssl.SSLContext.load_default_certs()](https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_default_certs))
50
51 ```python
52 from prometheus_client import start_http_server
53
54 start_http_server(8000, certfile="server.crt", keyfile="server.key")
55 ```
56
57 # Supported HTTP methods
58
59 The prometheus client will handle the following HTTP methods and resources:
60
61 * `OPTIONS (any)` - returns HTTP status 200 and an 'Allow' header indicating the
62 allowed methods (OPTIONS, GET)
63 * `GET (any)` - returns HTTP status 200 and the metrics data
64 * `GET /favicon.ico` - returns HTTP status 200 and an empty response body. Some
65 browsers support this to display the returned icon in the browser tab.
66
67 Other HTTP methods than these are rejected with HTTP status 405 "Method Not Allowed"
68 and an 'Allow' header indicating the allowed methods (OPTIONS, GET).
69
70 Any returned HTTP errors are also displayed in the response body after a hash
71 sign and with a brief hint. Example:
72 ```
73 # HTTP 405 Method Not Allowed: XXX; use OPTIONS or GET
74 ```