Skip to content

Commit

Permalink
Add evaluation details to finally hook stage open-feature#403
Browse files Browse the repository at this point in the history
Signed-off-by: christian.lutnik <christian.lutnik@dynatrace.com>
  • Loading branch information
chrfwow committed Jan 13, 2025
1 parent cbace6a commit f1676e7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
9 changes: 6 additions & 3 deletions openfeature/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ def evaluate_flag_details( # noqa: PLR0915
error_code=ErrorCode.PROVIDER_FATAL,
)

flag_evaluation = None
try:
# https://github.com/open-feature/spec/blob/main/specification/sections/03-evaluation-context.md
# Any resulting evaluation context from a before hook will overwrite
Expand Down Expand Up @@ -364,13 +365,14 @@ def evaluate_flag_details( # noqa: PLR0915
except OpenFeatureError as err:
error_hooks(flag_type, hook_context, err, reversed_merged_hooks, hook_hints)

return FlagEvaluationDetails(
flag_evaluation = FlagEvaluationDetails(
flag_key=flag_key,
value=default_value,
reason=Reason.ERROR,
error_code=err.error_code,
error_message=err.error_message,
)
return flag_evaluation
# Catch any type of exception here since the user can provide any exception
# in the error hooks
except Exception as err: # pragma: no cover
Expand All @@ -381,16 +383,17 @@ def evaluate_flag_details( # noqa: PLR0915
error_hooks(flag_type, hook_context, err, reversed_merged_hooks, hook_hints)

error_message = getattr(err, "error_message", str(err))
return FlagEvaluationDetails(
flag_evaluation = FlagEvaluationDetails(
flag_key=flag_key,
value=default_value,
reason=Reason.ERROR,
error_code=ErrorCode.GENERAL,
error_message=error_message,
)
return flag_evaluation

finally:
after_all_hooks(flag_type, hook_context, reversed_merged_hooks, hook_hints)
after_all_hooks(flag_type, hook_context, flag_evaluation, reversed_merged_hooks, hook_hints)

def _create_provider_evaluation(
self,
Expand Down
2 changes: 1 addition & 1 deletion openfeature/hook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def error(
"""
pass

def finally_after(self, hook_context: HookContext, hints: HookHints) -> None:
def finally_after(self, hook_context: HookContext, details: FlagEvaluationDetails[typing.Any], hints: HookHints) -> None:
"""
Run after flag evaluation, including any error processing.
This will always run. Errors will be swallowed.
Expand Down
3 changes: 2 additions & 1 deletion openfeature/hook/_hook_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def error_hooks(
def after_all_hooks(
flag_type: FlagType,
hook_context: HookContext,
details: FlagEvaluationDetails[typing.Any],
hooks: typing.List[Hook],
hints: typing.Optional[HookHints] = None,
) -> None:
kwargs = {"hook_context": hook_context, "hints": hints}
kwargs = {"hook_context": hook_context, "details": details, "hints": hints}
_execute_hooks(
flag_type=flag_type, hooks=hooks, hook_method=HookType.FINALLY_AFTER, **kwargs
)
Expand Down
7 changes: 5 additions & 2 deletions tests/hook/test_hook_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,15 @@ def test_after_hooks_run_after_method(mock_hook):
def test_finally_after_hooks_run_finally_after_method(mock_hook):
# Given
hook_context = HookContext("flag_key", FlagType.BOOLEAN, True, "")
flag_evaluation_details = FlagEvaluationDetails(
hook_context.flag_key, "val", "unknown"
)
hook_hints = MappingProxyType({})
# When
after_all_hooks(FlagType.BOOLEAN, hook_context, [mock_hook], hook_hints)
after_all_hooks(FlagType.BOOLEAN, hook_context, flag_evaluation_details, [mock_hook], hook_hints)
# Then
mock_hook.supports_flag_value_type.assert_called_once()
mock_hook.finally_after.assert_called_once()
mock_hook.finally_after.assert_called_with(
hook_context=hook_context, hints=hook_hints
hook_context=hook_context, details=flag_evaluation_details, hints=hook_hints
)

0 comments on commit f1676e7

Please sign in to comment.