query_loki_stats
Retrieve statistics about log streams matching a LogQL selector from a Loki datasource within specified time ranges to analyze log volume and stream metrics.
Instructions
Retrieves statistics about log streams matching a given LogQL selector within a Loki datasource
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| datasourceUid | Yes | The UID of the datasource to query | |
| endRfc3339 | No | The end time of the query in RFC3339 format | |
| logql | Yes | The LogQL matcher expression to execute | |
| startRfc3339 | No | The start time of the query in RFC3339 format |
Implementation Reference
- src/tools/loki.ts:131-153 (handler)The handler function that defines and implements the core logic of the 'query_loki_stats' tool, using LokiClient to fetch stats.export const queryLokiStats: ToolDefinition = { name: 'query_loki_stats', description: 'Retrieves statistics about log streams matching a given LogQL selector within a Loki datasource', inputSchema: QueryLokiStatsSchema, handler: async (params, context: ToolContext) => { try { const client = new LokiClient(context.config.grafanaConfig, params.datasourceUid); const timeRange: TimeRange = params.startRfc3339 || params.endRfc3339 ? { start: '', end: '' } : getDefaultTimeRange(); const stats = await client.queryStats( params.logql, params.startRfc3339 || timeRange.start, params.endRfc3339 || timeRange.end ); return createToolResult(stats); } catch (error: any) { return createErrorResult(error.message); } }, };
- src/tools/loki.ts:43-48 (schema)Zod schema defining the input parameters for the query_loki_stats tool.const QueryLokiStatsSchema = z.object({ datasourceUid: z.string().describe('The UID of the datasource to query'), logql: z.string().describe('The LogQL matcher expression to execute'), startRfc3339: z.string().optional().describe('The start time of the query in RFC3339 format'), endRfc3339: z.string().optional().describe('The end time of the query in RFC3339 format'), });
- src/tools/loki.ts:174-180 (registration)Function that registers the query_loki_stats tool (and other Loki tools) with the MCP server.export function registerLokiTools(server: any) { server.registerTool(listLokiLabelNames); server.registerTool(listLokiLabelValues); server.registerTool(queryLokiLogs); server.registerTool(queryLokiStats); server.registerTool(findErrorPatternLogs); }
- src/clients/loki-client.ts:62-78 (helper)The LokiClient.queryStats method that performs the HTTP request to Loki's /index/stats endpoint to retrieve log statistics.async queryStats(query: string, start?: string, end?: string): Promise<LokiStats> { try { const params: any = { query }; if (start) params.start = start; if (end) params.end = end; const response = await this.client.get('/loki/api/v1/index/stats', { params }); if (response.data.status !== 'success') { throw new Error(`Loki stats query failed: ${response.data.error || 'Unknown error'}`); } return response.data.data; } catch (error) { this.handleError(error); } }
- src/tools/loki.ts:6-13 (helper)Helper function to provide default time range (last hour) if not specified in tool parameters.function getDefaultTimeRange(): { start: string; end: string } { const now = Math.floor(Date.now() / 1000); const oneHourAgo = now - 3600; return { start: oneHourAgo.toString(), end: now.toString(), }; }