npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

newrelic-nerdgraph-client

v1.0.1

Published

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

Downloads

1,042

Readme

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

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!

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