query_loki_logs
Execute LogQL queries against Loki datasources to retrieve log entries and metric values within specified time ranges for monitoring and analysis.
Instructions
Executes a LogQL query against a Loki datasource to retrieve log entries or metric values
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| datasourceUid | Yes | The UID of the datasource to query | |
| direction | No | Direction of the query | |
| endRfc3339 | No | The end time of the query in RFC3339 format | |
| limit | No | Maximum number of log lines to return (default: 10, max: 100) | |
| logql | Yes | The LogQL query to execute against Loki | |
| startRfc3339 | No | The start time of the query in RFC3339 format |
Implementation Reference
- src/tools/loki.ts:105-129 (handler)The ToolDefinition object for 'query_loki_logs', including the async handler function that creates a LokiClient instance and executes the LogQL query to retrieve logs.export const queryLokiLogs: ToolDefinition = { name: 'query_loki_logs', description: 'Executes a LogQL query against a Loki datasource to retrieve log entries or metric values', inputSchema: QueryLokiLogsSchema, 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 logs = await client.queryLogs( params.logql, params.startRfc3339 || timeRange.start, params.endRfc3339 || timeRange.end, Math.min(params.limit || 10, 100), params.direction || 'backward' ); return createToolResult(logs); } catch (error: any) { return createErrorResult(error.message); } }, };
- src/tools/loki.ts:34-41 (schema)Zod input schema defining parameters for the query_loki_logs tool: datasourceUid, logql query, optional time range, limit, and direction.const QueryLokiLogsSchema = z.object({ datasourceUid: z.string().describe('The UID of the datasource to query'), logql: z.string().describe('The LogQL query to execute against Loki'), 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'), limit: z.number().optional().describe('Maximum number of log lines to return (default: 10, max: 100)'), direction: z.enum(['forward', 'backward']).optional().describe('Direction of the query'), });
- src/tools/loki.ts:174-180 (registration)Registration function for all Loki tools, including server.registerTool(queryLokiLogs) which registers the query_loki_logs tool.export function registerLokiTools(server: any) { server.registerTool(listLokiLabelNames); server.registerTool(listLokiLabelValues); server.registerTool(queryLokiLogs); server.registerTool(queryLokiStats); server.registerTool(findErrorPatternLogs); }
- src/cli.ts:110-112 (registration)Invocation of registerLokiTools(server) in the CLI entrypoint, conditionally enabling Loki tools including query_loki_logs.if (enabledTools.has('loki')) { registerLokiTools(server); }
- src/tools/loki.ts:6-13 (helper)Helper function used by the query_loki_logs handler to provide default time range (last hour) if not specified.function getDefaultTimeRange(): { start: string; end: string } { const now = Math.floor(Date.now() / 1000); const oneHourAgo = now - 3600; return { start: oneHourAgo.toString(), end: now.toString(), }; }