Skip to content

An API client for NerdGraph—the GraphQL API of New Relic. Supports both synchronous and asynchronous NRQL queries.

License

Notifications You must be signed in to change notification settings

vinitshahdeo/newrelic-nerdgraph-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

New Relic NerdGraph API Client

A Node.js API Client for NerdGraph, the GraphQL API of New Relic that supports both synchronous and asynchronous NRQL queries. Learn more about NerdGraph or try it out — api.newrelic.com/graphiql

npm node-current

Installation

Install using npm:

npm i newrelic-nerdgraph-client

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-api');

const apiKey = '<YOUR_API_KEY_HERE>';
const client = new NerdGraph(apiKey);

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);
  });

Async Query

NerdGraph also supports asynchronous NRQL query. 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);
  });

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);
  });

Callback

You can use a callback function instead of a promise by passing it as a second parameter:

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

client.query(options, (error, data) => {
  if (error) {
    console.error(error);
  } else {
    console.log(data);
  }
});

Complete NRQL Response

You can get complete NRQL response with completeResponse option set to true:

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

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

Below is a sample NRQL response:

{
  "data": {
    "actor": {
      "account": {
        "nrql": {
          "results": []
        }
      }
    }
  }
}

API Reference

The API Reference is also available here.

NerdGraph(apiKey: string)

Creates a new instance of the NewRelicNerdGraphAPI class.

Parameters:

  • apiKey The API key to use when making requests to New Relic.

query(options: Object, callback?: Function): Promise

Calls the NerdGraph API with the provided options and either invokes the provided callback or returns a promise.

Parameters:

  • options The options to use when calling the API.
  • options.account The account ID to use when calling the API.
  • options.query The NRQL query to execute.
  • options.async Whether or not to make an asynchronous query.
  • callback The optional callback function to invoke with the results of the query.

Returns:

  • A promise that resolves with the results of the query if no callback is provided.

poll(options: Object, callback?: Function): Promise

Polls a NerdGraph query using the specified options.

Parameters:

  • options The options to use when calling the API.
  • options.account The account ID to use when calling the API.
  • options.queryId The query ID to poll.
  • callback The optional callback function to invoke with the results of the query.

Returns:

  • A promise that resolves with the results of the query if no callback is provided.

Contributing

Contributions to newrelic-nerdgraph-client are most welcome!

If you find a bug or want to suggest a new feature, please open an issue on the GitHub repository. If you want to contribute code, please fork the repository, make your changes, and submit a pull request. Your contributions and feedback are most welcome!

License

This library is authored by @vinitshahdeo and released under the MIT License.

GitHub Twitter Follow

About

An API client for NerdGraph—the GraphQL API of New Relic. Supports both synchronous and asynchronous NRQL queries.

Topics

Resources

License

Stars

Watchers

Forks