编辑:标题并添加一些代码示例。
我正在尝试弄清楚如何使用相同的 DSN 将 Celery 工作程序和 Flask 日志记录发送到 Sentry。
设置如下:
- Flask 应用向 Celery Worker 发送异步任务。
- 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 容器内运行的单独的 celery_worker.py
文件中进行?
最佳答案
我设法这样做:
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)
关于python - Sentry - 同时记录 Flask 和 Sentry/相同的 DSN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35155634/