- 
                Notifications
    You must be signed in to change notification settings 
- Fork 50
feat: add logging hook, rm logging from evaluation #1084
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      fdb14a8
              
                feat: add logging hook, rm logging from evaluation
              
              
                toddbaert 569b838
              
                fixup: remove public
              
              
                toddbaert c18c2a9
              
                fixup: debug, not info
              
              
                toddbaert 83afa58
              
                fixup: move comment
              
              
                toddbaert b1ddafc
              
                fixup: underscoes
              
              
                toddbaert 3c358b7
              
                fixup: add to readme
              
              
                toddbaert 8e17466
              
                Merge branch 'main' into feat/logging-compliance
              
              
                toddbaert 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -17,19 +17,28 @@ | |
| @SuppressWarnings({ "unchecked", "rawtypes" }) | ||
| class HookSupport { | ||
|  | ||
| public void errorHooks(FlagValueType flagValueType, HookContext hookCtx, Exception e, List<Hook> hooks, | ||
| public EvaluationContext beforeHooks(FlagValueType flagValueType, HookContext hookCtx, List<Hook> hooks, | ||
| Map<String, Object> hints) { | ||
| executeHooks(flagValueType, hooks, "error", hook -> hook.error(hookCtx, e, hints)); | ||
| Stream<EvaluationContext> result = callBeforeHooks(flagValueType, hookCtx, hooks, hints); | ||
| return hookCtx.getCtx().merge( | ||
| result.reduce(hookCtx.getCtx(), (EvaluationContext accumulated, EvaluationContext current) -> { | ||
| return accumulated.merge(current); | ||
| })); | ||
| } | ||
|  | ||
| public void afterHooks(FlagValueType flagValueType, HookContext hookContext, FlagEvaluationDetails details, | ||
| List<Hook> hooks, Map<String, Object> hints) { | ||
| executeHooksUnchecked(flagValueType, hooks, hook -> hook.after(hookContext, details, hints)); | ||
| } | ||
|  | ||
| public void afterAllHooks(FlagValueType flagValueType, HookContext hookCtx, List<Hook> hooks, | ||
| Map<String, Object> hints) { | ||
| executeHooks(flagValueType, hooks, "finally", hook -> hook.finallyAfter(hookCtx, hints)); | ||
| } | ||
|  | ||
| public void afterHooks(FlagValueType flagValueType, HookContext hookContext, FlagEvaluationDetails details, | ||
| List<Hook> hooks, Map<String, Object> hints) { | ||
| executeHooksUnchecked(flagValueType, hooks, hook -> hook.after(hookContext, details, hints)); | ||
| public void errorHooks(FlagValueType flagValueType, HookContext hookCtx, Exception e, List<Hook> hooks, | ||
| Map<String, Object> hints) { | ||
| executeHooks(flagValueType, hooks, "error", hook -> hook.error(hookCtx, e, hints)); | ||
| } | ||
|  | ||
| private <T> void executeHooks( | ||
|  | @@ -44,6 +53,17 @@ private <T> void executeHooks( | |
| } | ||
| } | ||
|  | ||
| // before, error, and finally hooks shouldn't throw | ||
| private <T> void executeChecked(Hook<T> hook, Consumer<Hook<T>> hookCode, String hookMethod) { | ||
| try { | ||
| hookCode.accept(hook); | ||
| } catch (Exception exception) { | ||
| log.error("Unhandled exception when running {} hook {} (only 'after' hooks should throw)", hookMethod, | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ONLY runs for hooks which we expect not to throw (before/finally/error). | ||
| hook.getClass(), exception); | ||
| } | ||
| } | ||
|  | ||
| // after hooks can throw in order to do validation | ||
| private <T> void executeHooksUnchecked( | ||
| FlagValueType flagValueType, List<Hook> hooks, | ||
| Consumer<Hook<T>> hookCode) { | ||
|  | @@ -55,23 +75,6 @@ private <T> void executeHooksUnchecked( | |
| } | ||
| } | ||
|  | ||
| private <T> void executeChecked(Hook<T> hook, Consumer<Hook<T>> hookCode, String hookMethod) { | ||
| try { | ||
| hookCode.accept(hook); | ||
| } catch (Exception exception) { | ||
| log.error("Exception when running {} hooks {}", hookMethod, hook.getClass(), exception); | ||
| } | ||
| } | ||
|  | ||
| public EvaluationContext beforeHooks(FlagValueType flagValueType, HookContext hookCtx, List<Hook> hooks, | ||
| Map<String, Object> hints) { | ||
| Stream<EvaluationContext> result = callBeforeHooks(flagValueType, hookCtx, hooks, hints); | ||
| return hookCtx.getCtx().merge( | ||
| result.reduce(hookCtx.getCtx(), (EvaluationContext accumulated, EvaluationContext current) -> { | ||
| return accumulated.merge(current); | ||
| })); | ||
| } | ||
|  | ||
| private Stream<EvaluationContext> callBeforeHooks(FlagValueType flagValueType, HookContext hookCtx, | ||
| List<Hook> hooks, Map<String, Object> hints) { | ||
| // These traverse backwards from normal. | ||
|  | ||
  
    
      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
    
  
  
    
              
        
          
  
    
      
          
            92 changes: 92 additions & 0 deletions
          
          92 
        
  src/main/java/dev/openfeature/sdk/hooks/logging/LoggingHook.java
  
  
      
      
   
        
      
      
    
  
    
      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,92 @@ | ||
| package dev.openfeature.sdk.hooks.logging; | ||
|  | ||
| import dev.openfeature.sdk.ErrorCode; | ||
| import dev.openfeature.sdk.EvaluationContext; | ||
| import dev.openfeature.sdk.FlagEvaluationDetails; | ||
| import dev.openfeature.sdk.Hook; | ||
| import dev.openfeature.sdk.HookContext; | ||
| import dev.openfeature.sdk.exceptions.OpenFeatureError; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.slf4j.spi.LoggingEventBuilder; | ||
|  | ||
| /** | ||
| * A hook for logging flag evaluations. | ||
| * Useful for debugging. | ||
| * Flag evaluation data is logged at debug and error in before/after stages and error stages, respectively. | ||
| */ | ||
| @Slf4j | ||
| @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED", | ||
| justification = "we can ignore return values of chainables (builders) here") | ||
| public class LoggingHook implements Hook<Object> { | ||
|         
                  aepfli marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| static final String DOMAIN_KEY = "domain"; | ||
| static final String PROVIDER_NAME_KEY = "provider_name"; | ||
| static final String FLAG_KEY_KEY = "flag_key"; | ||
| static final String DEFAULT_VALUE_KEY = "default_value"; | ||
| static final String EVALUATION_CONTEXT_KEY = "evaluation_context"; | ||
| static final String ERROR_CODE_KEY = "error_code"; | ||
| static final String ERROR_MESSAGE_KEY = "error_message"; | ||
| static final String REASON_KEY = "reason"; | ||
| static final String VARIANT_KEY = "variant"; | ||
| static final String VALUE_KEY = "value"; | ||
|         
                  toddbaert marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| private boolean includeEvaluationContext; | ||
|  | ||
| /** | ||
| * Construct a new LoggingHook. | ||
| */ | ||
| public LoggingHook() { | ||
| this(false); | ||
| } | ||
|  | ||
| /** | ||
| * Construct a new LoggingHook. | ||
| * @param includeEvaluationContext include a serialized evaluation context in the log message (defaults to false) | ||
| */ | ||
| public LoggingHook(boolean includeEvaluationContext) { | ||
| this.includeEvaluationContext = includeEvaluationContext; | ||
| } | ||
|  | ||
| @Override | ||
| public Optional<EvaluationContext> before(HookContext<Object> hookContext, Map<String, Object> hints) { | ||
|         
                  Kavindu-Dodan marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| LoggingEventBuilder builder = log.atDebug(); | ||
| addCommonProps(builder, hookContext); | ||
| builder.log("Before stage"); | ||
|  | ||
| return Optional.empty(); | ||
| } | ||
|  | ||
| @Override | ||
| public void after(HookContext<Object> hookContext, FlagEvaluationDetails<Object> details, | ||
| Map<String, Object> hints) { | ||
| LoggingEventBuilder builder = log.atDebug() | ||
| .addKeyValue(REASON_KEY, details.getReason()) | ||
| .addKeyValue(VARIANT_KEY, details.getVariant()) | ||
| .addKeyValue(VALUE_KEY, details.getValue()); | ||
| addCommonProps(builder, hookContext); | ||
| builder.log("After stage"); | ||
| } | ||
|  | ||
| @Override | ||
| public void error(HookContext<Object> hookContext, Exception error, Map<String, Object> hints) { | ||
| LoggingEventBuilder builder = log.atError() | ||
| .addKeyValue(ERROR_MESSAGE_KEY, error.getMessage()); | ||
| addCommonProps(builder, hookContext); | ||
| ErrorCode errorCode = error instanceof OpenFeatureError ? ((OpenFeatureError) error).getErrorCode() : null; | ||
| builder.addKeyValue(ERROR_CODE_KEY, errorCode); | ||
| builder.log("Error stage", error); | ||
| } | ||
|  | ||
| private void addCommonProps(LoggingEventBuilder builder, HookContext<Object> hookContext) { | ||
| builder.addKeyValue(DOMAIN_KEY, hookContext.getClientMetadata().getDomain()) | ||
| .addKeyValue(PROVIDER_NAME_KEY, hookContext.getProviderMetadata().getName()) | ||
| .addKeyValue(FLAG_KEY_KEY, hookContext.getFlagKey()) | ||
| .addKeyValue(DEFAULT_VALUE_KEY, hookContext.getDefaultValue()); | ||
|  | ||
| if (includeEvaluationContext) { | ||
| builder.addKeyValue(EVALUATION_CONTEXT_KEY, hookContext.getCtx()); | ||
| } | ||
| } | ||
| } | ||
  
    
      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
    
  
  
    
              
      
      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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No functional changes in this class, I just re-ordered some methods to make the class read more sensibly, and added a bit more information to the log statement.