0

我已经设置了 Sentry 并让它自动报告异常,但是我还没有设法让用户反馈提交工作。

python 脚本是基于 CLI 的,所以我希望用户能够直接在命令行窗口中输入错误信息,而不是生成网页并使用它,因为在我的用例中通过 CLI 更加无缝。

下面的代码是我尝试遵循Sentry 用户反馈提交 API Python 文档

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

4

1 回答 1

0

删除Content-Type标题就可以了。新的响应是200 OK,我可以看到 Sentry 中显示的反馈

于 2020-10-06T18:11:47.437 回答