Skip to content

Commit 1260829

Browse files
authored
Merge pull request #77 from tharropoulos/metrics
Add metrics support
2 parents fba7e6a + 50ac673 commit 1260829

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

src/typesense/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- typesense.conversations_models: Provides the ConversationsModels class.
1818
- typesense.debug: Provides the Debug class.
1919
- typesense.keys: Provides the Keys class.
20+
- typesense.metrics: Provides the Metrics class.
2021
- typesense.multi_search: Provides the MultiSearch class.
2122
- typesense.operations: Provides the Operations class.
2223
- typesense.stopwords: Provides the Stopwords class.
@@ -43,6 +44,7 @@
4344
from typesense.conversations_models import ConversationsModels
4445
from typesense.debug import Debug
4546
from typesense.keys import Keys
47+
from typesense.metrics import Metrics
4648
from typesense.multi_search import MultiSearch
4749
from typesense.operations import Operations
4850
from typesense.stemming import Stemming
@@ -72,6 +74,7 @@ class Client:
7274
operations (Operations): Instance for various Typesense operations.
7375
debug (Debug): Instance for debug operations.
7476
stopwords (Stopwords): Instance for managing stopwords.
77+
metrics (Metrics): Instance for retrieving system and Typesense metrics.
7578
conversations_models (ConversationsModels): Instance for managing conversation models.
7679
"""
7780

@@ -102,6 +105,7 @@ def __init__(self, config_dict: ConfigDict) -> None:
102105
self.operations = Operations(self.api_call)
103106
self.debug = Debug(self.api_call)
104107
self.stopwords = Stopwords(self.api_call)
108+
self.metrics = Metrics(self.api_call)
105109
self.conversations_models = ConversationsModels(self.api_call)
106110

107111
def typed_collection(

src/typesense/metrics.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"""
2+
This module provides functionality for retrieving metrics from the Typesense API.
3+
4+
It contains the Metrics class, which handles API operations for retrieving
5+
system and Typesense metrics such as CPU, memory, disk, and network usage.
6+
7+
Classes:
8+
MetricsResponse: Type definition for metrics response.
9+
Metrics: Manages retrieving metrics from the Typesense API.
10+
11+
Dependencies:
12+
- typesense.api_call: Provides the ApiCall class for making API requests.
13+
14+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
15+
"""
16+
17+
import sys
18+
19+
if sys.version_info >= (3, 11):
20+
import typing
21+
else:
22+
import typing_extensions as typing
23+
24+
from typesense.api_call import ApiCall
25+
26+
27+
class MetricsResponseBase(typing.TypedDict):
28+
"""
29+
Response schema for metrics retrieval.
30+
31+
This TypedDict includes system metrics like CPU, memory, disk, and network usage,
32+
as well as Typesense-specific memory metrics.
33+
34+
Attributes:
35+
system_cpu_active_percentage (str): Overall CPU active percentage.
36+
system_disk_total_bytes (str): Total disk space in bytes.
37+
system_disk_used_bytes (str): Used disk space in bytes.
38+
system_memory_total_bytes (str): Total system memory in bytes.
39+
system_memory_used_bytes (str): Used system memory in bytes.
40+
system_network_received_bytes (str): Total network bytes received.
41+
system_network_sent_bytes (str): Total network bytes sent.
42+
typesense_memory_active_bytes (str): Active memory used by Typesense.
43+
typesense_memory_allocated_bytes (str): Allocated memory for Typesense.
44+
typesense_memory_fragmentation_ratio (str): Memory fragmentation ratio.
45+
typesense_memory_mapped_bytes (str): Mapped memory in bytes.
46+
typesense_memory_metadata_bytes (str): Memory used for metadata.
47+
typesense_memory_resident_bytes (str): Resident memory in bytes.
48+
typesense_memory_retained_bytes (str): Retained memory in bytes.
49+
"""
50+
51+
system_cpu_active_percentage: str
52+
system_disk_total_bytes: str
53+
system_disk_used_bytes: str
54+
system_memory_total_bytes: str
55+
system_memory_used_bytes: str
56+
system_network_received_bytes: str
57+
system_network_sent_bytes: str
58+
typesense_memory_active_bytes: str
59+
typesense_memory_allocated_bytes: str
60+
typesense_memory_fragmentation_ratio: str
61+
typesense_memory_mapped_bytes: str
62+
typesense_memory_metadata_bytes: str
63+
typesense_memory_resident_bytes: str
64+
typesense_memory_retained_bytes: str
65+
66+
67+
class MetricsResponse(MetricsResponseBase):
68+
"""Extended MetricsResponse with optional per-CPU core metrics."""
69+
70+
system_memory_total_swap_bytes: str
71+
system_memory_used_swap_bytes: str
72+
system_cpu1_active_percentage: typing.Optional[str]
73+
system_cpu2_active_percentage: typing.Optional[str]
74+
system_cpu3_active_percentage: typing.Optional[str]
75+
system_cpu4_active_percentage: typing.Optional[str]
76+
system_cpu5_active_percentage: typing.Optional[str]
77+
system_cpu6_active_percentage: typing.Optional[str]
78+
system_cpu7_active_percentage: typing.Optional[str]
79+
system_cpu8_active_percentage: typing.Optional[str]
80+
system_cpu9_active_percentage: typing.Optional[str]
81+
system_cpu10_active_percentage: typing.Optional[str]
82+
system_cpu11_active_percentage: typing.Optional[str]
83+
system_cpu12_active_percentage: typing.Optional[str]
84+
system_cpu13_active_percentage: typing.Optional[str]
85+
system_cpu14_active_percentage: typing.Optional[str]
86+
system_cpu15_active_percentage: typing.Optional[str]
87+
system_cpu16_active_percentage: typing.Optional[str]
88+
system_cpu17_active_percentage: typing.Optional[str]
89+
system_cpu18_active_percentage: typing.Optional[str]
90+
system_cpu19_active_percentage: typing.Optional[str]
91+
system_cpu20_active_percentage: typing.Optional[str]
92+
system_cpu21_active_percentage: typing.Optional[str]
93+
system_cpu22_active_percentage: typing.Optional[str]
94+
system_cpu23_active_percentage: typing.Optional[str]
95+
system_cpu24_active_percentage: typing.Optional[str]
96+
97+
98+
class Metrics:
99+
"""
100+
Manages metrics retrieval from the Typesense API.
101+
102+
This class provides methods to retrieve system and Typesense metrics
103+
such as CPU, memory, disk, and network usage.
104+
105+
Attributes:
106+
resource_path (str): The base path for metrics endpoint.
107+
api_call (ApiCall): The ApiCall instance for making API requests.
108+
"""
109+
110+
resource_path: typing.Final[str] = "/metrics.json"
111+
112+
def __init__(self, api_call: ApiCall):
113+
"""
114+
Initialize the Metrics instance.
115+
116+
Args:
117+
api_call (ApiCall): The ApiCall instance for making API requests.
118+
"""
119+
self.api_call = api_call
120+
121+
def retrieve(self) -> MetricsResponse:
122+
"""
123+
Retrieve metrics from the Typesense API.
124+
125+
Returns:
126+
MetricsResponse: A dictionary containing system and Typesense metrics.
127+
"""
128+
response: MetricsResponse = self.api_call.get(
129+
Metrics.resource_path,
130+
as_json=True,
131+
entity_type=MetricsResponse,
132+
)
133+
return response

tests/fixtures/metrics_fixtures.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Fixtures for the Metrics class tests."""
2+
3+
import pytest
4+
5+
from typesense.api_call import ApiCall
6+
from typesense.metrics import Metrics
7+
8+
9+
@pytest.fixture(scope="function", name="actual_metrics")
10+
def actual_debug_fixture(actual_api_call: ApiCall) -> Metrics:
11+
"""Return a Debug object using a real API."""
12+
return Metrics(actual_api_call)

tests/metrics_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Tests for the Debug class."""
2+
3+
from __future__ import annotations
4+
5+
from typesense.metrics import Metrics
6+
7+
8+
def test_actual_retrieve(actual_metrics: Metrics) -> None:
9+
"""Test that the Debug object can retrieve a debug on Typesense server and verify response structure."""
10+
response = actual_metrics.retrieve()
11+
12+
assert "system_cpu_active_percentage" in response
13+
assert "system_disk_total_bytes" in response
14+
assert "system_disk_used_bytes" in response
15+
assert "system_memory_total_bytes" in response
16+
assert "system_memory_used_bytes" in response
17+
assert "system_network_received_bytes" in response
18+
assert "system_network_sent_bytes" in response
19+
assert "typesense_memory_active_bytes" in response
20+
assert "typesense_memory_allocated_bytes" in response
21+
assert "typesense_memory_fragmentation_ratio" in response
22+
23+
assert "typesense_memory_mapped_bytes" in response
24+
assert "typesense_memory_metadata_bytes" in response
25+
assert "typesense_memory_resident_bytes" in response
26+
assert "typesense_memory_retained_bytes" in response

0 commit comments

Comments
 (0)