HTTP¶
mite_http is mite’s HTTP integration. It installs an async HTTP client onto
the journey context, built on top of acurl, 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 3.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"},
)
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 |
|
the URL actually requested |
|
HTTP status code |
|
DNS resolution time |
|
TCP connect time |
|
TLS handshake time |
|
time to start of transfer |
|
time to first response byte |
|
total request duration |
|
resolved IP the request went to |
|
HTTP method used |
|
bytes downloaded |
Stats and tuning¶
mite_http registers two processors under mite_stats, listed in full on
Customizing miteʼs stats:
mite_http_response_total— a counter, labelled bytest,journey,transaction,methodandresponse_codemite_http_response_time_seconds— a histogram oftotal_time, labelled bytransaction
The histogram’s 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.
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.
See also¶
acurl — 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