forked from rerun-io/rerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_crashes.py
executable file
·168 lines (133 loc) · 4.68 KB
/
fetch_crashes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
"""
Fetch user crashes from PostHog database, deduplicates them, and prefill issue reports for them.
Usage:
```
export POSTHOG_PERSONAL_API_KEY=<create_your_personal_api_key_in_posthog_ui>
python scripts/fetch_crashes.py -v 0.5.0 -v 0.5.1 > crashes.md
```
Optionally, you can filter out data older than a given timestamp:
```
python scripts/fetch_crashes.py -v 0.5.0 -v 0.5.1 --after 2023-05-02T20:17:52 > crashes.md
```
See Also
--------
```
python scripts/fetch_crashes.py --help
```
"""
from __future__ import annotations
import argparse
import json
import os
from collections import defaultdict
import requests
## CLI
parser = argparse.ArgumentParser(description="Fetch user crashes from PostHog database")
parser.add_argument(
"-v",
"--version",
action="append",
dest="versions",
metavar="VERSION",
help="Specify one or more Rerun version",
required=True,
)
parser.add_argument(
"-a",
"--after",
action="append",
dest="date_after_included",
metavar="TIMESTAMP",
help="Filter out data older than this ISO8061 timestamp",
)
args = parser.parse_args()
## Set up query, auth, etc
personal_api_key = os.environ.get("POSTHOG_PERSONAL_API_KEY")
project_id = os.environ.get("POSTHOG_PROJECT_ID", "1954")
url = f"https://eu.posthog.com/api/projects/{project_id}/events"
properties = [
{"key": "email", "value": "is_not_set", "operator": "is_not_set", "type": "person"},
{"key": "rerun_version", "value": args.versions, "operator": "exact", "type": "event"},
]
## Fetch results
# NOTE: For reference, here's the complete event payload:
#
# {
# "id": "01880cc1-10bd-0000-8338-2cc3ffed784b",
# "distinct_id": "7981eecd-f1b9-4446-8824-b854d5474787",
# "properties": {
# "llvm_version": "15.0.6",
# "target": "x86_64-pc-windows-msvc",
# "callstack": "<omitted>",
# "session_id": "f67f53b8-da72-4564-b849-05b048a5b6be",
# "git_hash": "968bf7355ef146c6fad3283835f2d87e7757abc6",
# "rerun_workspace": false,
# "file_line": "wgpu-0.15.1/src/backend/direct.rs:3024",
# "event_id": 1,
# "rerun_version": "0.5.1",
# "rust_version": "1.67.1 (d5a82bbd2 2023-02-07)",
# "debug": false,
# "build_date": "2023-05-02T21:24:20Z"
# },
# "event": "crash-panic",
# "timestamp": "2023-05-11T20:17:52.479000+00:00",
# "person": null,
# "elements": [],
# "elements_chain": ""
# }
results = []
for event in ["crash-panic", "crash-signal"]:
params = {
"properties": json.dumps(properties),
"event": event,
"orderBy": '["-timestamp"]',
}
if args.date_after_included:
params["after"] = args.date_after_included
headers = {"Authorization": f"Bearer {personal_api_key}"}
response = requests.get(url, headers=headers, params=params)
if response.status_code != 200:
print("Request failed with status code:", response.status_code)
exit(1)
results += response.json()["results"]
## Deduplicate results and massage output
backtraces = defaultdict(list)
for res in results:
res["properties"]["timestamp"] = res["timestamp"]
res["properties"]["event"] = res["event"]
res["properties"]["user_id"] = res["distinct_id"]
backtrace = res["properties"].pop("callstack").encode("utf-8").strip()
backtraces[backtrace].append(res.pop("properties"))
def count_uniques(backtrace):
return len({prop["user_id"] for prop in backtrace[1]})
backtraces = list(backtraces.items())
backtraces.sort(key=count_uniques, reverse=True)
## Generate reports
for backtrace, props in backtraces:
n = count_uniques((backtrace, props))
event = "panic" if props[0]["event"] == "crash-panic" else "signal"
file_line = props[0].get("file_line")
signal = props[0].get("signal")
title = file_line if file_line is not None else signal
timestamps = sorted(list({prop["timestamp"] for prop in props}))
first_occurrence = timestamps[0]
last_occurrence = timestamps[-1]
targets = sorted(list({prop["target"] for prop in props}))
rust_versions = sorted(list({prop["rust_version"] for prop in props}))
rerun_versions = sorted(list({prop["rerun_version"] for prop in props}))
print(
f"## {n} distinct user(s) affected by {event} crash @ `{title}`\n"
"\n"
f"- First occurrence: `{first_occurrence}`\n"
f"- Last occurrence: `{last_occurrence}`\n"
f"- Affected Rust versions: `{rust_versions}`\n"
f"- Affected Rerun versions: `{rerun_versions}`\n"
f"- Affected Targets: `{targets}`\n"
"\n"
"Backtrace:\n"
"```\n"
f' {backtrace.decode("utf-8")}\n'
"```\n"
"-------------------------------------------------------------------------------\n"
)