HTTP¶
mite_http is mite’s HTTP integration. It installs an async HTTP client onto
the journey context, built on top of httpperf, and automatically
reports timing metrics for every request.
Install: pip install mite[http] (no functional change today — every mite
install already includes it; this will matter starting in mite 4.0).
Basic usage¶
from mite_http import mite_http
@mite_http
async def journey(ctx):
async with ctx.transaction("Get homepage"):
response = await ctx.http.get("https://example.com")
The @mite_http decorator installs ctx.http, an HTTP session, for the
duration of the journey call. Naming the transaction matters: the transaction
name becomes the transaction label on every metric the request produces, so
mite_http_response_time_seconds{transaction="Get homepage"} is how you’d find
this request’s latency later.
Making requests¶
ctx.http exposes one async method per HTTP verb:
await ctx.http.get(url, ...)
await ctx.http.post(url, ...)
await ctx.http.put(url, ...)
await ctx.http.patch(url, ...)
await ctx.http.delete(url, ...)
await ctx.http.head(url, ...)
await ctx.http.options(url, ...)
All of them accept the same keyword arguments:
Keyword |
Meaning |
|---|---|
|
a mapping of request headers |
|
a mapping of cookies to send with the request |
|
basic auth credentials |
|
a request body, |
|
a JSON-serializable object; mutually exclusive with |
|
client certificate |
|
follow redirects automatically (default |
|
redirect limit before raising (default |
await ctx.http.post(
"https://example.com/users",
json={"username": "test"},
headers={"Authorization": "Bearer token"},
)
JSON is encoded and decoded with ujson.
json= bodies are formatted exactly as the standard library’s json.dumps
does by default ({"username": "test"}, with spaces after , and :, and
non-ASCII characters escaped), so they match what earlier versions of mite sent.
The response¶
Each call returns a response object with:
status_codetext— decoded bodyjson()— body parsed as JSONbody— raw bytesheaderscookiesencodingurl— the final URL after any redirectshistory— the chain of responses that preceded a redirect
Metrics emitted¶
Every request sends an http_metrics message, which is what the registered
stats processors (below) turn into Prometheus series:
Field |
Meaning |
|---|---|
|
when the request started (wall clock, seconds as a float) |
|
the URL actually requested |
|
HTTP status code |
|
curl: DNS lookup complete, from the start of the transfer |
|
curl: TCP connect complete, from the start of the transfer; 0 when a connection was reused |
|
curl: TLS handshake complete, from the start of the transfer; 0 for plain http and reused connections |
|
curl: ready to send the request (curl’s |
|
curl: first response byte received (curl’s |
|
curl: transfer complete, from the start of the transfer |
|
resolved IP the request went to |
|
HTTP method used, e.g. |
|
bytes downloaded |
|
bytes uploaded |
|
time waiting in httpperf’s queue for a connection, under the connection limits; not part of the other timings |
|
time the request spent waiting on Python (before it was sent and after it finished); if this grows, the runner is the bottleneck |
|
curl: request fully sent, from the start of the transfer; |
|
protocol negotiated: |
Stats and tuning¶
mite_http registers two stats processors under mite_stats (see
Customizing miteʼs stats).
mite_http (on by default)¶
Stat |
Type |
Labels |
What it shows |
|---|---|---|---|
|
counter |
|
Number of responses. Use it for request rates and error rates by status code. |
|
histogram |
|
|
mite_http_extended (opt-in)¶
Everything curl and httpperf measure beyond the overall response time, broken down
so you can see where the time goes. They are off by default, because each adds a
histogram series per transaction. Turn them on by naming the processor in
mite stats --stats-include-processors, alongside the others you want, for
example --stats-include-processors=mite,mite_http,mite_http_extended.
All are histograms labelled by transaction: timings in seconds, sizes in bytes.
curl’s timings. Like total_time, each is measured from the start of the
transfer, so they add up: DNS ≤ connect ≤ TLS ≤ pretransfer ≤ posttransfer ≤ first
byte ≤ total. Subtract neighbouring values to get the length of one phase.
start → DNS done → TCP connected → TLS done → ready to send → request sent → first byte → done
dns_time connect_time tls_time pretransfer posttransfer first_byte total_time
Stat |
What it shows |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Measured by httpperf, outside curl’s timings. Neither is included in
total_time or in the timings above.
Stat |
What it shows |
|---|---|
|
|
|
|
Sizes, in bytes. Their buckets are set separately with
MITE_HTTP_SIZE_HISTOGRAM_BUCKETS (default 128 B to 64 MiB, in powers of 4).
Stat |
What it shows |
|---|---|
|
|
|
|
The histograms’ bucket boundaries can be overridden with the
MITE_HTTP_HISTOGRAM_BUCKETS environment variable, a comma-separated list of
floats (seconds):
export MITE_HTTP_HISTOGRAM_BUCKETS=0.01,0.05,0.1,0.5,1,5
The default buckets run from 100 microseconds to 64 seconds.
Connection settings¶
Each @mite_http journey gets its own httpperf Session, and every journey in a
runner shares one httpperf Engine. Both are configured through mite config, so set
them with MITE_CONF_ environment variables, for example
MITE_CONF_http_version=HTTP/1.1. See the
httpperf connections guide
for what each option does.
Engine options are read once, when a runner makes its first request:
Config key |
Default |
httpperf |
|---|---|---|
|
|
|
|
|
|
|
|
|
Session options are read at the start of each journey, so changes apply to the next journeys:
Config key |
Default |
httpperf |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
none |
|
|
curl’s default |
|
|
|
|
The defaults keep the connection behaviour of mite’s old acurl fork (one shared
pool, TLS session tickets per journey). HTTP/2 is negotiated over https where the
server offers it, falling back to HTTP/1.1, as before; the difference is that
requests to an HTTP/2 server now wait to share one connection (curl’s
PIPEWAIT) rather than each opening their own. HTTP/3 has no fallback and
needs a libcurl built with HTTP/3 support. The session is closed when the journey
ends, including when it raises.
Custom metrics per request¶
Anything set on ctx.http.additional_metrics is merged into the next
http_metrics message:
ctx.http.additional_metrics = {"cache_status": "miss"}
await ctx.http.get(url)
For more control, ctx.http.set_response_callback(cb) registers a callback
invoked with every response: cb(response, additional_metrics), where
additional_metrics is the same dict described above, so the callback can
inspect the response before deciding what to add.
Reproducing a request with curl¶
resp.request.to_curl() returns a curl command line that repeats the request:
method, URL, headers, body, basic auth, and the cookies that would have been
sent (those passed to the request plus those in the journey’s cookie jar for the
URL). mite_http.to_curl(resp) does the same.
logger.info("Reproduce with: %s", resp.request.to_curl())
The cookie jar is read when you call it, so call it while the journey is running, for example in a response callback. After the journey has ended only the request’s own cookies are included.
Responses from ctx.http (and those passed to response callbacks) are thin
wrappers around httpperf’s Response that add this method and keep cookies
as the whole jar (see Cookies); every other attribute is
httpperf’s own.
See also¶
httpperf — the libcurl wrapper
mite_httpis built onOpenTelemetry —
mite_http_traced, a drop-in replacement for@mite_httpthat additionally creates OpenTelemetry spansCustomizing miteʼs stats — the full list of registered stats processors