Skip to content

Commit

Permalink
Statistics: Add opal version
Browse files Browse the repository at this point in the history
  • Loading branch information
roekatz committed May 26, 2024
1 parent 74b3050 commit 0b72cf9
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions packages/opal-server/opal_server/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ChannelStats(BaseModel):

class ServerStats(BaseModel):
uptime: datetime = Field(..., description="uptime for this opal server worker")
version: str = Field(..., description="opal server version")
clients: Dict[str, List[ChannelStats]] = Field(
...,
description="connected opal clients, each client can have multiple subscriptions",
Expand All @@ -37,9 +38,11 @@ class ServerStats(BaseModel):
)


class StatCounts(BaseModel):
clients: int
servers: int
class ServerStatsBrief(BaseModel):
uptime: datetime = Field(..., description="uptime for this opal server worker")
version: str = Field(..., description="opal server version")
client_count: int = Field(..., description="number of connected opal clients")
server_count: int = Field(..., description="number of opal server replicas")


class SyncRequest(BaseModel):
Expand Down Expand Up @@ -75,7 +78,6 @@ class OpalStatistics:
def __init__(self, endpoint):
self._endpoint: PubSubEndpoint = endpoint
self._uptime = datetime.utcnow()
self._opal_version = module_version(opal_server.__name__)
self._workers_count = (lambda envar: int(envar) if envar.isdigit() else 1)(
os.environ.get("UVICORN_NUM_WORKERS", "1")
)
Expand All @@ -88,7 +90,10 @@ def __init__(self, endpoint):
# you have connected to your OPAL server and to help merge client lists between servers.
# The state is keyed by unique client id (A unique id that each opal client can set in env var `OPAL_CLIENT_STAT_ID`)
self._state: ServerStats = ServerStats(
uptime=self._uptime, clients={}, servers={self._worker_id}
uptime=self._uptime,
clients={},
servers={self._worker_id},
version=module_version(opal_server.__name__),
)

# rpc_id_to_client_id:
Expand All @@ -106,10 +111,12 @@ def state(self) -> ServerStats:
return self._state

@property
def stat_counts(self) -> StatCounts:
return StatCounts(
clients=len(self._state.clients),
servers=len(self._state.servers) / self._workers_count,
def state_brief(self) -> ServerStatsBrief:
return ServerStatsBrief(
uptime=self._state.uptime,
version=self._state.version,
client_count=len(self._state.clients),
server_count=len(self._state.servers) / self._workers_count,
)

async def _expire_old_servers(self):
Expand Down Expand Up @@ -386,7 +393,7 @@ async def get_statistics():
logger.info("Serving statistics")
return stats.state

@router.get("/stat_counts", response_model=StatCounts)
@router.get("/stats", response_model=ServerStatsBrief)
async def get_stat_counts():
"""Route to serve only server and client instanace counts."""
if stats is None:
Expand All @@ -397,7 +404,7 @@ async def get_stat_counts():
+ " To turn on, set this config var: OPAL_STATISTICS_ENABLED=true"
},
)
logger.info("Serving stat counts")
return stats.stat_counts
logger.info("Serving brief statistics info")
return stats.state_brief

return router

0 comments on commit 0b72cf9

Please sign in to comment.