1+ import itertools
12import re
23from datetime import datetime
34from typing import Literal
@@ -59,27 +60,28 @@ async def query_prometheus(
5960 return DSQueryResponse .model_validate_json (response )
6061
6162
62- async def get_prometheus_metric_metadata (
63+ async def list_prometheus_metric_metadata (
6364 datasource_uid : str ,
64- limit : int | None = None ,
65- limit_per_metric : int | None = None ,
65+ limit : int = 10 ,
66+ limit_per_metric : int = 10 ,
6667 metric : str | None = None ,
6768) -> dict [str , list [PrometheusMetricMetadata ]]:
6869 """
69- Get metadata for all metrics in Prometheus.
70+ List metadata for all metrics in Prometheus.
7071
7172 # Parameters.
7273
7374 datasource_uid: The uid of the Grafana datasource to query.
74- limit: Optionally, the maximum number of results to return.
75- limit_per_metric: Optionally, the maximum number of results to return per metric.
75+ limit: The maximum number of results to return. Defaults to 10.
76+ limit_per_metric: The maximum number of results to return per metric.
77+ Defaults to 10.
7678 metric: Optionally, a metric name to filter the results by.
7779
7880 # Returns.
7981
8082 A mapping from metric name to all available metadata for that metric.
8183 """
82- response = await grafana_client .get_prometheus_metric_metadata (
84+ response = await grafana_client .list_prometheus_metric_metadata (
8385 datasource_uid ,
8486 limit = limit ,
8587 limit_per_metric = limit_per_metric ,
@@ -92,39 +94,46 @@ async def get_prometheus_metric_metadata(
9294 )
9395
9496
95- async def get_prometheus_metric_names (
97+ async def list_prometheus_metric_names (
9698 datasource_uid : str ,
9799 regex : str ,
100+ limit : int = 10 ,
101+ page : int = 1 ,
98102) -> list [str ]:
99103 """
100- Get metric names in a Prometheus datasource that match the given regex.
104+ List metric names in a Prometheus datasource that match the given regex.
101105
102106 # Parameters.
103107
104108 datasource_uid: The uid of the Grafana datasource to query.
105109 regex: The regex to match against the metric names. Uses Python's re.match.
110+ limit: The maximum number of results to return. Defaults to 10.
111+ page: The page number to return. Defaults to 1.
106112
107113 # Returns.
108114
109115 A list of metric names that match the given regex.
110116 """
111- name_label_values = await get_prometheus_label_values (datasource_uid , "__name__" )
117+ name_label_values = await list_prometheus_label_values (datasource_uid , "__name__" )
112118 compiled = re .compile (regex )
113- return [name for name in name_label_values if compiled .match (name )]
119+ matches = (name for name in name_label_values if compiled .match (name ))
120+ start = (page - 1 ) * limit
121+ stop = start + limit
122+ return list (itertools .islice (matches , start , stop ))
114123
115124
116- async def get_prometheus_label_names (
125+ async def list_prometheus_label_names (
117126 datasource_uid : str ,
118127 matches : list [Selector ] | None = None ,
119128 start : datetime | None = None ,
120129 end : datetime | None = None ,
121- limit : int | None = None ,
130+ limit : int = 100 ,
122131) -> list [str ]:
123132 """
124- Get the label names in a Prometheus datasource, optionally filtered to those
133+ List the label names in a Prometheus datasource, optionally filtered to those
125134 matching the given selectors and within the given time range.
126135
127- If you want to get the label names for a specific metric, pass a matcher
136+ If you want to list the label names for a specific metric, pass a matcher
128137 like `{__name__="metric_name"}` to the `matches` parameter.
129138
130139 # Parameters.
@@ -133,9 +142,9 @@ async def get_prometheus_label_names(
133142 matches: Optionally, a list of label matchers to filter the results by.
134143 start: Optionally, the start time of the time range to filter the results by.
135144 end: Optionally, the end time of the time range to filter the results by.
136- limit: Optionally, the maximum number of results to return.
145+ limit: Optionally, the maximum number of results to return. Defaults to 100.
137146 """
138- response = await grafana_client .get_prometheus_label_names (
147+ response = await grafana_client .list_prometheus_label_names (
139148 datasource_uid ,
140149 matches = matches ,
141150 start = start ,
@@ -145,13 +154,13 @@ async def get_prometheus_label_names(
145154 return ResponseWrapper [list [str ]].model_validate_json (response ).data
146155
147156
148- async def get_prometheus_label_values (
157+ async def list_prometheus_label_values (
149158 datasource_uid : str ,
150159 label_name : str ,
151160 matches : list [Selector ] | None = None ,
152161 start : datetime | None = None ,
153162 end : datetime | None = None ,
154- limit : int | None = None ,
163+ limit : int = 100 ,
155164):
156165 """
157166 Get the values of a label in Prometheus.
@@ -163,9 +172,9 @@ async def get_prometheus_label_values(
163172 matches: Optionally, a list of selectors to filter the results by.
164173 start: Optionally, the start time of the query.
165174 end: Optionally, the end time of the query.
166- limit: Optionally, the maximum number of results to return.
175+ limit: Optionally, the maximum number of results to return. Defaults to 100.
167176 """
168- response = await grafana_client .get_prometheus_label_values (
177+ response = await grafana_client .list_prometheus_label_values (
169178 datasource_uid ,
170179 label_name ,
171180 matches = matches ,
@@ -178,7 +187,7 @@ async def get_prometheus_label_values(
178187
179188def add_tools (mcp : FastMCP ):
180189 mcp .add_tool (query_prometheus )
181- mcp .add_tool (get_prometheus_metric_metadata )
182- mcp .add_tool (get_prometheus_metric_names )
183- mcp .add_tool (get_prometheus_label_names )
184- mcp .add_tool (get_prometheus_label_values )
190+ mcp .add_tool (list_prometheus_metric_metadata )
191+ mcp .add_tool (list_prometheus_metric_names )
192+ mcp .add_tool (list_prometheus_label_names )
193+ mcp .add_tool (list_prometheus_label_values )
0 commit comments