Skip to content

Commit ad15ef9

Browse files
author
袁定胜
committed
modified: prometheus_api_client/prometheus_connect.py
modified: tests/test_prometheus_connect.py
1 parent 5529de0 commit ad15ef9

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

prometheus_api_client/prometheus_connect.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,13 +608,12 @@ def get_targets(self, state: str = None, scrape_pool: str = None):
608608
response.status_code, response.content)
609609
)
610610

611-
def get_target_metadata(self, target: dict[str, str], metric: str = None, limit: int = None):
611+
def get_target_metadata(self, target: dict[str, str], metric: str = None):
612612
"""
613613
Get metadata about metrics from a specific target.
614614
615615
:param target: (dict) A dictionary containing target labels to match against (e.g. {'job': 'prometheus'})
616616
:param metric: (str) Optional metric name to filter metadata
617-
:param limit: (int) Optional maximum number of targets to match
618617
:returns: (list) A list of metadata entries for matching targets
619618
:raises:
620619
(RequestException) Raises an exception in case of a connection error
@@ -631,9 +630,6 @@ def get_target_metadata(self, target: dict[str, str], metric: str = None, limit:
631630
",".join(f'{k}="{v}"' for k, v in target.items()) + "}"
632631
params['match_target'] = match_target
633632

634-
if limit:
635-
params['limit'] = limit
636-
637633
response = self._session.get(
638634
"{0}/api/v1/targets/metadata".format(self.url),
639635
verify=self._session.verify,

tests/test_prometheus_connect.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_get_scrape_pools(self): # noqa D102
145145
self.assertTrue(len(scrape_pools) > 0, "no scrape pools found")
146146
self.assertIsInstance(scrape_pools[0], str)
147147

148-
def test_get_targets(self): # noqa D102
148+
def test_get_targets(self): # PR #295
149149
targets = self.pc.get_targets()
150150
self.assertIsInstance(targets, dict)
151151
self.assertIn('activeTargets', targets)
@@ -160,9 +160,8 @@ def test_get_targets(self): # noqa D102
160160
if len(scrape_pools := self.pc.get_scrape_pools()) > 0:
161161
pool_targets = self.pc.get_targets(scrape_pool=scrape_pools[0])
162162
self.assertIsInstance(pool_targets, dict)
163-
self.assertIn('activeTargets', pool_targets)
164163

165-
def test_get_target_metadata(self): # noqa D102
164+
def test_get_target_metadata(self): # PR #295
166165
# Get a target to test with
167166
targets = self.pc.get_targets()
168167
if len(targets['activeTargets']) > 0:
@@ -171,42 +170,42 @@ def test_get_target_metadata(self): # noqa D102
171170
}
172171
metadata = self.pc.get_target_metadata(target)
173172
self.assertIsInstance(metadata, list)
174-
173+
175174
# Test with metric filter
176175
if len(metadata) > 0:
177176
metric_name = metadata[0]['metric']
178-
filtered_metadata = self.pc.get_target_metadata(target, metric=metric_name)
177+
filtered_metadata = self.pc.get_target_metadata(
178+
target, metric=metric_name)
179179
self.assertIsInstance(filtered_metadata, list)
180-
self.assertTrue(all(item['metric'] == metric_name for item in filtered_metadata))
181-
182-
# Test with limit
183-
limited_metadata = self.pc.get_target_metadata(target, limit=1)
184-
self.assertLessEqual(len(limited_metadata), 1)
180+
self.assertTrue(
181+
all(item['target']['job'] == target['job'] for item in filtered_metadata))
182+
185183

186-
def test_get_metric_metadata(self): # noqa D102
184+
def test_get_metric_metadata(self): # PR #295
187185
metadata = self.pc.get_metric_metadata(metric=None)
188186
self.assertIsInstance(metadata, list)
189187
self.assertTrue(len(metadata) > 0, "no metric metadata found")
190-
188+
191189
# Check structure of metadata
192190
self.assertIn('metric_name', metadata[0])
193191
self.assertIn('type', metadata[0])
194192
self.assertIn('help', metadata[0])
195193
self.assertIn('unit', metadata[0])
196-
194+
197195
# Test with specific metric
198196
if len(metadata) > 0:
199197
metric_name = metadata[0]['metric_name']
200198
filtered_metadata = self.pc.get_metric_metadata(metric=metric_name)
201199
self.assertIsInstance(filtered_metadata, list)
202-
self.assertTrue(all(item['metric_name'] == metric_name for item in filtered_metadata))
203-
200+
self.assertTrue(
201+
all(item['metric_name'] == metric_name for item in filtered_metadata))
202+
204203
# Test with limit
205-
limited_metadata = self.pc.get_metric_metadata(limit=1)
204+
limited_metadata = self.pc.get_metric_metadata(metric_name, limit=1)
206205
self.assertLessEqual(len(limited_metadata), 1)
207-
206+
208207
# Test with limit_per_metric
209-
limited_per_metric = self.pc.get_metric_metadata(limit_per_metric=1)
208+
limited_per_metric = self.pc.get_metric_metadata(metric_name, limit_per_metric=1)
210209
self.assertIsInstance(limited_per_metric, list)
211210

212211

0 commit comments

Comments
 (0)