Skip to content

Commit

Permalink
add get_metrics method to sandbox sync & async
Browse files Browse the repository at this point in the history
  • Loading branch information
0div committed Dec 27, 2024
1 parent b9cb0ae commit b1d02df
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 29 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions packages/python-sdk/e2b/api/client/models/sandbox_metric.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion packages/python-sdk/e2b/sandbox/sandbox_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from abc import ABC
from dataclasses import dataclass
from typing import Optional, Dict
from datetime import datetime
from typing import Dict, Optional

from httpx import Limits


Expand All @@ -21,6 +22,22 @@ class SandboxInfo:
"""Sandbox start time."""


@dataclass
class SandboxMetrics:
"""Sandbox resource usage metrics"""

timestamp: datetime
"""Timestamp of the metrics."""
cpu_pct: float
"""CPU usage in percentage."""
cpu_count: int
"""Number of CPU cores."""
mem_mib_used: int
"""Memory usage in bytes."""
mem_mib_total: int
"""Total memory available"""


class SandboxApiBase(ABC):
_limits = Limits(
max_keepalive_connections=10,
Expand Down
28 changes: 22 additions & 6 deletions packages/python-sdk/e2b/sandbox_async/main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import logging
import httpx

from typing import Dict, Optional, TypedDict, overload
from typing_extensions import Unpack
from typing import Dict, List, Optional, TypedDict, overload

import httpx
from e2b.connection_config import ConnectionConfig
from e2b.envd.api import ENVD_API_HEALTH_ROUTE, ahandle_envd_api_exception
from e2b.exceptions import format_request_timeout_error
from e2b.sandbox.main import SandboxSetup
from e2b.sandbox.utils import class_method_variant
from e2b.sandbox_async.filesystem.filesystem import Filesystem
from e2b.sandbox_async.commands.command import Commands
from e2b.sandbox_async.commands.pty import Pty
from e2b.sandbox_async.sandbox_api import SandboxApi
from e2b.sandbox_async.filesystem.filesystem import Filesystem
from e2b.sandbox_async.sandbox_api import SandboxApi, SandboxMetrics
from typing_extensions import Unpack

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -364,3 +363,20 @@ async def set_timeout( # type: ignore
timeout=timeout,
**self.connection_config.__dict__,
)

@class_method_variant("_cls_get_metrics")
async def get_metrics( # type: ignore
self,
request_timeout: Optional[float] = None,
) -> List[SandboxMetrics]:
config_dict = self.connection_config.__dict__
config_dict.pop("access_token", None)
config_dict.pop("api_url", None)

if request_timeout:
config_dict["request_timeout"] = request_timeout

return await SandboxApi._cls_get_metrics(
sandbox_id=self.sandbox_id,
**self.connection_config.__dict__,
)
Loading

0 comments on commit b1d02df

Please sign in to comment.