|
| 1 | +import re |
| 2 | +from datetime import datetime |
| 3 | +from typing import Literal |
| 4 | + |
| 5 | +from mcp.server import FastMCP |
| 6 | + |
| 7 | +from ..client import grafana_client |
| 8 | +from ..grafana_types import ( |
| 9 | + DatasourceRef, |
| 10 | + DSQueryResponse, |
| 11 | + PrometheusMetricMetadata, |
| 12 | + Query, |
| 13 | + ResponseWrapper, |
| 14 | + Selector, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +PrometheusQueryType = Literal["range", "instant"] |
| 19 | + |
| 20 | + |
| 21 | +async def query_prometheus( |
| 22 | + datasource_uid: str, |
| 23 | + expr: str, |
| 24 | + start_rfc3339: str, |
| 25 | + end_rfc3339: str, |
| 26 | + step_seconds: int, |
| 27 | + query_type: PrometheusQueryType = "range", |
| 28 | +) -> DSQueryResponse: |
| 29 | + """ |
| 30 | + Query Prometheus using a range request. |
| 31 | +
|
| 32 | + # Parameters. |
| 33 | +
|
| 34 | + datasource_uid: The uid of the datasource to query. |
| 35 | + expr: The PromQL expression to query. |
| 36 | + start_rfc3339: The start time in RFC3339 format. |
| 37 | + end_rfc3339: The end time in RFC3339 format. Ignored if `query_type` is 'instant'. |
| 38 | + step_seconds: The time series step size in seconds. Ignored if `query_type` is 'instant'. |
| 39 | + query_type: The type of query to use. Either 'range' or 'instant'. |
| 40 | + """ |
| 41 | + start = datetime.fromisoformat(start_rfc3339) |
| 42 | + end = datetime.fromisoformat(end_rfc3339) |
| 43 | + query = Query( |
| 44 | + refId="A", |
| 45 | + datasource=DatasourceRef( |
| 46 | + uid=datasource_uid, |
| 47 | + type="prometheus", |
| 48 | + ), |
| 49 | + queryType=query_type, |
| 50 | + expr=expr, # type: ignore |
| 51 | + intervalMs=step_seconds * 1000, |
| 52 | + ) |
| 53 | + response = await grafana_client.query(start, end, [query]) |
| 54 | + return DSQueryResponse.model_validate_json(response) |
| 55 | + |
| 56 | + |
| 57 | +async def get_prometheus_metric_metadata( |
| 58 | + datasource_uid: str, |
| 59 | + limit: int | None = None, |
| 60 | + limit_per_metric: int | None = None, |
| 61 | + metric: str | None = None, |
| 62 | +) -> dict[str, list[PrometheusMetricMetadata]]: |
| 63 | + """ |
| 64 | + Get metadata for all metrics in Prometheus. |
| 65 | +
|
| 66 | + # Parameters. |
| 67 | +
|
| 68 | + datasource_uid: The uid of the Grafana datasource to query. |
| 69 | + limit: Optionally, the maximum number of results to return. |
| 70 | + limit_per_metric: Optionally, the maximum number of results to return per metric. |
| 71 | + metric: Optionally, a metric name to filter the results by. |
| 72 | +
|
| 73 | + # Returns. |
| 74 | +
|
| 75 | + A mapping from metric name to all available metadata for that metric. |
| 76 | + """ |
| 77 | + response = await grafana_client.get_prometheus_metric_metadata( |
| 78 | + datasource_uid, |
| 79 | + limit=limit, |
| 80 | + limit_per_metric=limit_per_metric, |
| 81 | + metric=metric, |
| 82 | + ) |
| 83 | + return ( |
| 84 | + ResponseWrapper[dict[str, list[PrometheusMetricMetadata]]] |
| 85 | + .model_validate_json(response) |
| 86 | + .data |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +async def get_prometheus_metric_names( |
| 91 | + datasource_uid: str, |
| 92 | + regex: str, |
| 93 | +) -> list[str]: |
| 94 | + """ |
| 95 | + Get metric names in a Prometheus datasource that match the given regex. |
| 96 | +
|
| 97 | + # Parameters. |
| 98 | +
|
| 99 | + datasource_uid: The uid of the Grafana datasource to query. |
| 100 | + regex: The regex to match against the metric names. Uses Python's re.match. |
| 101 | +
|
| 102 | + # Returns. |
| 103 | +
|
| 104 | + A list of metric names that match the given regex. |
| 105 | + """ |
| 106 | + name_label_values = await get_prometheus_label_values(datasource_uid, "__name__") |
| 107 | + compiled = re.compile(regex) |
| 108 | + return [name for name in name_label_values if compiled.match(name)] |
| 109 | + |
| 110 | + |
| 111 | +async def get_prometheus_label_names( |
| 112 | + datasource_uid: str, |
| 113 | + matches: list[Selector] | None = None, |
| 114 | + start: datetime | None = None, |
| 115 | + end: datetime | None = None, |
| 116 | + limit: int | None = None, |
| 117 | +) -> list[str]: |
| 118 | + """ |
| 119 | + Get the label names in a Prometheus datasource, optionally filtered to those |
| 120 | + matching the given selectors and within the given time range. |
| 121 | +
|
| 122 | + If you want to get the label names for a specific metric, pass a matcher |
| 123 | + like `{__name__="metric_name"}` to the `matches` parameter. |
| 124 | +
|
| 125 | + # Parameters. |
| 126 | +
|
| 127 | + datasource_uid: The uid of the Grafana datasource to query. |
| 128 | + matches: Optionally, a list of label matchers to filter the results by. |
| 129 | + start: Optionally, the start time of the time range to filter the results by. |
| 130 | + end: Optionally, the end time of the time range to filter the results by. |
| 131 | + limit: Optionally, the maximum number of results to return. |
| 132 | + """ |
| 133 | + response = await grafana_client.get_prometheus_label_names( |
| 134 | + datasource_uid, |
| 135 | + matches=matches, |
| 136 | + start=start, |
| 137 | + end=end, |
| 138 | + limit=limit, |
| 139 | + ) |
| 140 | + return ResponseWrapper[list[str]].model_validate_json(response).data |
| 141 | + |
| 142 | + |
| 143 | +async def get_prometheus_label_values( |
| 144 | + datasource_uid: str, |
| 145 | + label_name: str, |
| 146 | + matches: list[Selector] | None = None, |
| 147 | + start: datetime | None = None, |
| 148 | + end: datetime | None = None, |
| 149 | + limit: int | None = None, |
| 150 | +): |
| 151 | + """ |
| 152 | + Get the values of a label in Prometheus. |
| 153 | +
|
| 154 | + # Parameters. |
| 155 | +
|
| 156 | + datasource_uid: The uid of the Grafana datasource to query. |
| 157 | + label_name: The name of the label to query. |
| 158 | + matches: Optionally, a list of selectors to filter the results by. |
| 159 | + start: Optionally, the start time of the query. |
| 160 | + end: Optionally, the end time of the query. |
| 161 | + limit: Optionally, the maximum number of results to return. |
| 162 | + """ |
| 163 | + response = await grafana_client.get_prometheus_label_values( |
| 164 | + datasource_uid, |
| 165 | + label_name, |
| 166 | + matches=matches, |
| 167 | + start=start, |
| 168 | + end=end, |
| 169 | + limit=limit, |
| 170 | + ) |
| 171 | + return ResponseWrapper[list[str]].model_validate_json(response).data |
| 172 | + |
| 173 | + |
| 174 | +def add_tools(mcp: FastMCP): |
| 175 | + mcp.add_tool(query_prometheus) |
| 176 | + mcp.add_tool(get_prometheus_metric_metadata) |
| 177 | + mcp.add_tool(get_prometheus_metric_names) |
| 178 | + mcp.add_tool(get_prometheus_label_names) |
| 179 | + mcp.add_tool(get_prometheus_label_values) |
0 commit comments