All Projects → AnthonyBloomer → nrql-simple

AnthonyBloomer / nrql-simple

Licence: other
nrql-simple provides a convenient way to interact with the New Relic Insights query API.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to nrql-simple

newrelic-context
Contains different helpers to make life easier with NewRelic and Context.
Stars: ✭ 21 (+61.54%)
Mutual labels:  newrelic
illuminsight
💡👀 Read EPUB books with built-in insights from wikis, definitions, translations, and Google.
Stars: ✭ 55 (+323.08%)
Mutual labels:  insights
apollo-newrelic-extension
Apollo Server extension library for performing NewRelic transaction traces of graphql requests
Stars: ✭ 16 (+23.08%)
Mutual labels:  newrelic
newrelic-quickstarts
New Relic One quickstarts help accelerate your New Relic journey by providing immediate value for your specific use cases.
Stars: ✭ 46 (+253.85%)
Mutual labels:  newrelic
Datav
📊https://datav.io is a modern APM, provide observability for your business, application and infrastructure. It's also a lightweight alternative to Grafana.
Stars: ✭ 2,757 (+21107.69%)
Mutual labels:  newrelic
internet-affordability
🌍 Dataset that shows the Internet affordability by country (a shocking reality!)
Stars: ✭ 13 (+0%)
Mutual labels:  insights
newrelic-telemetry-sdk-java
Java library for sending telemetry data to New Relic
Stars: ✭ 34 (+161.54%)
Mutual labels:  newrelic
k8s-webhook-cert-manager
Generate certificate suitable for use with any Kubernetes Mutating Webhook.
Stars: ✭ 59 (+353.85%)
Mutual labels:  newrelic
fabric8-analytics-vscode-extension
Red Hat Dependency Analytics extension
Stars: ✭ 125 (+861.54%)
Mutual labels:  insights
nrjmx
Command line tool to connect to a JMX server and retrieve the MBeans it exposes.
Stars: ✭ 28 (+115.38%)
Mutual labels:  newrelic
php7-alpine
Docker container for PHP 7 in Alpine Linux, with almost all extensions that you may need
Stars: ✭ 20 (+53.85%)
Mutual labels:  newrelic
statsbit
NewRelic drop-in replacement on top of Grafana and TimescaleDB
Stars: ✭ 19 (+46.15%)
Mutual labels:  newrelic
Countly Server
Countly helps you get insights from your application. Available self-hosted or on private cloud.
Stars: ✭ 4,857 (+37261.54%)
Mutual labels:  insights
newrelic-telemetry-sdk-python
A python library to send data to New Relic!
Stars: ✭ 23 (+76.92%)
Mutual labels:  newrelic
checkup
A health checkup for your project.
Stars: ✭ 76 (+484.62%)
Mutual labels:  insights
newrelic-cli
Newrelic client written in Python providing both CLI and Python interfaces
Stars: ✭ 26 (+100%)
Mutual labels:  newrelic
tnb-analysis
Gain insights about thenewboston digital crypto currency network by doing some analysis
Stars: ✭ 24 (+84.62%)
Mutual labels:  insights
stateOfVeganism
🌱 Get insights into the current state of Veganism around the world based on global news
Stars: ✭ 26 (+100%)
Mutual labels:  insights
nr1-status-pages
NR1 Status Pages allows you to collect and display the statuses of key dependencies in one place.
Stars: ✭ 31 (+138.46%)
Mutual labels:  newrelic
newrelic-unix-monitor
Monitoring service for Unix (AIX, Linux, HP-UX, MacOS, Solaris) systems
Stars: ✭ 26 (+100%)
Mutual labels:  insights

nrql-simple

Build Status codecov

nrql-simple is a small Python library that provides a convenient way to interact with the New Relic Insights query API. You can interact with this library programmatically or via the Command Line.

Installation

nrql-simple is available on the Python Package Index (PyPI). You can install nrql-simple using pip.

virtualenv env
source env/bin/activate
pip install nrql-simple

To install the development version, run:

pip install https://github.com/AnthonyBloomer/nrql-simple/archive/master.zip

About

The New Relic Insights query API is a REST API for querying Insights event data. After you supply a standard NRQL query via HTTPS request, the query API returns a JSON response for parsing.

To use the API, you need a query key. You can have multiple query keys, and any query key can be used to initiate any Insights API query. If you have multiple systems querying Insights or different data destinations, New Relic recommends you use multiple query keys to enhance data security.

To create a new query key:

  1. Go to insights.newrelic.com > Manage data > API keys.
  2. Select the plus icon next to the Query keys heading.
  3. Enter a short description of the key.
  4. Select Save your notes.

You will also need make note of your New Relic Account ID. To find the account ID for your New Relic account:

  1. Sign in to rpm.newrelic.com.
  2. In the URL bar, copy the number after the /accounts/ portion of the URL: https://rpm.newrelic.com/accounts/ACCOUNT_ID/

Usage

The first step is to initialize a NRQL object and set your API Key and Account ID.

from nrql.api import NRQL
nrql = NRQL()
nrql.api_key = 'YOUR_API_KEY'
nrql.account_id = 'YOUR_ACCOUNT_ID'

Alternatively, you can export your API key and Account ID as environment variables.

$ export NR_API_KEY='YOUR_API_KEY'
$ export NR_ACCOUNT_ID='YOUR_ACCOUNT_ID'

Then simply pass your NRQL statement into the query function. NRQL is a query language similar to SQL that you use to make calls against the New Relic Insights Events database. Refer to the NRQL documentation for examples and usage information.

Consider the following example that gets the unique number of container IDs for each application since this quarter.

req = nrql.query("select uniqueCount(containerId) from NrDailyUsage facet apmAppName since this quarter")
for k in req['facets']:
    print("%s : %s" % (k['name'], k['results'][0]['uniqueCount']))

Command Line Usage

usage: nrql [-h] [--region REGION] [--env ENV] [--filename FILENAME]
                   [--csv] [--verbose]
                   stmt

positional arguments:
  stmt                  The NRQL statement.

optional arguments:
  -h, --help            show this help message and exit
  --region REGION, --r REGION
                        Pass this flag to set your region (EU or US) By
                        default the region is set to US.
  --env ENV, --e ENV    Environment handler.
  --filename FILENAME, --f FILENAME
                        The output CSV filename. Default is events.csv
  --csv, --c            Pass this flag to output the Event data to CSV.
  --verbose, --v        Pass this flag if you want the whole response.

To use the CLI, you must first export your API key and Account ID as environment variables.

$ export NR_API_KEY='YOUR_API_KEY'
$ export NR_ACCOUNT_ID='YOUR_ACCOUNT_ID'

Then, simply call the nrql command with your NRQL statement as an argument.

nrql "select uniqueCount(containerId) from nrdailyusage where apmAppName = 'SinatraApp' since this quarter"

The above command will output JSON formatted like this:

{
    "results": [
        {
            "uniqueCount": 175
        }
    ]
}

By default the output will not include the performanceStats or metadata objects from the response. To output the entire JSON response, pass the --verbose flag.

nrql "select uniqueCount(containerId) from NrDailyUsage facet apmAppName since this quarter" --verbose

Managing multiple accounts

If you wish to easily switch between accounts, you can use the environment class method. If you are using the command line tool use the env command line argument. For example:

from nrql.api import NRQL
nrql = NRQL()
nrql.environment = "PROD"

Or via the command line:

nrql "select uniqueCount(containerId) from NrDailyUsage facet apmAppName since this quarter" --env='PROD'

By default, the program looks for the environment variables NR_API_KEY and NR_ACCOUNT_KEY.

If the env argument is not none, then the program appends the environment string to NR_API_KEY. For example:

NR_API_KEY_PROD

When naming your environment variables, ensure to follow this naming convention.

Output as CSV

To export Event data to a csv file via the CLI, pass the --csv argument, for example:

nrql "select * from Transaction where appName = 'RabbitMQ' since this quarter" --csv 

This will export a csv file (events.csv) to the current working directory.

To change the output file, pass the --filename argument:

nrql "select * from Transaction where appName = 'RabbitMQ' since this quarter" --csv --filename='rabbit.csv'

You can also export Event data programmatically:

nrql = NRQL()
nrql.csv = True
nrql.filename = 'events.csv'

nrql.query("select * from Transaction where appName = 'RabbitMQ' since this quarter")

This will output events.csv to the current working directory.

Tests

The Python unittest module contains its own test discovery function, which you can run from the command line:

 python -m unittest discover tests/

Contributing

  • Fork the project and clone locally.
  • Create a new branch for what you're going to work on.
  • Push to your origin repository.
  • Create a new pull request in GitHub.

Support

Please note that this is offered for use as-is without warranty. You are free to use and modify as needed. It has been created for use with New Relic, but is not a supported product of New Relic.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].