New Relic Now Start training on Intelligent Observability February 25th.
Save your seat.
No momento, esta página está disponível apenas em inglês.

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)