Last active
March 31, 2021 09:52
-
-
Save gspncr/b2f5316f32b27713e62bcb73467ed883 to your computer and use it in GitHub Desktop.
run new relic nerdgraph query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, json, csv | |
graphQLEndpoint = 'https://api.newrelic.com/graphql' | |
#graphQLEndpoint = 'https://api.eu.newrelic.com/graphql' | |
APIKey = '<NRAK Key>' | |
headers = { | |
"Content-Type": 'application/json', | |
"API-Key": APIKey | |
} | |
query = """ | |
{ | |
actor { | |
account(id: 1147177) { | |
nrql(query: "FROM PageView SELECT apdex(duration) facet appName since 7 days ago") { | |
results | |
} | |
} | |
} | |
} | |
""" | |
request = requests.post( | |
graphQLEndpoint, | |
json={'query': query}, | |
headers=headers | |
) | |
def runQuery(request): | |
jsonpayload = json.loads(request.text) | |
entries = len(jsonpayload["data"]["actor"]["account"]["nrql"]["results"]) | |
for x in range(entries): | |
print(jsonpayload["data"]["actor"]["account"]["nrql"]["results"][x]["facet"], | |
jsonpayload["data"]["actor"]["account"]["nrql"]["results"][x]["score"]) | |
runQuery(request) | |
def makeCSV(request): | |
with open('apdex.csv', 'w', newline='') as csvfile: | |
fieldnames = ['Name', 'Score'] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
jsonpayload = json.loads(request.text) | |
entries = len(jsonpayload["data"]["actor"]["account"]["nrql"]["results"]) | |
writer.writeheader() | |
for x in range(entries): | |
writer.writerow({'Name': jsonpayload["data"]["actor"]["account"]["nrql"]["results"][x]["facet"], 'Score': jsonpayload["data"]["actor"]["account"]["nrql"]["results"][x]["score"]}) | |
makeCSV(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment