INNER CODE UNIT · Python
ResponseHeaderLabel
stephenhillier/starlette_exporter · starlette_exporter/labels.py:7
class ResponseHeaderLabel:
"""ResponseHeaderLabel is a special class that allows populating a label
value based on response headers. starlette_exporter will recognize that this
should not be called until response headers are written."""
def __init__(
self, key: str, allowed_values: Optional[Iterable] = None, default: str = ""
) -> None:
self.key = key.lower()
self.default = default
self.allowed_values = allowed_values
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