Skip to content

Commit

Permalink
evaluation log update: hashed value + max 10 length lists
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-cat committed Aug 30, 2023
1 parent b7fa936 commit 0ad97c0
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 59 deletions.
4 changes: 2 additions & 2 deletions configcatclient/configcatclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .configservice import ConfigService
from .constants import TARGETING_RULES, VARIATION_ID, PERCENTAGE_OPTIONS, FEATURE_FLAGS, SERVED_VALUE
from .evaluationdetails import EvaluationDetails
from .logbuilder import LogBuilder
from .evaluationlogbuilder import EvaluationLogBuilder
from .interfaces import ConfigCatClientException
from .logger import Logger
from .configfetcher import ConfigFetcher
Expand Down Expand Up @@ -373,7 +373,7 @@ def __evaluate(self, key, user, default_value, default_variation_id, config, fet
user = user if user is not None else self._default_user

# Skip building the evaluation log if it won't be logged.
log_builder = LogBuilder() if self.log.isEnabledFor(logging.INFO) else None
log_builder = EvaluationLogBuilder() if self.log.isEnabledFor(logging.INFO) else None

value, variation_id, rule, percentage_rule, error = self._rollout_evaluator.evaluate(
key=key,
Expand Down
50 changes: 50 additions & 0 deletions configcatclient/evaluationlogbuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class EvaluationLogBuilder(object):
def __init__(self):
self.indent_level = 0
self.text = ''

@staticmethod
def trunc_comparison_value_if_needed(comparator_text, comparison_value):
if '(hashed)' in comparator_text:
if isinstance(comparison_value, list):
length = len(comparison_value)
if length > 1:
return '[<{} hashed values>]'.format(length)
return '[<{} hashed value>]'.format(length)

return '<hashed value>'

if isinstance(comparison_value, list):
limit = 10
length = len(comparison_value)
if length > limit:
remaining = length - limit
if remaining == 1:
more_text = "(1 more value)"
else:
more_text = "({} more values)".format(remaining)

return str(comparison_value[:limit])[:-1] + ', ... ' + more_text + ']'

return str(comparison_value)

def increase_indent(self):
self.indent_level += 1
return self

def decrease_indent(self):
self.indent_level = max(0, self.indent_level - 1)
return self

def append(self, text):
self.text += text
return self

def new_line(self, text=None):
self.text += '\n' + ' ' * self.indent_level
if text:
self.text += text
return self

def __str__(self):
return self.text
25 changes: 0 additions & 25 deletions configcatclient/logbuilder.py

This file was deleted.

15 changes: 4 additions & 11 deletions configcatclient/rolloutevaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import semver

from .evaluationcontext import EvaluationContext
from .evaluationlogbuilder import EvaluationLogBuilder
from .logger import Logger

from .constants import TARGETING_RULES, VALUE, VARIATION_ID, COMPARISON_ATTRIBUTE, \
Expand Down Expand Up @@ -215,19 +216,11 @@ def evaluate(self, key, user, default_value, default_variation_id, config, log_b
self.log.error(error, *error_args, event_id=2001)
return default_value, default_variation_id, None, None, Logger.format(error, error_args)

def _trunc_if_needed(self, comparator, comparison_value):
if '(hashed)' in self.COMPARATOR_TEXTS[comparator]:
if isinstance(comparison_value, list):
return [item[:10] + '...' for item in comparison_value]

return comparison_value[:10] + '...'

return comparison_value

def _format_rule(self, comparison_attribute, comparator, comparison_value):
comparator_text = self.COMPARATOR_TEXTS[comparator]
return 'User.%s %s %s' \
% (comparison_attribute, self.COMPARATOR_TEXTS[comparator],
self._trunc_if_needed(comparator, comparison_value))
% (comparison_attribute, comparator_text,
EvaluationLogBuilder.trunc_comparison_value_if_needed(comparator_text, comparison_value))

def _handle_invalid_user_attribute(self, comparison_attribute, comparator, comparison_value, key, validation_error):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WARNING [3003] Cannot evaluate condition (User.Email IS ONE OF (hashed) ['265522bb68...', '72ff4554fa...']) for setting 'stringIsInDogDefaultCat' (the User.Email attribute is missing). You should set the User.Email attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
WARNING [3003] Cannot evaluate condition (User.Email IS ONE OF (hashed) [<2 hashed values>]) for setting 'stringIsInDogDefaultCat' (the User.Email attribute is missing). You should set the User.Email attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
INFO [5000] Evaluating 'stringIsInDogDefaultCat' for User '{"Identifier":"12345","Custom1":"admin"}'
Evaluating targeting rules and applying the first match if any:
- IF User.Email IS ONE OF (hashed) ['265522bb68...', '72ff4554fa...'] THEN 'Dog' => cannot evaluate, the User.Email attribute is missing
- IF User.Email IS ONE OF (hashed) [<2 hashed values>] THEN 'Dog' => cannot evaluate, the User.Email attribute is missing
The current targeting rule is ignored and the evaluation continues with the next rule.
- IF User.Custom1 IS ONE OF (hashed) ['3eec7c82dd...'] THEN 'Dog' => MATCH, applying rule
- IF User.Custom1 IS ONE OF (hashed) [<1 hashed value>] THEN 'Dog' => MATCH, applying rule
Returning 'Dog'.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
WARNING [3003] Cannot evaluate condition (User.Email IS ONE OF (hashed) ['265522bb68...', '72ff4554fa...']) for setting 'stringIsInDogDefaultCat' (the User.Email attribute is missing). You should set the User.Email attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
WARNING [3003] Cannot evaluate condition (User.Custom1 IS ONE OF (hashed) ['3eec7c82dd...']) for setting 'stringIsInDogDefaultCat' (the User.Custom1 attribute is missing). You should set the User.Custom1 attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
WARNING [3003] Cannot evaluate condition (User.Email IS ONE OF (hashed) [<2 hashed values>]) for setting 'stringIsInDogDefaultCat' (the User.Email attribute is missing). You should set the User.Email attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
WARNING [3003] Cannot evaluate condition (User.Custom1 IS ONE OF (hashed) [<1 hashed value>]) for setting 'stringIsInDogDefaultCat' (the User.Custom1 attribute is missing). You should set the User.Custom1 attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
INFO [5000] Evaluating 'stringIsInDogDefaultCat' for User '{"Identifier":"12345"}'
Evaluating targeting rules and applying the first match if any:
- IF User.Email IS ONE OF (hashed) ['265522bb68...', '72ff4554fa...'] THEN 'Dog' => cannot evaluate, the User.Email attribute is missing
- IF User.Email IS ONE OF (hashed) [<2 hashed values>] THEN 'Dog' => cannot evaluate, the User.Email attribute is missing
The current targeting rule is ignored and the evaluation continues with the next rule.
- IF User.Custom1 IS ONE OF (hashed) ['3eec7c82dd...'] THEN 'Dog' => cannot evaluate, the User.Custom1 attribute is missing
- IF User.Custom1 IS ONE OF (hashed) [<1 hashed value>] THEN 'Dog' => cannot evaluate, the User.Custom1 attribute is missing
The current targeting rule is ignored and the evaluation continues with the next rule.
Returning 'Cat'.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
WARNING [3001] Cannot evaluate targeting rules and % options for setting 'stringIsInDogDefaultCat' (User Object is missing). You should pass a User Object to the evaluation methods like `get_value()` in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
INFO [5000] Evaluating 'stringIsInDogDefaultCat'
Evaluating targeting rules and applying the first match if any:
- IF User.Email IS ONE OF (hashed) ['265522bb68...', '72ff4554fa...'] THEN 'Dog' => cannot evaluate, User Object is missing
- IF User.Email IS ONE OF (hashed) [<2 hashed values>] THEN 'Dog' => cannot evaluate, User Object is missing
The current targeting rule is ignored and the evaluation continues with the next rule.
- IF User.Custom1 IS ONE OF (hashed) ['3eec7c82dd...'] THEN 'Dog' => cannot evaluate, User Object is missing
- IF User.Custom1 IS ONE OF (hashed) [<1 hashed value>] THEN 'Dog' => cannot evaluate, User Object is missing
The current targeting rule is ignored and the evaluation continues with the next rule.
Returning 'Cat'.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WARNING [3003] Cannot evaluate condition (User.Email IS ONE OF (hashed) ['265522bb68...', '72ff4554fa...']) for setting 'stringIsInDogDefaultCat' (the User.Email attribute is missing). You should set the User.Email attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
WARNING [3003] Cannot evaluate condition (User.Email IS ONE OF (hashed) [<2 hashed values>]) for setting 'stringIsInDogDefaultCat' (the User.Email attribute is missing). You should set the User.Email attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
INFO [5000] Evaluating 'stringIsInDogDefaultCat' for User '{"Identifier":"12345","Custom1":"user"}'
Evaluating targeting rules and applying the first match if any:
- IF User.Email IS ONE OF (hashed) ['265522bb68...', '72ff4554fa...'] THEN 'Dog' => cannot evaluate, the User.Email attribute is missing
- IF User.Email IS ONE OF (hashed) [<2 hashed values>] THEN 'Dog' => cannot evaluate, the User.Email attribute is missing
The current targeting rule is ignored and the evaluation continues with the next rule.
- IF User.Custom1 IS ONE OF (hashed) ['3eec7c82dd...'] THEN 'Dog' => no match
- IF User.Custom1 IS ONE OF (hashed) [<1 hashed value>] THEN 'Dog' => no match
Returning 'Cat'.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
WARNING [3001] Cannot evaluate targeting rules and % options for setting 'emailAnd' (User Object is missing). You should pass a User Object to the evaluation methods like `get_value()` in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/
INFO [5000] Evaluating 'emailAnd'
Evaluating targeting rules and applying the first match if any:
- IF User.Email STARTS WITH ANY OF (hashed) ['4_489600ff...'] => false, skipping the remaining AND conditions
- IF User.Email STARTS WITH ANY OF (hashed) [<1 hashed value>] => false, skipping the remaining AND conditions
THEN 'Dog' => cannot evaluate, User Object is missing
The current targeting rule is ignored and the evaluation continues with the next rule.
Returning 'Cat'.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
INFO [5000] Evaluating 'emailAnd' for User '{"Identifier":"12345","Email":"[email protected]"}'
Evaluating targeting rules and applying the first match if any:
- IF User.Email STARTS WITH ANY OF (hashed) ['4_489600ff...'] => true
- IF User.Email STARTS WITH ANY OF (hashed) [<1 hashed value>] => true
AND User.Email CONTAINS ANY OF ['@'] => true
AND User.Email ENDS WITH ANY OF (hashed) ['20_be728e1...'] => false, skipping the remaining AND conditions
AND User.Email ENDS WITH ANY OF (hashed) [<1 hashed value>] => false, skipping the remaining AND conditions
THEN 'Dog' => no match
Returning 'Cat'.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ INFO [5000] Evaluating 'dependentFeature' for User '{"Identifier":"12345","Email
(
Evaluating prerequisite flag 'mainFeature':
Evaluating targeting rules and applying the first match if any:
- IF User.Email ENDS WITH ANY OF (hashed) ['21_57e6ffe...'] => false, skipping the remaining AND conditions
- IF User.Email ENDS WITH ANY OF (hashed) [<1 hashed value>] => false, skipping the remaining AND conditions
THEN 'private' => no match
- IF User.Country IS ONE OF (hashed) ['172faabf6a...'] => true
- IF User.Country IS ONE OF (hashed) [<1 hashed value>] => true
AND User IS NOT IN SEGMENT 'Beta Users'
(
Evaluating segment 'Beta Users':
- IF User.Email IS ONE OF (hashed) ['53b705ed36...', '9a043335df...'] => false, skipping the remaining AND conditions
- IF User.Email IS ONE OF (hashed) [<2 hashed values>] => false, skipping the remaining AND conditions
Segment evaluation result: User IS NOT IN SEGMENT.
Condition (User IS NOT IN SEGMENT 'Beta Users') evaluates to true.
) => true
AND User IS NOT IN SEGMENT 'Developers'
(
Evaluating segment 'Developers':
- IF User.Email IS ONE OF (hashed) ['242f9fc710...', 'b2f917f062...'] => false, skipping the remaining AND conditions
- IF User.Email IS ONE OF (hashed) [<2 hashed values>] => false, skipping the remaining AND conditions
Segment evaluation result: User IS NOT IN SEGMENT.
Condition (User IS NOT IN SEGMENT 'Developers') evaluates to true.
) => true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ INFO [5000] Evaluating 'featureWithSegmentTargeting' for User '{"Identifier":"12
- IF User IS IN SEGMENT 'Beta users'
(
Evaluating segment 'Beta users':
- IF User.Email IS ONE OF (hashed) ['26fc71b9ce...', 'daaa967a93...'] => true
- IF User.Email IS ONE OF (hashed) [<2 hashed values>] => true
Segment evaluation result: User IS IN SEGMENT.
Condition (User IS IN SEGMENT 'Beta users') evaluates to true.
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ INFO [5000] Evaluating 'featureWithNegatedSegmentTargeting' for User '{"Identifi
- IF User IS NOT IN SEGMENT 'Beta users'
(
Evaluating segment 'Beta users':
- IF User.Email IS ONE OF (hashed) ['26fc71b9ce...', 'daaa967a93...'] => true
- IF User.Email IS ONE OF (hashed) [<2 hashed values>] => true
Segment evaluation result: User IS IN SEGMENT.
Condition (User IS NOT IN SEGMENT 'Beta users') evaluates to false.
)
Expand Down
13 changes: 13 additions & 0 deletions configcatclienttests/test_evaluationlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import os
import unittest
import re

from configcatclient.evaluationlogbuilder import EvaluationLogBuilder

try:
from cStringIO import StringIO # Python 2.7
except ImportError:
Expand Down Expand Up @@ -65,6 +68,16 @@ def test_epoch_date_validation(self):
def test_number_validation(self):
self.assertTrue(self._evaluation_log('data/evaluation/number_validation.json'))

def test_list_truncation(self):
self.assertEqual('[<1 hashed value>]', EvaluationLogBuilder.trunc_comparison_value_if_needed('(hashed)', ['1']))
self.assertEqual('[<4 hashed values>]', EvaluationLogBuilder.trunc_comparison_value_if_needed('(hashed)', ['1', '2', '3', '4']))
self.assertEqual("['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']",
EvaluationLogBuilder.trunc_comparison_value_if_needed('', ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']))
self.assertEqual("['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ... (1 more value)]",
EvaluationLogBuilder.trunc_comparison_value_if_needed('', ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']))
self.assertEqual("['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', ... (2 more values)]",
EvaluationLogBuilder.trunc_comparison_value_if_needed('', ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']))

def _evaluation_log(self, file_path, test_filter=None, generate_expected_log=False):
script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, file_path)
Expand Down

0 comments on commit 0ad97c0

Please sign in to comment.