INNER CODE UNIT · Python
__call__
stephenhillier/starlette_exporter · starlette_exporter/labels.py:19
def __call__(self, headers: Dict) -> Any:
v = headers.get(self.key, self.default)
if self.allowed_values is not None and v not in self.allowed_values:
return self.default
return v
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: