【问题标题】:Sentry - Log Flask and Sentry at the same time / same DSNSentry - 同时记录 Flask 和 Sentry / 相同的 DSN
【发布时间】:2016-05-11 09:15:41
【问题描述】:

编辑:标题并添加了一些代码示例。

我正在尝试研究如何使用相同的 DSN 将 Celery worker 和 Flask 日志记录发送到 Sentry。

设置如下:

  • Flask 应用将异步任务发送到 Celery 工作线程。
  • Flask 和 Celery worker 都在链接的 Docker 容器中运行
  • 我们正在使用 Application Factory 模式在 __init__.py 文件中动态创建应用程序实例,这就是当前进行 Celery 和 Sentry 设置工作的地方:

(出于演示目的,代码已被简化。)

celery = Celery(__name__, broker=config[config_name].CELERY_BROKER_URL)
client = Client(dsn={my_dsn}, transport=HTTPTransport, )
sentry = Sentry(client=client)
def create_app(config_filename): 
    app = Flask(__name__)
    app.config.from_pyfile(config_filename)
    celery.conf.update(app.config)
    # https://docs.getsentry.com/hosted/clients/python/integrations/flask/#extended-setup :
    sentry.init_app(app, logging=True, level=logging.INFO, )
    # https://docs.getsentry.com/hosted/clients/python/integrations/celery/ :
    register_logger_signal(client, loglevel=logging.INFO)
    register_signal(client)
    return app

通过关注Sentry Celery instructions,Sentry(用他们自己的话说)“劫持 Celery 错误处理”,并且运行良好。但是,这会在过程中丢弃 Flask 错误处理。

有没有办法注册两个记录器(Flask 和 Celery),以便它们都将日志条目发送到 Sentry?还是应该在 Celery 容器中运行的单独的 celery_worker.py 文件中进行 Celery 注册?

【问题讨论】:

    标签: python logging flask celery sentry


    【解决方案1】:

    我设法做到了:

    app = Flask(__name__)
    celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'], backend=app.config['CELERY_RESULT_BACKEND'], worker_send_task_events=True, task_send_sent_event=True)
    celery.conf.update(app.config)
    
    client = Client(dsn={my_dsn})
    sentry = Sentry(app, dsn={my_dsn})
    register_logger_signal(client)
    register_logger_signal(client, loglevel=logging.INFO)
    register_signal(client)
    register_signal(client, ignore_expected=True)
    

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 2018-08-14
      • 2020-10-01
      • 2016-12-09
      • 2021-08-15
      • 1970-01-01
      • 2021-02-20
      • 2015-04-10
      • 2021-09-02
      相关资源
      最近更新 更多