-
Notifications
You must be signed in to change notification settings - Fork 20
feat: Implement Default Logging Hook #308
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
beeme1mr
merged 12 commits into
open-feature:main
from
kylejuliandev:feat/add-logging-hook
Jan 23, 2025
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cbe4552
Add initial logging hook
kylejuliandev 5b9c2e4
Refactor to improve formating of Context
kylejuliandev 8b3bbd2
Address issue on older FTMs not resolving non nullable after check wi…
kylejuliandev 6d72e66
Add XML comments to the LoggingHook
kylejuliandev ea2b393
Add unit tests to cover new LoggingHook
kylejuliandev 25a4781
Add documentation on the new LoggingHook
kylejuliandev c8b563e
Fix copy-paste error with creating LoggingHookContent
kylejuliandev 0b46216
Improve unit test coverage of LoggingHook
kylejuliandev 72a9f39
Address PR comments
kylejuliandev 3f05e27
Switch to blocked namespace in new test class
kylejuliandev f98a521
Merge branch 'main' into feat/add-logging-hook
askpt 9ecb92d
Merge branch 'main' into feat/add-logging-hook
beeme1mr 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,182 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using OpenFeature.Model; | ||
|
||
namespace OpenFeature.Hooks | ||
{ | ||
/// <summary> | ||
/// The logging hook is a hook which logs messages during the flag evaluation life-cycle. | ||
/// </summary> | ||
public sealed partial class LoggingHook : Hook | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly bool _includeContext; | ||
|
||
/// <summary> | ||
/// Initialise a <see cref="LoggingHook"/> with a <paramref name="logger"/> and optional Evaluation Context. <paramref name="includeContext"/> will | ||
/// include properties in the <see cref="HookContext{T}.EvaluationContext"/> to the generated logs. | ||
/// </summary> | ||
public LoggingHook(ILogger logger, bool includeContext = false) | ||
{ | ||
if (logger == null) throw new ArgumentNullException(nameof(logger)); | ||
|
||
this._logger = logger; | ||
this._includeContext = includeContext; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override ValueTask<EvaluationContext> BeforeAsync<T>(HookContext<T> context, IReadOnlyDictionary<string, object>? hints = null, CancellationToken cancellationToken = default) | ||
{ | ||
var evaluationContext = this._includeContext ? context.EvaluationContext : null; | ||
|
||
var content = new LoggingHookContent( | ||
context.ClientMetadata.Name, | ||
context.ProviderMetadata.Name, | ||
context.FlagKey, | ||
context.DefaultValue?.ToString(), | ||
evaluationContext); | ||
|
||
this.HookBeforeStageExecuted(content); | ||
|
||
return base.BeforeAsync(context, hints, cancellationToken); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override ValueTask ErrorAsync<T>(HookContext<T> context, Exception error, IReadOnlyDictionary<string, object>? hints = null, CancellationToken cancellationToken = default) | ||
{ | ||
var evaluationContext = this._includeContext ? context.EvaluationContext : null; | ||
|
||
var content = new LoggingHookContent( | ||
context.ClientMetadata.Name, | ||
context.ProviderMetadata.Name, | ||
context.FlagKey, | ||
context.DefaultValue?.ToString(), | ||
evaluationContext); | ||
|
||
this.HookErrorStageExecuted(content); | ||
|
||
return base.ErrorAsync(context, error, hints, cancellationToken); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override ValueTask AfterAsync<T>(HookContext<T> context, FlagEvaluationDetails<T> details, IReadOnlyDictionary<string, object>? hints = null, CancellationToken cancellationToken = default) | ||
{ | ||
var evaluationContext = this._includeContext ? context.EvaluationContext : null; | ||
|
||
var content = new LoggingHookContent( | ||
context.ClientMetadata.Name, | ||
context.ProviderMetadata.Name, | ||
context.FlagKey, | ||
context.DefaultValue?.ToString(), | ||
evaluationContext); | ||
|
||
this.HookAfterStageExecuted(content); | ||
|
||
return base.AfterAsync(context, details, hints, cancellationToken); | ||
} | ||
|
||
[LoggerMessage( | ||
Level = LogLevel.Debug, | ||
Message = "Before Flag Evaluation {Content}")] | ||
partial void HookBeforeStageExecuted(LoggingHookContent content); | ||
|
||
[LoggerMessage( | ||
Level = LogLevel.Error, | ||
Message = "Error during Flag Evaluation {Content}")] | ||
partial void HookErrorStageExecuted(LoggingHookContent content); | ||
|
||
[LoggerMessage( | ||
Level = LogLevel.Debug, | ||
Message = "After Flag Evaluation {Content}")] | ||
partial void HookAfterStageExecuted(LoggingHookContent content); | ||
|
||
/// <summary> | ||
/// Generates a log string with contents provided by the <see cref="LoggingHook"/>. | ||
/// <para> | ||
/// Specification for log contents found at https://github.com/open-feature/spec/blob/d261f68331b94fd8ed10bc72bc0485cfc72a51a8/specification/appendix-a-included-utilities.md#logging-hook | ||
/// </para> | ||
/// </summary> | ||
internal class LoggingHookContent | ||
{ | ||
private readonly string _domain; | ||
private readonly string _providerName; | ||
private readonly string _flagKey; | ||
private readonly string _defaultValue; | ||
private readonly EvaluationContext? _evaluationContext; | ||
|
||
public LoggingHookContent(string? domain, string? providerName, string flagKey, string? defaultValue, EvaluationContext? evaluationContext = null) | ||
{ | ||
this._domain = string.IsNullOrEmpty(domain) ? "missing" : domain!; | ||
this._providerName = string.IsNullOrEmpty(providerName) ? "missing" : providerName!; | ||
this._flagKey = flagKey; | ||
this._defaultValue = string.IsNullOrEmpty(defaultValue) ? "missing" : defaultValue!; | ||
this._evaluationContext = evaluationContext; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
|
||
stringBuilder.Append("Domain:"); | ||
stringBuilder.Append(this._domain); | ||
stringBuilder.Append(Environment.NewLine); | ||
askpt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
stringBuilder.Append("ProviderName:"); | ||
stringBuilder.Append(this._providerName); | ||
stringBuilder.Append(Environment.NewLine); | ||
|
||
stringBuilder.Append("FlagKey:"); | ||
stringBuilder.Append(this._flagKey); | ||
stringBuilder.Append(Environment.NewLine); | ||
|
||
stringBuilder.Append("DefaultValue:"); | ||
stringBuilder.Append(this._defaultValue); | ||
stringBuilder.Append(Environment.NewLine); | ||
|
||
if (this._evaluationContext != null) | ||
{ | ||
stringBuilder.Append("Context:"); | ||
stringBuilder.Append(Environment.NewLine); | ||
foreach (var kvp in this._evaluationContext.AsDictionary()) | ||
{ | ||
stringBuilder.Append('\t'); | ||
stringBuilder.Append(kvp.Key); | ||
stringBuilder.Append(':'); | ||
stringBuilder.Append(GetValueString(kvp.Value)); | ||
stringBuilder.Append(Environment.NewLine); | ||
} | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
|
||
static string? GetValueString(Value value) | ||
{ | ||
if (value.IsNull) | ||
return string.Empty; | ||
|
||
if (value.IsString) | ||
return value.AsString; | ||
|
||
if (value.IsBoolean) | ||
return value.AsBoolean.ToString(); | ||
|
||
if (value.IsNumber) | ||
{ | ||
// Value.AsDouble will attempt to cast other numbers to double | ||
// There is an implicit conversation for int/long to double | ||
if (value.AsDouble != null) return value.AsDouble.ToString(); | ||
} | ||
|
||
if (value.IsDateTime) | ||
return value.AsDateTime?.ToString("O"); | ||
|
||
return value.ToString(); | ||
} | ||
} | ||
} | ||
} |
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.