Skip to content

feat: Add new Prometheus target and metadata API endpoints #295

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

Merged
merged 3 commits into from
Apr 18, 2025

Conversation

RiviaAzusa
Copy link
Contributor

Add four new methods to PrometheusConnect class:

  • get_scrape_pools(): Retrieve list of unique scrape pool names
  • get_targets(): Get active/dropped targets with optional state and pool filters
  • get_target_metadata(): Fetch metadata about metrics from specific targets
  • get_metric_metadata(): Get metadata about metrics with optional filtering

These additions provide better visibility into Prometheus targets and metric
metadata, enabling more detailed monitoring and configuration analysis.

Add four new methods to PrometheusConnect class:
- get_scrape_pools(): Retrieve list of unique scrape pool names
- get_targets(): Get active/dropped targets with optional state and pool filters
- get_target_metadata(): Fetch metadata about metrics from specific targets
- get_metric_metadata(): Get metadata about metrics with optional filtering

These additions provide better visibility into Prometheus targets and metric
metadata, enabling more detailed monitoring and configuration analysis.
@4n4nd
Copy link
Owner

4n4nd commented Apr 7, 2025

@RiviaAzusa this looks great but could you please add tests for these new functions?

@RiviaAzusa
Copy link
Contributor Author

I have submitted the test cases, please review them and point out any errors if there are any.

@4n4nd
Copy link
Owner

4n4nd commented Apr 17, 2025

Run PROM_URL="https://demo.promlabs.com/" pytest
============================= test session starts ==============================
platform linux -- Python 3.13.3, pytest-8.3.5, pluggy-1.5.0
rootdir: /home/runner/work/prometheus-api-client-python/prometheus-api-client-python
configfile: pyproject.toml
collected 48 items

tests/test_metric.py ..........                                          [ 20%]
tests/test_metric_range_df.py ........                                   [ 37%]
tests/test_metric_snapshot_df.py .......                                 [ 52%]
tests/test_metrics_list.py ....                                          [ 60%]
tests/test_prometheus_connect.py ...F....F..........                     [100%]

=================================== FAILURES ===================================
________________ TestPrometheusConnect.test_get_metric_metadata ________________

self = <tests.test_prometheus_connect.TestPrometheusConnect testMethod=test_get_metric_metadata>

    def test_get_metric_metadata(self):  # noqa D102
        metadata = self.pc.get_metric_metadata(metric=None)
        self.assertIsInstance(metadata, list)
        self.assertTrue(len(metadata) > 0, "no metric metadata found")
    
        # Check structure of metadata
        self.assertIn('metric_name', metadata[0])
        self.assertIn('type', metadata[0])
        self.assertIn('help', metadata[0])
        self.assertIn('unit', metadata[0])
    
        # Test with specific metric
        if len(metadata) > 0:
            metric_name = metadata[0]['metric_name']
            filtered_metadata = self.pc.get_metric_metadata(metric=metric_name)
            self.assertIsInstance(filtered_metadata, list)
            self.assertTrue(all(item['metric_name'] == metric_name for item in filtered_metadata))
    
        # Test with limit
>       limited_metadata = self.pc.get_metric_metadata(limit=1)
E       TypeError: PrometheusConnect.get_metric_metadata() missing 1 required positional argument: 'metric'

tests/test_prometheus_connect.py:205: TypeError
________________ TestPrometheusConnect.test_get_target_metadata ________________

self = <tests.test_prometheus_connect.TestPrometheusConnect testMethod=test_get_target_metadata>

    def test_get_target_metadata(self):  # noqa D102
        # Get a target to test with
        targets = self.pc.get_targets()
        if len(targets['activeTargets']) > 0:
            target = {
                'job': targets['activeTargets'][0]['labels']['job']
            }
            metadata = self.pc.get_target_metadata(target)
            self.assertIsInstance(metadata, list)
    
            # Test with metric filter
            if len(metadata) > 0:
                metric_name = metadata[0]['metric']
                filtered_metadata = self.pc.get_target_metadata(target, metric=metric_name)
                self.assertIsInstance(filtered_metadata, list)
>               self.assertTrue(all(item['metric'] == metric_name for item in filtered_metadata))

tests/test_prometheus_connect.py:180: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

.0 = <list_iterator object at 0x7fbbdfdd3790>

>   self.assertTrue(all(item['metric'] == metric_name for item in filtered_metadata))
E   KeyError: 'metric'

tests/test_prometheus_connect.py:180: KeyError
=========================== short test summary info ============================
FAILED tests/test_prometheus_connect.py::TestPrometheusConnect::test_get_metric_metadata - TypeError: PrometheusConnect.get_metric_metadata() missing 1 required positional argument: 'metric'
FAILED tests/test_prometheus_connect.py::TestPrometheusConnect::test_get_target_metadata - KeyError: 'metric'
========================= 2 failed, 46 passed in 9.26s =========================
Error: Process completed with exit code 1.
0s
0s

	modified:   tests/test_prometheus_connect.py
@RiviaAzusa
Copy link
Contributor Author

I have fixed the related bugs, please review again.

@4n4nd
Copy link
Owner

4n4nd commented Apr 18, 2025

Thank you @RiviaAzusa!

@4n4nd 4n4nd merged commit 4496fb0 into 4n4nd:master Apr 18, 2025
5 checks passed
@RiviaAzusa
Copy link
Contributor Author

You are welcome @4n4nd !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants