Hit REST in Python to get data from New Relic

Hello.

Here is an example script that creates a REST client using Python's requests library and retrieves information from New Relic. However, since it is a general REST client, there may be few people who can refer to it. ..

1. What you need

--python execution environment (my environment is 2.7.12)

[ec2-user@xxx ~]$ python --version
Python 2.7.12

--python requests library (installed with the following command)

[ec2-user@xxx ~]$ pip install requests

--Something you are monitoring with New Relic (this time a Python application)

2. Sample code

Since error handling etc. are broken, please add as necessary.

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import requests

# def getAppInfo( sApiKey, sAppID ):
def getAppInfo( sApiKey ):

    sURL = "https://api.newrelic.com/v2/applications.json"
    sHeaders = { 'X-Api-Key': sApiKey }

    stRes = requests.get( sURL, headers=sHeaders )
    # stData = stRes.json()

    return stRes


if __name__ == '__main__':

    sApiKey = "{API Key}"
    # sAppID = "{Application ID}"

    # stRes = getAppInfo( sApiKey, sAppID )
    stRes = getAppInfo( sApiKey )

    print stRes.text

3. Execution result

Since the text of the execution result of the get method of requests is simply output as it is, it is formatted and displayed by mjson.tool.

apm_gray_status.png

{
    "applications": [
        {
            "health_status": "gray",
            "id": 12345678,
            "language": "python",
Omission
            "name": "Python Agent Test",
            "reporting": false,
            "settings": {
                "app_apdex_threshold": 0.5,
                "enable_real_user_monitoring": true,
                "end_user_apdex_threshold": 7.0,
                "use_server_side_config": false
            }
        }
    ],
    "links": {
Omission
    }
}

It's not a good example because there is only one application and the state is not healthy (gray), but you can see that you can get the information of the application. Other New Relic APIs can be hit in the same way with this application.

There seems to be a library like this, so it seems that you can implement a REST client more easily. I would like to write an article if I try this area as well.

Well then.

Recommended Posts

Hit REST in Python to get data from New Relic
Hit the New Relic API in Python to get the server status
Get data from Quandl in Python
Get time series data from k-db.com in Python
Get LEAD data using Marketo's REST API in Python
Get data from GPS module at 10Hz in Python
Get Leap Motion data in Python.
How to get a string from a command line argument in python
Get exchange rates from open exchange rates in Python
[Note] Get data from PostgreSQL with Python
Get Google Fit API data in Python
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
Get battery level from SwitchBot in Python
Convert from Markdown to HTML in Python
Get Precipitation Probability from XML in Python
Get metric history from MLflow in Python
How to get a value from a parameter store in lambda (using python)
How to get results from id in Celery
From file to graph drawing in Python. Elementary elementary
[Python] How to read data from CIFAR-10 and CIFAR-100
[Bash] Use here-documents to get python power from bash
Get pointers to struct members in Python ctypes
What to do to get google spreadsheet in python
Get only articles from web pages in Python
Use os.getenv to get environment variables in Python
Get users belonging to your organization from Garoon REST API with Python + Requests
Use PIL in Python to extract only the data you want from Exif
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
What's new in Python 3.5
New in Python 3.4.0 (1)-pathlib
Get date in Python
What's new in Python 3.6
How to get the variable name itself in python
Summary of tools needed to analyze data in Python
How to get the number of digits in Python
How to scrape image data from flickr with python
Receive dictionary data from a Python program in AppleScript
App development to tweet in Python from Visual Studio 2017
Send data from Python to Processing via socket communication
How to download files from Selenium in Python in Chrome
Hit Watson's REST API from Python on IBM Bluemix
Get data from database via ODBC with Python (Access)
Generate Word Cloud from case law data in python3
I want to use a python data source in Re: Dash to get query results
[Impression] [Data analysis starting from zero] Introduction to Python data science learned in business cases
Get YouTube Comments in Python
Get structural data from CHEMBLID
Handle Ambient data in Python
Post from Python to Slack
I tried to create API list.csv in Python from swagger.yaml
Get last month in python
How to slice a block multiple array from a multiple array in Python
Get your heart rate from the fitbit API in Python!
What's new in Python 3.10 (Summary)
Display UTM-30LX data in Python
Login to website in Python
Get Youtube data with python
OCR from PDF in Python
How to create an instance of a particular class from dict using __new__ () in python