Skip to content
Snippets Groups Projects
Unverified Commit a1ed944c authored by Robert Raposa's avatar Robert Raposa Committed by GitHub
Browse files

add temp waffle flag default metric (#24284)

The argument flag_undefined_default is soon to be retired
once ARCHBOM-132 is closed. The following will be used to
help ensure the rollout is complete.

- Add a temporary metric if flag_undefined_default is used.
- Add deprecation warning for flag_undefined_default.
- Add minor fix for waffle flag metric when no request found.

ARCHBOM-132
parent 950be924
Branches
Tags release-2019-06-19-11.15
No related merge requests found
......@@ -56,6 +56,7 @@ can't yet be deleted, for example if there are still course overrides.
"""
import logging
import warnings
from abc import ABCMeta
from contextlib import contextmanager
......@@ -254,6 +255,13 @@ class WaffleFlagNamespace(six.with_metaclass(ABCMeta, WaffleNamespace)):
# Import is placed here to avoid model import at project startup.
from waffle.models import Flag
if flag_undefined_default:
warnings.warn(
# NOTE: This will be removed once ARCHBOM-132, currently in-progress, is complete.
'flag_undefined_default has been deprecated. For existing uses this is already actively being fixed.',
DeprecationWarning
)
# validate arguments
namespaced_flag_name = self._namespaced_name(flag_name)
value = None
......@@ -271,6 +279,10 @@ class WaffleFlagNamespace(six.with_metaclass(ABCMeta, WaffleNamespace)):
try:
Flag.objects.get(name=namespaced_flag_name)
except Flag.DoesNotExist:
if flag_undefined_default:
# This metric will go away once this has been fully retired with ARCHBOM-132.
# Also, even though the value will only track the last flag, that should be enough.
set_custom_metric('temp_flag_default_used', namespaced_flag_name)
value = flag_undefined_default
if value is None:
......@@ -279,12 +291,15 @@ class WaffleFlagNamespace(six.with_metaclass(ABCMeta, WaffleNamespace)):
value = flag_is_active(request, namespaced_flag_name)
else:
log.warning(u"%sFlag '%s' accessed without a request", self.log_prefix, namespaced_flag_name)
set_custom_metric('warn_flag_no_request', True)
# Return the default value if not in a request context.
# Note: this skips the cache as the value might be different
# in a normal request context. This case seems to occur when
# a page redirects to a 404. In this case, we'll just return
# the default value.
return bool(flag_undefined_default)
value = bool(flag_undefined_default)
self._set_waffle_flag_metric(namespaced_flag_name, value)
return value
self._cached_flags[namespaced_flag_name] = value
......
......@@ -127,7 +127,11 @@ class TestCourseWaffleFlag(TestCase):
self.TEST_COURSE_KEY
)
self._assert_waffle_flag_metric(mock_set_custom_metric, expected_flag_value=str(data['result']))
self._assert_waffle_flag_metric(
mock_set_custom_metric,
expected_flag_value=str(data['result']),
flag_undefined_default=data['flag_undefined_default'],
)
@ddt.data(
{'flag_undefined_default': None, 'result': False},
......@@ -165,11 +169,12 @@ class TestCourseWaffleFlag(TestCase):
self.assertEqual(mock_set_custom_metric.call_count, data['expected_count'])
def _assert_waffle_flag_metric(self, mock_set_custom_metric, expected_flag_value=None):
def _assert_waffle_flag_metric(self, mock_set_custom_metric, expected_flag_value=None, flag_undefined_default=None):
if expected_flag_value:
expected_calls = [call(self.NAMESPACED_FLAG_NAME, expected_flag_value)]
mock_set_custom_metric.assert_has_calls(expected_calls)
self.assertEqual(mock_set_custom_metric.call_count, 1)
expected_call_count = 2 if flag_undefined_default else 1
self.assertEqual(mock_set_custom_metric.call_count, expected_call_count)
else:
self.assertEqual(mock_set_custom_metric.call_count, 0)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment