Skip to content
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

feat: Add evaluation details to finally hook stage #335

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/OpenFeature/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public abstract class Hook
/// <typeparam name="T">Flag value type (bool|number|string|object)</typeparam>
/// <returns>Modified EvaluationContext that is used for the flag evaluation</returns>
public virtual ValueTask<EvaluationContext> BeforeAsync<T>(HookContext<T> context,
IReadOnlyDictionary<string, object>? hints = null, CancellationToken cancellationToken = default)
IReadOnlyDictionary<string, object>? hints = null,
CancellationToken cancellationToken = default)
{
return new ValueTask<EvaluationContext>(EvaluationContext.Empty);
}
Expand All @@ -44,8 +45,10 @@ public virtual ValueTask<EvaluationContext> BeforeAsync<T>(HookContext<T> contex
/// <param name="hints">Caller provided data</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <typeparam name="T">Flag value type (bool|number|string|object)</typeparam>
public virtual ValueTask AfterAsync<T>(HookContext<T> context, FlagEvaluationDetails<T> details,
IReadOnlyDictionary<string, object>? hints = null, CancellationToken cancellationToken = default)
public virtual ValueTask AfterAsync<T>(HookContext<T> context,
FlagEvaluationDetails<T> details,
IReadOnlyDictionary<string, object>? hints = null,
CancellationToken cancellationToken = default)
{
return new ValueTask();
}
Expand All @@ -58,8 +61,10 @@ public virtual ValueTask AfterAsync<T>(HookContext<T> context, FlagEvaluationDet
/// <param name="hints">Caller provided data</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <typeparam name="T">Flag value type (bool|number|string|object)</typeparam>
public virtual ValueTask ErrorAsync<T>(HookContext<T> context, Exception error,
IReadOnlyDictionary<string, object>? hints = null, CancellationToken cancellationToken = default)
public virtual ValueTask ErrorAsync<T>(HookContext<T> context,
Exception error,
IReadOnlyDictionary<string, object>? hints = null,
CancellationToken cancellationToken = default)
{
return new ValueTask();
}
Expand All @@ -68,10 +73,14 @@ public virtual ValueTask ErrorAsync<T>(HookContext<T> context, Exception error,
/// Called unconditionally after flag evaluation.
/// </summary>
/// <param name="context">Provides context of innovation</param>
/// <param name="evaluationDetails">Flag evaluation information</param>
/// <param name="hints">Caller provided data</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <typeparam name="T">Flag value type (bool|number|string|object)</typeparam>
public virtual ValueTask FinallyAsync<T>(HookContext<T> context, IReadOnlyDictionary<string, object>? hints = null, CancellationToken cancellationToken = default)
public virtual ValueTask FinallyAsync<T>(HookContext<T> context,
FlagEvaluationDetails<T> evaluationDetails,
IReadOnlyDictionary<string, object>? hints = null,
CancellationToken cancellationToken = default)
{
return new ValueTask();
}
Expand Down
12 changes: 7 additions & 5 deletions src/OpenFeature/OpenFeatureClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private async Task<FlagEvaluationDetails<T>> EvaluateFlagAsync<T>(
evaluationContextBuilder.Build()
);

FlagEvaluationDetails<T> evaluation;
FlagEvaluationDetails<T>? evaluation = null;
try
{
var contextFromHooks = await this.TriggerBeforeHooksAsync(allHooks, hookContext, options, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -297,7 +297,9 @@ await this.TriggerErrorHooksAsync(allHooksReversed, hookContext, exception, opti
}
finally
{
await this.TriggerFinallyHooksAsync(allHooksReversed, hookContext, options, cancellationToken).ConfigureAwait(false);
evaluation ??= new FlagEvaluationDetails<T>(flagKey, defaultValue, ErrorType.General, Reason.Error, string.Empty,
"Evaluation failed to return a result.");
await this.TriggerFinallyHooksAsync(allHooksReversed, evaluation, hookContext, options, cancellationToken).ConfigureAwait(false);
}

return evaluation;
Expand Down Expand Up @@ -351,14 +353,14 @@ private async Task TriggerErrorHooksAsync<T>(IReadOnlyList<Hook> hooks, HookCont
}
}

private async Task TriggerFinallyHooksAsync<T>(IReadOnlyList<Hook> hooks, HookContext<T> context,
FlagEvaluationOptions? options, CancellationToken cancellationToken = default)
private async Task TriggerFinallyHooksAsync<T>(IReadOnlyList<Hook> hooks, FlagEvaluationDetails<T> evaluation,
HookContext<T> context, FlagEvaluationOptions? options, CancellationToken cancellationToken = default)
{
foreach (var hook in hooks)
{
try
{
await hook.FinallyAsync(context, options?.HookHints, cancellationToken).ConfigureAwait(false);
await hook.FinallyAsync(context, evaluation, options?.HookHints, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down
Loading
Loading