-
Notifications
You must be signed in to change notification settings - Fork 45
feat: add logging hook, rm logging from evaluation #289
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 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
572bb28
draft
liran2000 1467f4c
cont.
liran2000 26aabde
logging
liran2000 c5ae3d5
logging
liran2000 cf001c4
logging
liran2000 b73a438
Merge branch 'main' into issue/284
toddbaert 69e6850
add NewLoggingHook with default logger
liran2000 be2d8bc
cont.
liran2000 c1ebf81
update
liran2000 f973199
remove line
liran2000 3377286
update
liran2000 853a6cc
updates
liran2000 81acdf6
Update README.md
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
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,97 @@ | ||
package hooks | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
of "github.com/open-feature/go-sdk/openfeature" | ||
) | ||
|
||
const ( | ||
DOMAIN_KEY = "domain" | ||
PROVIDER_NAME_KEY = "provider_name" | ||
FLAG_KEY_KEY = "flag_key" | ||
DEFAULT_VALUE_KEY = "default_value" | ||
EVALUATION_CONTEXT_KEY = "evaluation_context" | ||
ERROR_CODE_KEY = "error_code" | ||
ERROR_MESSAGE_KEY = "error_message" | ||
REASON_KEY = "reason" | ||
VARIANT_KEY = "variant" | ||
VALUE_KEY = "value" | ||
) | ||
|
||
type LoggingHook struct { | ||
includeEvaluationContext bool | ||
logger *slog.Logger | ||
} | ||
|
||
func NewLoggingHook(includeEvaluationContext bool) (*LoggingHook, error) { | ||
return NewCustomLoggingHook(slog.Default(), includeEvaluationContext) | ||
} | ||
|
||
func NewCustomLoggingHook(logger *slog.Logger, includeEvaluationContext bool) (*LoggingHook, error) { | ||
toddbaert marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return &LoggingHook{ | ||
logger: logger, | ||
includeEvaluationContext: includeEvaluationContext, | ||
}, nil | ||
} | ||
|
||
type MarshaledEvaluationContext struct { | ||
TargetingKey string | ||
Attributes map[string]interface{} | ||
} | ||
|
||
func (l LoggingHook) buildArgs(hookContext of.HookContext) ([]interface{}, error) { | ||
|
||
args := []interface{}{ | ||
DOMAIN_KEY, hookContext.ClientMetadata().Domain(), | ||
PROVIDER_NAME_KEY, hookContext.ProviderMetadata().Name, | ||
FLAG_KEY_KEY, hookContext.FlagKey(), | ||
DEFAULT_VALUE_KEY, hookContext.DefaultValue(), | ||
} | ||
if l.includeEvaluationContext { | ||
marshaledEvaluationContext := MarshaledEvaluationContext{ | ||
TargetingKey: hookContext.EvaluationContext().TargetingKey(), | ||
Attributes: hookContext.EvaluationContext().Attributes(), | ||
} | ||
args = append(args, EVALUATION_CONTEXT_KEY, marshaledEvaluationContext) | ||
} | ||
|
||
return args, nil | ||
} | ||
|
||
func (h *LoggingHook) Before(ctx context.Context, hookContext of.HookContext, | ||
hint of.HookHints) (*of.EvaluationContext, error) { | ||
var args, err = h.buildArgs(hookContext) | ||
if err != nil { | ||
return nil, err | ||
} | ||
h.logger.Debug("Before stage", args...) | ||
return nil, nil | ||
} | ||
|
||
func (h *LoggingHook) After(ctx context.Context, hookContext of.HookContext, | ||
flagEvaluationDetails of.InterfaceEvaluationDetails, hookHints of.HookHints) error { | ||
var args, err = h.buildArgs(hookContext) | ||
if err != nil { | ||
return err | ||
} | ||
args = append(args, REASON_KEY, flagEvaluationDetails.Reason) | ||
args = append(args, VARIANT_KEY, flagEvaluationDetails.Variant) | ||
args = append(args, VALUE_KEY, flagEvaluationDetails.Value) | ||
h.logger.Debug("After stage", args...) | ||
return nil | ||
} | ||
|
||
func (h *LoggingHook) Error(ctx context.Context, hookContext of.HookContext, err error, hint of.HookHints) { | ||
args, buildArgsErr := h.buildArgs(hookContext) | ||
if buildArgsErr != nil { | ||
slog.Error("Error building args", "error", buildArgsErr) | ||
} | ||
args = append(args, ERROR_CODE_KEY, err) | ||
h.logger.Error("Error stage", args...) | ||
} | ||
|
||
func (h *LoggingHook) Finally(ctx context.Context, hCtx of.HookContext, hint of.HookHints) { | ||
|
||
} |
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.