56

Export Dashboards with Python and NerdGraph API | New Relic

 2 years ago
source link: https://newrelic.com/blog/how-to-relic/python-export-dashboards
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Export Dashboards with Python and NerdGraph API Skip to main content

Join us at FutureStack May 17-19 for hands-on training, 50+ breakouts, and a keynote with the legendary Steve Wozniak. Register now

This is the fourth part of a five-part series on automating common DevOps tasks in New Relic One.

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:

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)

Top takeaways

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 One 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.


Steve Ng is Principal Developer Relations Lead of APJ at New Relic.

The views expressed on this blog are those of the author and do not necessarily reflect the views of New Relic. Any solutions offered by the author are environment-specific and not part of the commercial solutions or support offered by New Relic. Please join us exclusively at the Explorers Hub (discuss.newrelic.com) for questions and support related to this blog post. This blog may contain links to content on third-party sites. By providing such links, New Relic does not adopt, guarantee, approve or endorse the information, views or products available on such sites.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK