Created
February 1, 2017 00:16
-
-
Save stantonk/f5a4a2380bfaf97b686a77ca87b4c212 to your computer and use it in GitHub Desktop.
beginning of grabbing stats from NewRelic API
This file contains hidden or 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 json | |
import logging | |
import requests | |
import sys | |
from collections import namedtuple | |
from functools import partial | |
from urlparse import urlparse | |
from urlparse import parse_qs | |
logging.basicConfig(**{ | |
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
'level': logging.DEBUG | |
}) | |
logger = logging.getLogger() | |
headers = {'X-Api-Key': 'REDACTED'} | |
APP_ID = REDACTED | |
BASE = 'https://api.newrelic.com/v2/' | |
get = partial(requests.get, headers=headers) | |
post = partial(requests.post, headers=headers) | |
Pagination = namedtuple('Pagination', 'first prev next last') | |
def get_pagination(raw_link_header): | |
link_map = {} | |
for link, rel in (lh.split(';') for lh in raw_link_header.split(',')): | |
link_map[rel.split('=')[1].strip('"')] = link.strip(' <>') | |
return Pagination(*(link_map.get(f) for f in Pagination._fields)) | |
r = get(BASE + 'applications/%(app_id)s.json' % {'app_id': APP_ID}) | |
app = r.json()['application'] | |
app_summary = app['application_summary'] | |
last_reported_at = app['last_reported_at'] | |
print last_reported_at | |
print 'throughput: %s' % app_summary['throughput'] | |
print 'response_time: %s' % app_summary['response_time'] | |
print 'apdex_score: %s' % app_summary['apdex_score'] | |
print 'error_rate: %s' % app_summary['error_rate'] | |
endpoint_metrics = [] | |
endpoint_error_metrics = [] | |
url = BASE + 'applications/%(app_id)s/metrics.json?' % {'app_id': APP_ID} | |
while True: | |
r = get(url) | |
if not r.ok: | |
print r | |
sys.exit(1) | |
metrics = r.json()['metrics'] | |
pagination = get_pagination(r.headers['Link']) | |
print pagination | |
contains_cursor = 'cursor' in parse_qs(urlparse(pagination.next).query) if pagination.next else False | |
if not contains_cursor: | |
break | |
else: | |
url = pagination.next | |
endpoints = filter(lambda m: m['name'].startswith('WebTransaction/Function'), metrics) | |
endpoints_errors = filter(lambda m: m['name'].startswith('Errors/WebTransaction/Function'), metrics) | |
print len(metrics), len(endpoints), len(endpoints_errors) | |
endpoint_metrics.extend(endpoints) | |
endpoint_error_metrics.extend(endpoints_errors) | |
print len(endpoint_metrics), len(endpoint_error_metrics) | |
for epm in endpoint_metrics: | |
print epm | |
for epem in endpoint_error_metrics: | |
print epem | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment