query-logs
Execute custom NRQL queries on New Relic logs for complex filtering and aggregation, such as searching for error patterns or recent entries.
Instructions
Execute a custom NRQL query against New Relic logs. Use this for complex queries with specific filtering and aggregations. Example: "SELECT * FROM Log WHERE message LIKE '%error%' SINCE 1 HOUR AGO LIMIT 100"
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | NRQL query to execute against New Relic logs |
Implementation Reference
- src/index.ts:211-223 (handler)The main handler case for 'query-logs' tool. It parses the input using QueryLogsInputSchema, calls newRelicClient.queryLogs(query), and returns the results as JSON text.
case 'query-logs': { const { query } = QueryLogsInputSchema.parse(args); const results = await newRelicClient.queryLogs(query); return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, ], }; } - src/types.ts:12-14 (schema)Zod schema for the query-logs tool input. Defines a required 'query' string parameter representing an NRQL query.
export const QueryLogsInputSchema = z.object({ query: z.string().describe('NRQL query to execute (e.g., "SELECT * FROM Log WHERE message LIKE \'%error%\' SINCE 1 HOUR AGO")'), }); - src/index.ts:59-75 (registration)Registration of the 'query-logs' tool under ListToolsRequestSchema. Includes name, description, and inputSchema for the tool listing.
{ name: 'query-logs', description: 'Execute a custom NRQL query against New Relic logs. ' + 'Use this for complex queries with specific filtering and aggregations. ' + 'Example: "SELECT * FROM Log WHERE message LIKE \'%error%\' SINCE 1 HOUR AGO LIMIT 100"', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'NRQL query to execute against New Relic logs', }, }, required: ['query'], }, }, - src/newrelic-client.ts:62-65 (helper)The queryLogs method on NewRelicClient. Delegates to the generic executeNRQL method which makes a GraphQL request to New Relic's NerdGraph API.
*/ async queryLogs(nrqlQuery: string): Promise<any[]> { return this.executeNRQL(nrqlQuery); } - src/newrelic-client.ts:22-58 (helper)The core executeNRQL helper method that sends the NRQL query to New Relic's GraphQL API and returns results.
async executeNRQL(query: string): Promise<any[]> { const graphqlQuery = ` { actor { account(id: ${this.accountId}) { nrql(query: "${this.escapeQuery(query)}") { results } } } } `; try { const response = await this.client.post<NerdGraphResponse>('', { query: graphqlQuery, }); if (response.data.errors && response.data.errors.length > 0) { const errorMessages = response.data.errors .map(err => err.message) .join(', '); throw new Error(`NerdGraph errors: ${errorMessages}`); } return response.data.data?.actor?.account?.nrql?.results || []; } catch (error) { if (axios.isAxiosError(error)) { throw new Error( `Failed to query New Relic: ${error.message}${ error.response?.data ? ` - ${JSON.stringify(error.response.data)}` : '' }` ); } throw error; } }