Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Auth prometheus metrics #4725

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/phoenix/server/jwt_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ClaimSet,
Token,
)
from phoenix.config import get_env_enable_prometheus
from phoenix.db import models
from phoenix.db.enums import UserRole
from phoenix.server.types import (
Expand Down Expand Up @@ -444,6 +445,13 @@ def _to_db(self, claims: RefreshTokenClaims) -> models.RefreshToken:
expires_at=claims.expiration_time,
)

async def _update(self) -> None:
await super()._update()
if get_env_enable_prometheus():
from phoenix.server.prometheus import JWT_STORE_TOKENS_ACTIVE

JWT_STORE_TOKENS_ACTIVE.set(len(self._claims._cache))


class _ApiKeyStore(
_Store[
Expand Down Expand Up @@ -487,3 +495,10 @@ def _to_db(self, claims: ApiKeyClaims) -> models.ApiKey:
created_at=claims.issued_at,
expires_at=claims.expiration_time or None,
)

async def _update(self) -> None:
await super()._update()
if get_env_enable_prometheus():
from phoenix.server.prometheus import JWT_STORE_API_KEYS_ACTIVE

JWT_STORE_API_KEYS_ACTIVE.set(len(self._claims._cache))
20 changes: 20 additions & 0 deletions src/phoenix/server/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@
documentation="Total count of bulk loader exceptions",
)

RATE_LIMITER_CACHE_SIZE = Gauge(
name="rate_limiter_cache_size",
documentation="Current size of the rate limiter cache",
)

RATE_LIMITER_THROTTLES = Counter(
name="rate_limiter_throttles_total",
documentation="Total count of rate limiter throttles",
)

JWT_STORE_TOKENS_ACTIVE = Gauge(
name="jwt_store_tokens_active",
documentation="Current number of refresh tokens in the JWT store",
)

JWT_STORE_API_KEYS_ACTIVE = Gauge(
name="jwt_store_api_keys_active",
documentation="Current number of API keys in the JWT store",
)


class PrometheusMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
Expand Down
9 changes: 9 additions & 0 deletions src/phoenix/server/rate_limiters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from fastapi import HTTPException, Request

from phoenix.config import get_env_enable_prometheus
from phoenix.exceptions import PhoenixException


Expand Down Expand Up @@ -55,6 +56,10 @@ def available_tokens(self) -> float:

def make_request_if_ready(self) -> None:
if self.available_tokens() < 1:
if get_env_enable_prometheus():
from phoenix.server.prometheus import RATE_LIMITER_THROTTLES

RATE_LIMITER_THROTTLES.inc()
raise UnavailableTokensError
self.tokens -= 1

Expand Down Expand Up @@ -144,6 +149,10 @@ def make_request(self, key: str) -> None:
self._cleanup_expired_limiters(request_time)
rate_limiter = self._fetch_token_bucket(key, request_time)
rate_limiter.make_request_if_ready()
if get_env_enable_prometheus():
from phoenix.server.prometheus import RATE_LIMITER_CACHE_SIZE

RATE_LIMITER_CACHE_SIZE.set(sum(len(partition) for partition in self.cache_partitions))


def fastapi_ip_rate_limiter(
Expand Down
Loading