-
Notifications
You must be signed in to change notification settings - Fork 35
feat: add logging hook #577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danjuv
wants to merge
5
commits into
open-feature:main
Choose a base branch
from
danjuv:feat/logging-hook
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+417
−138
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import typing | ||
| from collections.abc import Mapping, MutableMapping, Sequence | ||
| from datetime import datetime | ||
| from enum import Enum | ||
|
|
||
| from openfeature.evaluation_context import EvaluationContext | ||
| from openfeature.flag_evaluation import FlagEvaluationDetails, FlagType, FlagValueType | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from openfeature.client import ClientMetadata | ||
| from openfeature.provider.metadata import Metadata | ||
|
|
||
| # https://openfeature.dev/specification/sections/hooks/#requirement-461 | ||
| HookData = MutableMapping[str, typing.Any] | ||
|
|
||
|
|
||
| class HookType(Enum): | ||
| BEFORE = "before" | ||
| AFTER = "after" | ||
| FINALLY_AFTER = "finally_after" | ||
| ERROR = "error" | ||
|
|
||
|
|
||
| class HookContext: | ||
| def __init__( # noqa: PLR0913 | ||
| self, | ||
| flag_key: str, | ||
| flag_type: FlagType, | ||
| default_value: FlagValueType, | ||
| evaluation_context: EvaluationContext, | ||
| client_metadata: ClientMetadata | None = None, | ||
| provider_metadata: Metadata | None = None, | ||
| hook_data: HookData | None = None, | ||
| ): | ||
| self.flag_key = flag_key | ||
| self.flag_type = flag_type | ||
| self.default_value = default_value | ||
| self.evaluation_context = evaluation_context | ||
| self.client_metadata = client_metadata | ||
| self.provider_metadata = provider_metadata | ||
| self.hook_data = hook_data or {} | ||
|
|
||
| def __setattr__(self, key: str, value: typing.Any) -> None: | ||
| if hasattr(self, key) and key in ( | ||
| "flag_key", | ||
| "flag_type", | ||
| "default_value", | ||
| "client_metadata", | ||
| "provider_metadata", | ||
| ): | ||
| raise AttributeError(f"Attribute {key!r} is immutable") | ||
| super().__setattr__(key, value) | ||
|
|
||
|
|
||
| # https://openfeature.dev/specification/sections/hooks/#requirement-421 | ||
| HookHintValue: typing.TypeAlias = ( | ||
| bool | ||
| | int | ||
| | float | ||
| | str | ||
| | datetime | ||
| | Sequence["HookHintValue"] | ||
| | Mapping[str, "HookHintValue"] | ||
| ) | ||
|
|
||
| HookHints = Mapping[str, HookHintValue] | ||
|
|
||
|
|
||
| class Hook: | ||
| def before( | ||
| self, hook_context: HookContext, hints: HookHints | ||
| ) -> EvaluationContext | None: | ||
| """ | ||
| Runs before flag is resolved. | ||
|
|
||
| :param hook_context: Information about the particular flag evaluation | ||
| :param hints: An immutable mapping of data for users to | ||
| communicate to the hooks. | ||
| :return: An EvaluationContext. It will be merged with the | ||
| EvaluationContext instances from other hooks, the client and API. | ||
| """ | ||
| return None | ||
|
|
||
| def after( | ||
| self, | ||
| hook_context: HookContext, | ||
| details: FlagEvaluationDetails[FlagValueType], | ||
| hints: HookHints, | ||
| ) -> None: | ||
| """ | ||
| Runs after a flag is resolved. | ||
|
|
||
| :param hook_context: Information about the particular flag evaluation | ||
| :param details: Information about how the flag was resolved, | ||
| including any resolved values. | ||
| :param hints: A mapping of data for users to communicate to the hooks. | ||
| """ | ||
| pass | ||
|
|
||
| def error( | ||
| self, hook_context: HookContext, exception: Exception, hints: HookHints | ||
| ) -> None: | ||
| """ | ||
| Run when evaluation encounters an error. Errors thrown will be swallowed. | ||
|
|
||
| :param hook_context: Information about the particular flag evaluation | ||
| :param exception: The exception that was thrown | ||
| :param hints: A mapping of data for users to communicate to the hooks. | ||
| """ | ||
| pass | ||
|
|
||
| def finally_after( | ||
| self, | ||
| hook_context: HookContext, | ||
| details: FlagEvaluationDetails[FlagValueType], | ||
| hints: HookHints, | ||
| ) -> None: | ||
| """ | ||
| Run after flag evaluation, including any error processing. | ||
| This will always run. Errors will be swallowed. | ||
|
|
||
| :param hook_context: Information about the particular flag evaluation | ||
| :param hints: A mapping of data for users to communicate to the hooks. | ||
| """ | ||
| pass | ||
|
|
||
| def supports_flag_value_type(self, flag_type: FlagType) -> bool: | ||
| """ | ||
| Check to see if the hook supports the particular flag type. | ||
|
|
||
| :param flag_type: particular type of the flag | ||
| :return: a boolean containing whether the flag type is supported (True) | ||
| or not (False) | ||
| """ | ||
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import json | ||
| import logging | ||
|
|
||
| from openfeature.evaluation_context import EvaluationContext | ||
| from openfeature.exception import ErrorCode, OpenFeatureError | ||
| from openfeature.flag_evaluation import FlagEvaluationDetails, FlagValueType | ||
| from openfeature.hook.hook import Hook, HookContext, HookHints | ||
|
|
||
|
|
||
| class LoggingHook(Hook): | ||
| def __init__( | ||
| self, | ||
| include_evaluation_context: bool = False, | ||
| logger: logging.Logger | None = None, | ||
| ): | ||
| self.logger = logger or logging.getLogger("openfeature") | ||
| self.include_evaluation_context = include_evaluation_context | ||
|
|
||
| def _build_args(self, hook_context: HookContext) -> dict: | ||
| args = { | ||
| "domain": hook_context.client_metadata.domain | ||
| if hook_context.client_metadata | ||
| else None, | ||
| "provider_name": hook_context.provider_metadata.name | ||
| if hook_context.provider_metadata | ||
| else None, | ||
| "flag_key": hook_context.flag_key, | ||
| "default_value": hook_context.default_value, | ||
| } | ||
| if self.include_evaluation_context: | ||
| args["evaluation_context"] = json.dumps( | ||
| { | ||
| "targeting_key": hook_context.evaluation_context.targeting_key, | ||
| "attributes": hook_context.evaluation_context.attributes, | ||
| }, | ||
| default=str, | ||
| ) | ||
| return args | ||
|
|
||
| def before( | ||
| self, hook_context: HookContext, hints: HookHints | ||
| ) -> EvaluationContext | None: | ||
| args = self._build_args(hook_context) | ||
| args["stage"] = "before" | ||
| self.logger.debug("Before stage %s", args) | ||
| return None | ||
|
|
||
| def after( | ||
| self, | ||
| hook_context: HookContext, | ||
| details: FlagEvaluationDetails[FlagValueType], | ||
| hints: HookHints, | ||
| ) -> None: | ||
| args = self._build_args(hook_context) | ||
| args["stage"] = "after" | ||
| args["reason"] = details.reason | ||
| args["variant"] = details.variant | ||
| args["value"] = details.value | ||
| self.logger.debug("After stage %s", args) | ||
|
|
||
| def error( | ||
| self, hook_context: HookContext, exception: Exception, hints: HookHints | ||
| ) -> None: | ||
| args = self._build_args(hook_context) | ||
| args["stage"] = "error" | ||
| args["error_code"] = ( | ||
| exception.error_code | ||
| if isinstance(exception, OpenFeatureError) | ||
| else ErrorCode.GENERAL | ||
| ) | ||
| args["error_message"] = str(exception) | ||
| self.logger.error("Error stage %s", args) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.