From e7f6c874b74a3fe0d2830c3ea3de34466df50bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Misty=20De=20M=C3=A9o?= Date: Mon, 24 Feb 2025 14:00:16 -0800 Subject: [PATCH] CLI: add new --worker-id option This adds a new commandline flag allowing the worker ID to be specified. If present, it will be added to the global context so that it will be included in every logging statement. Previously, we only had some indirect values to tie logging statements to specific workers, so this should make it easier to follow. --- brozzler/cli.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/brozzler/cli.py b/brozzler/cli.py index 7a7b7b1..34ac014 100755 --- a/brozzler/cli.py +++ b/brozzler/cli.py @@ -80,6 +80,11 @@ def add_common_options(arg_parser, argv=None): action="store_true", help="add syslogd log level prefix for journalctl filtering", ) + arg_parser.add_argument( + "--worker-id", + dest="worker_id", + help="ID for this worker, displayed in logs if provided", + ) # arg_parser.add_argument( # '-s', '--silent', dest='log_level', action='store_const', # default=logging.INFO, const=logging.CRITICAL) @@ -185,6 +190,10 @@ def configure_logging(args): cache_logger_on_first_use=False, ) + # Adds the worker ID to the global binding, if supplied + if args.worker_id is not None: + structlog.contextvars.bind_contextvars(worker_id=args.worker_id) + # We still configure logging for now because its handlers # are used for the gunicorn spawned by the brozzler dashboard. logging.basicConfig(