Package: nrql-simple
nrql-simple is a small Python library that provides a convenient way to
interact with theNew Relic Insights query
API.
You can interact with this library programmatically or via the Command
Line.Installationnrql-simple is available on thePython Package Index
(PyPI). You can install
nrql-simple using pip.virtualenv env
source env/bin/activate
pip install nrql-simpleTo install the development version, run:pip install https://github.com/AnthonyBloomer/nrql-simple/archive/master.zipAboutThe 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:Go toinsights.newrelic.com>
Manage data > API keys.Select the plus icon next to the Query keys heading.Enter a short description of the key.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:Sign in torpm.newrelic.com.In the URL bar, copy the number after the /accounts/ portion of the
URL:https://rpm.newrelic.com/accounts/ACCOUNT_ID/UsageThe first step is to initialize a NRQL object and set your API Key and
Account ID.fromnrql.apiimportNRQLnrql=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 thequeryfunction. NRQL
is a query language similar to SQL that you use to make calls against
the New Relic Insights Events database. Refer to theNRQL
documentationfor 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")forkinreq['facets']:print("%s:%s"%(k['name'],k['results'][0]['uniqueCount']))Command Line Usageusage: 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 thenrqlcommand 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 theperformanceStatsormetadataobjects from the response. To output the entire JSON
response, pass the--verboseflag.nrql "select uniqueCount(containerId) from NrDailyUsage facet apmAppName since this quarter" --verboseManaging multiple accountsIf you wish to easily switch between accounts, you can use theenvironmentclass method. If you are using the command line tool use
theenvcommand line argument. For example:fromnrql.apiimportNRQLnrql=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 variablesNR_API_KEYandNR_ACCOUNT_KEY.If theenvargument is not none, then the program appends the
environment string toNR_API_KEY. For example:NR_API_KEY_PRODWhen naming your environment variables, ensure to follow this naming
convention.Output as CSVTo export Event data to a csv file via the CLI, pass the--csvargument, for example:nrql "select * from Transaction where appName = 'RabbitMQ' since this quarter" --csvThis will export a csv file (events.csv) to the current working
directory.To change the output file, pass the--filenameargument: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=Truenrql.filename='events.csv'nrql.query("select * from Transaction where appName = 'RabbitMQ' since this quarter")This will outputevents.csvto the current working directory.TestsThe Pythonunittestmodule contains its own test discovery function,
which you can run from the command line:python -m unittest discover tests/ContributingFork 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.