编辑:标题并添加了一些代码示例。
我正在尝试找出如何使用相同的DSN将Celery worker和Flask日志记录发送到Sentry。
设置如下:
__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
文件中进行?
答案 0 :(得分: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)