This is the fourth part of a five-part series on automating common DevOps tasks in New Relic.
The first part shows you how to audit your services with Python and the NerdGraph API, and also includes the sample API code that this post uses. The second part shows you how to automate your tags with Python and the NerdGraph API. The third part shows you how to export telemetry data with Python and the NerdGraph API.
All of the code from this five-part series can be found in this repository.
Management reporting is a common requirement when you’re responsible for the overall reliability of a system, and you may want to export your New Relic dashboards for reporting purposes. You can use New Relic's NerdGraph API, a GraphQL-format API that lets you query New Relic data, to automate this process.
This post shows you how to export dashboards using New Relic Query Language (NRQL) and Python.
Prerequisites
If you plan to follow along with this tutorial and use Python to make your API calls, you need:
- Python 3 installed on your machine. Download and install it from python.org. Note that you can also make API calls to the specified endpoints using languages other than Python as well.
- With Python, you’ll be using the requests and json modules.
- A New Relic account. Sign up for free.
- An application instrumented with New Relic APM and infrastructure. Follow these installation steps to instrument an application.
- A New Relic API key. Follow these steps to get an API key, or, if you’re already signed in, generate an API key on the API keys page. To learn more about querying and configuration, see New Relic API keys.
- The Python starter code, which is in part one of this blog series.
The next video shows you how to export dashboards using the NerdGraph API.
Exporting dashboards
Here's an example query for exporting dashboards using the NerdGraph API. If you need more context on the starter code, see Using Python to make an API call in part one of this series.
import requests
import json
from license import user_key
def nerdgraph_dashboards(key):
# GraphQL query to NerdGraph
query = """mutation { dashboardCreateSnapshotUrl(guid: "XXX") }"""
# NerdGraph endpoint
endpoint = "https://api.newrelic.com/graphql"
headers = {'API-Key': f'{key}'}
response = requests.post(endpoint, headers=headers, json={"query": query})
if response.status_code == 200:
# convert a JSON into an equivalent python dictionary
json_dictionary = json.loads(response.content)
print(json_dictionary)
# only interested with the dashboard url
url_pdf = json_dictionary["data"]["dashboardCreateSnapshotUrl"]
print(url_pdf)
# replace PDF with PNG, and get the link to download the file
url_png = url_pdf[:-3] + "PNG"
print(url_png)
# rename the downloaded file, and save it in the working directory
dashboard_response = requests.get(url_png, stream=True)
open('dashboard_example.png', 'wb').write(dashboard_response.content)
# optional - serialize object as a JSON formatted stream
# json_response = json.dumps(response.json()["data"]["dashboardCreateSnapshotUrl"], indent=2)
# print(json_response)
else:
# raise an error with a HTTP response code
raise Exception(f'Nerdgraph query failed with a {response.status_code}.')
nerdgraph_dashboards(user_key)
Note that you would replace the "XXX"
in (guid: "XXX")
with the GUID of the dashboard you'd like to export. You can also switch out guid
for other dashboard properties in your own queries depending on your needs.
The previous code example shows how to export the dashboard as a PDF:
url_pdf = json_dictionary["data"]["dashboardCreateSnapshotUrl"]
print(url_pdf)
The example also includes a snippet that allows you to export the dashboard as a PNG:
url_png = url_pdf[:-3] + "PNG"
print(url_png)
To learn more, see the NerdGraph tutorial on exporting dashboards.
Points clés
Learn what else you can do with the Nerdgraph API and continue to build DevOps into your observability practice. If you don't have a New Relic account yet, sign up today. Your free account includes 100 GB/month of free data ingest, one full-platform user, and unlimited free basic users.
Read the next part in this 5-part series: Rotate your license keys with Python and the NerdGraph API.
Les opinions exprimées sur ce blog sont celles de l'auteur et ne reflètent pas nécessairement celles de New Relic. Toutes les solutions proposées par l'auteur sont spécifiques à l'environnement et ne font pas partie des solutions commerciales ou du support proposés par New Relic. Veuillez nous rejoindre exclusivement sur l'Explorers Hub (discuss.newrelic.com) pour toute question et assistance concernant cet article de blog. Ce blog peut contenir des liens vers du contenu de sites tiers. En fournissant de tels liens, New Relic n'adopte, ne garantit, n'approuve ou n'approuve pas les informations, vues ou produits disponibles sur ces sites.