query-apm
Run tailored NRQL queries against New Relic APM data for deep analysis of transactions, metrics, and other APM events. Obtain specific performance insights from your applications.
Instructions
Execute a custom NRQL query against New Relic APM data. Use this for complex queries against Transaction, Metric, or other APM event types. Example: "SELECT average(duration) FROM Transaction WHERE appName = 'MyApp' SINCE 1 HOUR AGO"
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | NRQL query to execute against APM data |
Implementation Reference
- src/index.ts:127-143 (registration)Tool 'query-apm' is registered in ListToolsRequestSchema handler with name, description, and inputSchema.
{ name: 'query-apm', description: 'Execute a custom NRQL query against New Relic APM data. ' + 'Use this for complex queries against Transaction, Metric, or other APM event types. ' + 'Example: "SELECT average(duration) FROM Transaction WHERE appName = \'MyApp\' SINCE 1 HOUR AGO"', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'NRQL query to execute against APM data', }, }, required: ['query'], }, }, - src/index.ts:253-265 (handler)Case 'query-apm' in CallToolRequestSchema: parses input with QueryApmInputSchema, calls newRelicClient.queryApm(), and returns results as JSON.
case 'query-apm': { const { query } = QueryApmInputSchema.parse(args); const results = await newRelicClient.queryApm(query); return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, ], }; } - src/types.ts:42-44 (schema)QueryApmInputSchema: Zod schema defining a required 'query' string parameter for APM NRQL queries.
export const QueryApmInputSchema = z.object({ query: z.string().describe('NRQL query to execute against APM data (e.g., "SELECT average(duration) FROM Transaction WHERE appName = \'MyApp\' SINCE 1 HOUR AGO")'), }); - src/newrelic-client.ts:108-110 (helper)NewRelicClient.queryApm(): executes the APM NRQL query by delegating to the generic executeNRQL() method.
async queryApm(nrqlQuery: string): Promise<any[]> { return this.executeNRQL(nrqlQuery); } - src/newrelic-client.ts:22-58 (helper)NewRelicClient.executeNRQL(): core helper that sends the GraphQL query to New Relic's NerdGraph 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; } }