list_prometheus_label_names
Retrieve label names from Prometheus datasources with filtering by series selectors and time range to identify available metrics and dimensions for monitoring queries.
Instructions
List label names in a Prometheus datasource. Allows filtering by series selectors and time range.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| datasourceUid | Yes | The UID of the datasource to query | |
| endRfc3339 | No | The end time of the time range | |
| limit | No | Maximum number of results | |
| matches | No | Label matchers to filter the results | |
| startRfc3339 | No | The start time of the time range |
Implementation Reference
- src/tools/prometheus.ts:170-187 (handler)The asynchronous handler function that implements the core logic of the 'list_prometheus_label_names' tool. It creates a PrometheusClient, builds match selectors, queries for label names with optional time range and limit, and returns the result or error.handler: async (params, context: ToolContext) => { try { const client = new PrometheusClient(context.config.grafanaConfig, params.datasourceUid); const match = params.matches?.map((m: any) => buildSelector(m.filters)) || []; const labels = await client.getLabelNames( match.length > 0 ? match : undefined, params.startRfc3339, params.endRfc3339 ); const limited = params.limit ? labels.slice(0, params.limit) : labels; return createToolResult(limited); } catch (error: any) { return createErrorResult(error.message); } },
- src/tools/prometheus.ts:21-33 (schema)Zod schema defining the input parameters for the 'list_prometheus_label_names' tool, including datasource UID, optional match filters, time range, and limit.const ListPrometheusLabelNamesSchema = z.object({ datasourceUid: z.string().describe('The UID of the datasource to query'), matches: z.array(z.object({ filters: z.array(z.object({ name: z.string().describe('The name of the label to match against'), value: z.string().describe('The value to match against'), type: z.enum(['=', '!=', '=~', '!~']).describe('The match operator'), })), })).optional().describe('Label matchers to filter the results'), startRfc3339: z.string().optional().describe('The start time of the time range'), endRfc3339: z.string().optional().describe('The end time of the time range'), limit: z.number().optional().describe('Maximum number of results'), });
- src/tools/prometheus.ts:232-238 (registration)Function that registers the 'list_prometheus_label_names' tool (and other Prometheus tools) with the MCP server.export function registerPrometheusTools(server: any) { server.registerTool(queryPrometheus); server.registerTool(listPrometheusMetricNames); server.registerTool(listPrometheusLabelNames); server.registerTool(listPrometheusLabelValues); server.registerTool(listPrometheusMetricMetadata); }
- src/tools/prometheus.ts:84-98 (helper)Utility function to construct Prometheus label matchers string from filter objects, used by the label names handler to build query selectors.function buildSelector(filters: any[]): string { if (!filters || filters.length === 0) return '{}'; const parts = filters.map(f => { switch (f.type) { case '=': return `${f.name}="${f.value}"`; case '!=': return `${f.name}!="${f.value}"`; case '=~': return `${f.name}=~"${f.value}"`; case '!~': return `${f.name}!~"${f.value}"`; default: return ''; } }).filter(p => p); return `{${parts.join(',')}}`; }