我已经设置了Sentry,并让它自动报告异常,但是我还没有设法让用户反馈提交工作。
python脚本是基于CLI的,所以我希望用户能够直接在命令行窗口中输入错误信息,而不是生成一个网页并使用它,因为在我的用例中,通过CLI是更完美的。
下面的代码是我尝试遵循Sentry user feedback submission API Python documentation的代码
bearer_auth_token是根据Sentry auth API文档获得的
基本代码示例:
organisation_slug = 'cooldev'
project_slug = 'python-app'
bearer_auth_token = 'dcb275d6aaa040e1818326bec567cb0d9c09343171c65451ab53e740fa450314' # with scope project:write
dsn_link = 'https://8ef42a3641fa42d69fdf8e034732a3c5@o357469.ingest.sentry.io/4423355'
import sentry_sdk, requests
sentry_sdk.init(
dsn=dsn_link,
traces_sample_rate=1.0,
environment='testing'
)
def bug_send(event_id, name, email, comments):
url = f'https://sentry.io/api/0/projects/{organisation_slug}/{project_slug}/user-feedback/'
headers = {
'Authorization': f'Bearer {bearer_auth_token}',
'Content-Type': 'application/json'}
data = {
"event_id": str(event_id),
"name": str(name),
"email": str(email),
"comments": str(comments)}
response = requests.post(url, headers=headers, data=str(data))
return response
try:
x = 1 / 0
except Exception as e:
event_id = sentry_sdk.last_event_id()
name = input('Name: ')
email = input('Email: ')
comments = input('Comments: ')
response = bug_send(event_id, name, email, comments)
print(response.status_code, response.reason)
input()输出:
Name: John Doe
Email: john.doe@gmail.com
Comments: It is not working!
400 Bad Request发布于 2020-10-07 02:11:47
删除Content-Type报头起到了作用。新的响应是200 OK,我可以在Sentry中看到反馈
https://stackoverflow.com/questions/64228436
复制相似问题