diff --git a/src/prometheus_fastapi_instrumentator/instrumentation.py b/src/prometheus_fastapi_instrumentator/instrumentation.py index f6860ac..17e51db 100644 --- a/src/prometheus_fastapi_instrumentator/instrumentation.py +++ b/src/prometheus_fastapi_instrumentator/instrumentation.py @@ -3,9 +3,19 @@ import os import re import warnings -from enum import Enum -from typing import Any, Awaitable, Callable, List, Optional, Sequence, Union, cast, Tuple from base64 import b64encode +from enum import Enum +from typing import ( + Any, + Awaitable, + Callable, + List, + Optional, + Sequence, + Tuple, + Union, + cast, +) from fastapi import FastAPI, HTTPException from prometheus_client import ( @@ -248,7 +258,7 @@ def expose( tags (List[str], optional): If you manage your routes with tags. Defaults to None. - + basic_auth (Tuple[str, str], optional): username and password for HTTP basic authentication. Disabled if None. @@ -264,18 +274,22 @@ def expose( authorization_value = None if basic_auth is not None: username, password = basic_auth - encoded_cred = b64encode(f'{username}:{password}'.encode('utf-8')).decode('ascii') + encoded_cred = b64encode(f"{username}:{password}".encode("utf-8")).decode( + "ascii" + ) authorization_value = f"Basic {encoded_cred}" @app.get(endpoint, include_in_schema=include_in_schema, tags=tags, **kwargs) def metrics(request: Request) -> Response: """Endpoint that serves Prometheus metrics.""" - authorization_header = request.headers.get('authorization', None) + authorization_header = request.headers.get("authorization", None) if authorization_header != authorization_value: raise HTTPException( status_code=401, - headers={'WWW-Authenticate': 'Basic realm="Access to metrics endpoint"'} + headers={ + "WWW-Authenticate": 'Basic realm="Access to metrics endpoint"' + }, ) ephemeral_registry = self.registry diff --git a/tests/test_instrumentator_expose.py b/tests/test_instrumentator_expose.py index 6437fbd..34d78f4 100644 --- a/tests/test_instrumentator_expose.py +++ b/tests/test_instrumentator_expose.py @@ -80,8 +80,8 @@ def test_expose_custom_path(): def test_expose_basic_auth(): - username = 'hello' - password = 'mom' + username = "hello" + password = "mom" app = create_app() Instrumentator().instrument(app).expose(app, basic_auth=(username, password)) client = TestClient(app)