Only log hub / singleuser version mismatch once

If your jupyterhub and jupyterhub-singleuser instances
are running at different minor or greater versions a
warning gets logged per active server which can be a lot
when you have hundreds of active servers.

This adds a flag to that version mismatch logging logic
such that the warning is only logged once per restart
of the hub server.

Closes issue #2970
This commit is contained in:
Matt Riedemann
2020-03-04 11:40:23 -05:00
parent b4391d0f79
commit 08eee9309e
2 changed files with 42 additions and 7 deletions

View File

@@ -18,6 +18,15 @@ version_info = (
__version__ = ".".join(map(str, version_info[:3])) + ".".join(version_info[3:])
# Singleton flag to only log the major/minor mismatch warning once.
_version_mismatch_warning_logged = False
def reset_globals():
"""Used to reset globals between test cases."""
global _version_mismatch_warning_logged
_version_mismatch_warning_logged = False
def _check_version(hub_version, singleuser_version, log):
"""Compare Hub and single-user server versions"""
@@ -42,14 +51,21 @@ def _check_version(hub_version, singleuser_version, log):
hub_major_minor = V(hub_version).version[:2]
singleuser_major_minor = V(singleuser_version).version[:2]
extra = ""
do_log = True
if singleuser_major_minor == hub_major_minor:
# patch-level mismatch or lower, log difference at debug-level
# because this should be fine
log_method = log.debug
else:
# log warning-level for more significant mismatch, such as 0.8 vs 0.9, etc.
global _version_mismatch_warning_logged
if _version_mismatch_warning_logged:
do_log = False # We already logged this warning so don't log it again.
else:
log_method = log.warning
extra = " This could cause failure to authenticate and result in redirect loops!"
_version_mismatch_warning_logged = True
if do_log:
log_method(
"jupyterhub version %s != jupyterhub-singleuser version %s." + extra,
hub_version,

View File

@@ -4,6 +4,11 @@ import logging
import pytest
from .._version import _check_version
from .._version import reset_globals
def setup_function(function):
reset_globals()
@pytest.mark.parametrize(
@@ -25,3 +30,17 @@ def test_check_version(hub_version, singleuser_version, log_level, msg, caplog):
record = caplog.records[0]
assert record.levelno == log_level
assert msg in record.getMessage()
def test_check_version_singleton(caplog):
"""Tests that minor version difference logging is only logged once."""
# Run test_check_version twice which will assert that the warning is only logged
# once.
for x in range(2):
test_check_version(
'1.2.0',
'1.1.0',
logging.WARNING,
'This could cause failure to authenticate',
caplog,
)