Last active
October 9, 2019 20:00
-
-
Save millerdev/3d0a541f90b9817c3be5dd9bffed288e to your computer and use it in GitHub Desktop.
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
#! /usr/bin/env python | |
"""Download events form Sentry as CSV | |
https://gist.github.com/millerdev/3d0a541f90b9817c3be5dd9bffed288e | |
""" | |
import sys | |
import csv | |
from argparse import ArgumentParser | |
import requests | |
ISSUES_URL_BASE = "https://sentry.io/api/0/issues" | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument("auth_token", | |
help="See https://sentry.io/settings/account/api/auth-tokens/") | |
parser.add_argument("issue_id", help="Comma-delimited list of sentry issue ids") | |
parser.add_argument("-o", "--output-path", default="output.csv") | |
args = parser.parse_args() | |
issues = sorted(set(args.issue_id.split(","))) | |
tags = set() | |
for issue_id in issues: | |
info = get_issue_info(issue_id, args.auth_token) | |
tags.update(item["key"] for item in info["tags"]) | |
tags = sorted(tags) | |
print(f"writing events to {args.output_path}", file=sys.stderr) | |
with open(args.output_path, 'w') as csvfile: | |
for i, issue_id in enumerate(issues): | |
write_events(csvfile, issue_id, args.auth_token, tags, i == 0) | |
def get_issue_info(issue_id, auth_token): | |
url = f"{ISSUES_URL_BASE}/{issue_id}/" | |
resp = fetch_url(url, auth_token) | |
return resp.json() | |
def write_events(csvfile, issue_id, auth_token, tags, write_header=True): | |
url = f"{ISSUES_URL_BASE}/{issue_id}/events/" | |
fields = [ | |
'timestamp', | |
'issue_id', | |
'event_id', | |
'title', | |
'username', | |
] + tags | |
writer = csv.DictWriter(csvfile, fieldnames=fields) | |
if write_header: | |
writer.writeheader() | |
while True: | |
resp = fetch_url(url, auth_token) | |
events = resp.json() | |
if not events: | |
break | |
print(f'{url} -> {len(events)} events', file=sys.stderr) | |
for event in events: | |
writer.writerow(make_row(event, tags)) | |
if 'next' not in resp.links: | |
break | |
url = resp.links['next']['url'] | |
def fetch_url(url, auth_token): | |
auth = {'Authorization': 'Bearer {}'.format(auth_token)} | |
resp = requests.get(url, headers=auth) | |
resp.raise_for_status() | |
return resp | |
def make_row(event, tags): | |
row = { | |
'timestamp': event['dateCreated'], | |
'issue_id': event['groupID'], | |
'event_id': event['id'], | |
'title': event['title'], | |
'username': event['user']['username'], | |
} | |
tags_by_key = {tag["key"]: tag["value"] for tag in event['tags']} | |
for key in tags: | |
row[key] = tags_by_key.get(key, "") | |
return row | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment