INNER CODE UNIT · Python
from_header
stephenhillier/starlette_exporter · starlette_exporter/labels.py:26
def from_header(key: str, allowed_values: Optional[Iterable] = None) -> Callable:
"""returns a function that retrieves a header value from a request.
The returned function can be passed to the `labels` argument of PrometheusMiddleware
to label metrics using a header value.
`key`: header key
`allowed_values`: an iterable (e.g. list or tuple) containing an allowlist of values. Any
header value not in allowed_values will result in an empty string being returned. Use
this to constrain the potential label values.
example:
```
PrometheusMiddleware(
labels={
"host": from_header("X-User", allowed_values=("frank", "estelle"))
}
)