DEV Community

Vinit Shahdeo
Vinit Shahdeo

Posted on

New Relic NerdGraph API Client for Node.js

As software applications become more complex and distributed, monitoring and debugging them can become a significant challenge. New Relic is a well-known SaaS platform that offers monitoring, observability, and optimization solutions for modern software applications. NerdGraph is the GraphQL API of New Relic, and it provides developers with a more flexible and powerful way to interact with the New Relic platform.

To help you leverage the power of NerdGraph in your Node.js applications, I have recently created a Node.js API client for NerdGraph, called newrelic-nerdgraph-client. In this blog, I will walk you through the installation process and show you how to use the client to make synchronous and asynchronous NRQL queries.

📥 Installation

To install the newrelic-nerdgraph-client, simply run the following command:

npm i newrelic-nerdgraph-client
Enter fullscreen mode Exit fullscreen mode

🔧 Usage

First, you need to obtain a User API key from New Relic. You can create one by logging into your New Relic account and navigating to Account settings > API keys.

After obtaining the API key, you can create an instance of NerdGraph by passing it as a parameter to the constructor:

const NerdGraph = require('newrelic-nerdgraph-client');

const apiKey = '<YOUR_API_KEY_HERE>';
const client = new NerdGraph(apiKey);
Enter fullscreen mode Exit fullscreen mode

⚙️ Sync Query

You can make a synchronous NRQL query using the query method:

const options = {
  account: '<YOUR_ACCOUNT_ID_HERE>',
  query: 'SELECT * FROM Transaction SINCE 1 day ago'
};

client.query(options)
  .then((data) => {
    console.log(data);
  })
  .catch((error) => {
    console.error(error);
  });
Enter fullscreen mode Exit fullscreen mode

⚡ Async Query

NerdGraph also supports asynchronous NRQL queries. Asynchronous queries run in the background, and you can make follow-up requests to retrieve query results or the query status. This type of query avoids a query being interrupted by issues like browser timeouts or HTTP connection timeouts. It's especially useful for running queries that may take a long time to complete.

You can make an asynchronous NRQL query using the query method with the async option set to true:

const options = {
  account: '<YOUR_ACCOUNT_ID_HERE>',
  query: 'SELECT * FROM Transaction SINCE 1 day ago',
  async: true
};

client.query(options)
  .then((data) => {
    const queryId = data?.queryId;

    if (!queryId) {
      // Poll the results using this queryId
    } else {
      console.log(data);
    }
  })
  .catch((error) => {
    console.error(error);
  });
Enter fullscreen mode Exit fullscreen mode

🔄 Polling Async Query

You can poll for the results of an asynchronous NRQL query using the poll method:

const options = {
  account: '<YOUR_ACCOUNT_ID_HERE>',
  queryId: '<YOUR_QUERY_ID_HERE>'
};

client.poll(options)
  .then((data) => {
    if (data.queryId) {
      // Poll it again
    } else {
      console.log(data);
    } 
  })
  .catch((error) => {
    console.error(error);
  });
Enter fullscreen mode Exit fullscreen mode

🚀 Seamlessly Interact with NerdGraph in Node.js

In conclusion, the New Relic NerdGraph API Client provides a flexible way to interact with the NerdGraph GraphQL API of New Relic in your Node.js applications. Whether you need to perform synchronous or asynchronous NRQL queries, retrieve query results, or poll for the status of async queries, this client has you covered. With its easy-to-use interface, support for promises and callbacks integrating the New Relic NerdGraph API Client into your projects is a seamless experience.

For detailed API documentation, please refer to the GitHub repository, and don't forget to check out the package on NPM to get started.

Thank you.

Top comments (0)