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.
This commit is contained in:
Misty De Méo 2025-02-24 14:00:16 -08:00
parent e6d0de8f3c
commit e7f6c874b7

View File

@ -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(