Skip to content

Commit

Permalink
ci(pre-commit): Apply hook auto fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci-lite[bot] authored Mar 12, 2024
1 parent 224b34a commit ad7b996
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
26 changes: 20 additions & 6 deletions src/prometheus_fastapi_instrumentator/instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_instrumentator_expose.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ad7b996

Please sign in to comment.