forked from open-feature/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenFeatureClient.cs
385 lines (335 loc) · 18.4 KB
/
OpenFeatureClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using OpenFeature.Constant;
using OpenFeature.Error;
using OpenFeature.Extension;
using OpenFeature.Model;
namespace OpenFeature
{
/// <summary>
///
/// </summary>
public sealed partial class FeatureClient : IFeatureClient
{
private readonly ClientMetadata _metadata;
private readonly ConcurrentStack<Hook> _hooks = new ConcurrentStack<Hook>();
private readonly ILogger _logger;
private readonly Func<FeatureProvider> _providerAccessor;
private EvaluationContext _evaluationContext;
private readonly object _evaluationContextLock = new object();
/// <summary>
/// Get a provider and an associated typed flag resolution method.
/// <para>
/// The global provider could change between two accesses, so in order to safely get provider information we
/// must first alias it and then use that alias to access everything we need.
/// </para>
/// </summary>
/// <param name="method">
/// This method should return the desired flag resolution method from the given provider reference.
/// </param>
/// <typeparam name="T">The type of the resolution method</typeparam>
/// <returns>A tuple containing a resolution method and the provider it came from.</returns>
private (Func<string, T, EvaluationContext, CancellationToken, Task<ResolutionDetails<T>>>, FeatureProvider)
ExtractProvider<T>(
Func<FeatureProvider, Func<string, T, EvaluationContext, CancellationToken, Task<ResolutionDetails<T>>>> method)
{
// Alias the provider reference so getting the method and returning the provider are
// guaranteed to be the same object.
var provider = Api.Instance.GetProvider(this._metadata.Name!);
return (method(provider), provider);
}
/// <inheritdoc />
public ProviderStatus ProviderStatus => this._providerAccessor.Invoke().Status;
/// <inheritdoc />
public EvaluationContext GetContext()
{
lock (this._evaluationContextLock)
{
return this._evaluationContext;
}
}
/// <inheritdoc />
public void SetContext(EvaluationContext? context)
{
lock (this._evaluationContextLock)
{
this._evaluationContext = context ?? EvaluationContext.Empty;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="FeatureClient"/> class.
/// </summary>
/// <param name="providerAccessor">Function to retrieve current provider</param>
/// <param name="name">Name of client <see cref="ClientMetadata"/></param>
/// <param name="version">Version of client <see cref="ClientMetadata"/></param>
/// <param name="logger">Logger used by client</param>
/// <param name="context">Context given to this client</param>
/// <exception cref="ArgumentNullException">Throws if any of the required parameters are null</exception>
internal FeatureClient(Func<FeatureProvider> providerAccessor, string? name, string? version, ILogger? logger = null, EvaluationContext? context = null)
{
this._metadata = new ClientMetadata(name, version);
this._logger = logger ?? NullLogger<FeatureClient>.Instance;
this._evaluationContext = context ?? EvaluationContext.Empty;
this._providerAccessor = providerAccessor;
}
/// <inheritdoc />
public ClientMetadata GetMetadata() => this._metadata;
/// <summary>
/// Add hook to client
/// <para>
/// Hooks which are dependent on each other should be provided in a collection
/// using the <see cref="AddHooks(IEnumerable{Hook})"/>.
/// </para>
/// </summary>
/// <param name="hook">Hook that implements the <see cref="Hook"/> interface</param>
public void AddHooks(Hook hook) => this._hooks.Push(hook);
/// <inheritdoc />
public void AddHandler(ProviderEventTypes eventType, EventHandlerDelegate handler)
{
Api.Instance.AddClientHandler(this._metadata.Name!, eventType, handler);
}
/// <inheritdoc />
public void RemoveHandler(ProviderEventTypes type, EventHandlerDelegate handler)
{
Api.Instance.RemoveClientHandler(this._metadata.Name!, type, handler);
}
/// <inheritdoc />
public void AddHooks(IEnumerable<Hook> hooks)
#if NET7_0_OR_GREATER
=> this._hooks.PushRange(hooks as Hook[] ?? hooks.ToArray());
#else
{
// See: https://github.com/dotnet/runtime/issues/62121
if (hooks is Hook[] array)
{
if (array.Length > 0)
this._hooks.PushRange(array);
return;
}
array = hooks.ToArray();
if (array.Length > 0)
this._hooks.PushRange(array);
}
#endif
/// <inheritdoc />
public IEnumerable<Hook> GetHooks() => this._hooks.Reverse();
/// <summary>
/// Removes all hooks from the client
/// </summary>
public void ClearHooks() => this._hooks.Clear();
/// <inheritdoc />
public async Task<bool> GetBooleanValueAsync(string flagKey, bool defaultValue, EvaluationContext? context = null,
FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
(await this.GetBooleanDetailsAsync(flagKey, defaultValue, context, config, cancellationToken).ConfigureAwait(false)).Value;
/// <inheritdoc />
public async Task<FlagEvaluationDetails<bool>> GetBooleanDetailsAsync(string flagKey, bool defaultValue,
EvaluationContext? context = null, FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
await this.EvaluateFlagAsync(this.ExtractProvider<bool>(provider => provider.ResolveBooleanValueAsync),
FlagValueType.Boolean, flagKey,
defaultValue, context, config, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public async Task<string> GetStringValueAsync(string flagKey, string defaultValue, EvaluationContext? context = null,
FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
(await this.GetStringDetailsAsync(flagKey, defaultValue, context, config, cancellationToken).ConfigureAwait(false)).Value;
/// <inheritdoc />
public async Task<FlagEvaluationDetails<string>> GetStringDetailsAsync(string flagKey, string defaultValue,
EvaluationContext? context = null, FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
await this.EvaluateFlagAsync(this.ExtractProvider<string>(provider => provider.ResolveStringValueAsync),
FlagValueType.String, flagKey,
defaultValue, context, config, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public async Task<int> GetIntegerValueAsync(string flagKey, int defaultValue, EvaluationContext? context = null,
FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
(await this.GetIntegerDetailsAsync(flagKey, defaultValue, context, config, cancellationToken).ConfigureAwait(false)).Value;
/// <inheritdoc />
public async Task<FlagEvaluationDetails<int>> GetIntegerDetailsAsync(string flagKey, int defaultValue,
EvaluationContext? context = null, FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
await this.EvaluateFlagAsync(this.ExtractProvider<int>(provider => provider.ResolveIntegerValueAsync),
FlagValueType.Number, flagKey,
defaultValue, context, config, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public async Task<double> GetDoubleValueAsync(string flagKey, double defaultValue,
EvaluationContext? context = null,
FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
(await this.GetDoubleDetailsAsync(flagKey, defaultValue, context, config, cancellationToken).ConfigureAwait(false)).Value;
/// <inheritdoc />
public async Task<FlagEvaluationDetails<double>> GetDoubleDetailsAsync(string flagKey, double defaultValue,
EvaluationContext? context = null, FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
await this.EvaluateFlagAsync(this.ExtractProvider<double>(provider => provider.ResolveDoubleValueAsync),
FlagValueType.Number, flagKey,
defaultValue, context, config, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public async Task<Value> GetObjectValueAsync(string flagKey, Value defaultValue, EvaluationContext? context = null,
FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
(await this.GetObjectDetailsAsync(flagKey, defaultValue, context, config, cancellationToken).ConfigureAwait(false)).Value;
/// <inheritdoc />
public async Task<FlagEvaluationDetails<Value>> GetObjectDetailsAsync(string flagKey, Value defaultValue,
EvaluationContext? context = null, FlagEvaluationOptions? config = null, CancellationToken cancellationToken = default) =>
await this.EvaluateFlagAsync(this.ExtractProvider<Value>(provider => provider.ResolveStructureValueAsync),
FlagValueType.Object, flagKey,
defaultValue, context, config, cancellationToken).ConfigureAwait(false);
private async Task<FlagEvaluationDetails<T>> EvaluateFlagAsync<T>(
(Func<string, T, EvaluationContext, CancellationToken, Task<ResolutionDetails<T>>>, FeatureProvider) providerInfo,
FlagValueType flagValueType, string flagKey, T defaultValue, EvaluationContext? context = null,
FlagEvaluationOptions? options = null,
CancellationToken cancellationToken = default)
{
var resolveValueDelegate = providerInfo.Item1;
var provider = providerInfo.Item2;
// New up an evaluation context if one was not provided.
context ??= EvaluationContext.Empty;
// merge api, client, and invocation context.
var evaluationContext = Api.Instance.GetContext();
var evaluationContextBuilder = EvaluationContext.Builder();
evaluationContextBuilder.Merge(evaluationContext);
evaluationContextBuilder.Merge(this.GetContext());
evaluationContextBuilder.Merge(context);
var allHooks = new List<Hook>()
.Concat(Api.Instance.GetHooks())
.Concat(this.GetHooks())
.Concat(options?.Hooks ?? Enumerable.Empty<Hook>())
.Concat(provider.GetProviderHooks())
.ToList()
.AsReadOnly();
var allHooksReversed = allHooks
.AsEnumerable()
.Reverse()
.ToList()
.AsReadOnly();
var hookContext = new HookContext<T>(
flagKey,
defaultValue,
flagValueType, this._metadata,
provider.GetMetadata(),
evaluationContextBuilder.Build()
);
FlagEvaluationDetails<T> evaluation;
try
{
var contextFromHooks = await this.TriggerBeforeHooksAsync(allHooks, hookContext, options, cancellationToken).ConfigureAwait(false);
// short circuit evaluation entirely if provider is in a bad state
if (provider.Status == ProviderStatus.NotReady)
{
throw new ProviderNotReadyException("Provider has not yet completed initialization.");
}
else if (provider.Status == ProviderStatus.Fatal)
{
throw new ProviderFatalException("Provider is in an irrecoverable error state.");
}
evaluation =
(await resolveValueDelegate.Invoke(flagKey, defaultValue, contextFromHooks.EvaluationContext, cancellationToken).ConfigureAwait(false))
.ToFlagEvaluationDetails();
if (evaluation.ErrorType == ErrorType.None)
{
await this.TriggerAfterHooksAsync(
allHooksReversed,
hookContext,
evaluation,
options,
cancellationToken
).ConfigureAwait(false);
}
else
{
var exception = new FeatureProviderException(evaluation.ErrorType, evaluation.ErrorMessage);
this.FlagEvaluationErrorWithDescription(flagKey, evaluation.ErrorType.GetDescription(), exception);
await this.TriggerErrorHooksAsync(allHooksReversed, hookContext, exception, options, cancellationToken)
.ConfigureAwait(false);
}
}
catch (FeatureProviderException ex)
{
this.FlagEvaluationErrorWithDescription(flagKey, ex.ErrorType.GetDescription(), ex);
evaluation = new FlagEvaluationDetails<T>(flagKey, defaultValue, ex.ErrorType, Reason.Error,
string.Empty, ex.Message);
await this.TriggerErrorHooksAsync(allHooksReversed, hookContext, ex, options, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
this.FlagEvaluationError(flagKey, ex);
var errorCode = ex is InvalidCastException ? ErrorType.TypeMismatch : ErrorType.General;
evaluation = new FlagEvaluationDetails<T>(flagKey, defaultValue, errorCode, Reason.Error, string.Empty, ex.Message);
await this.TriggerErrorHooksAsync(allHooksReversed, hookContext, ex, options, cancellationToken).ConfigureAwait(false);
}
finally
{
await this.TriggerFinallyHooksAsync(allHooksReversed, hookContext, options, cancellationToken).ConfigureAwait(false);
}
return evaluation;
}
private async Task<HookContext<T>> TriggerBeforeHooksAsync<T>(IReadOnlyList<Hook> hooks, HookContext<T> context,
FlagEvaluationOptions? options, CancellationToken cancellationToken = default)
{
var evalContextBuilder = EvaluationContext.Builder();
evalContextBuilder.Merge(context.EvaluationContext);
foreach (var hook in hooks)
{
var resp = await hook.BeforeAsync(context, options?.HookHints, cancellationToken).ConfigureAwait(false);
if (resp != null)
{
evalContextBuilder.Merge(resp);
context = context.WithNewEvaluationContext(evalContextBuilder.Build());
}
else
{
this.HookReturnedNull(hook.GetType().Name);
}
}
return context.WithNewEvaluationContext(evalContextBuilder.Build());
}
private async Task TriggerAfterHooksAsync<T>(IReadOnlyList<Hook> hooks, HookContext<T> context,
FlagEvaluationDetails<T> evaluationDetails, FlagEvaluationOptions? options, CancellationToken cancellationToken = default)
{
foreach (var hook in hooks)
{
await hook.AfterAsync(context, evaluationDetails, options?.HookHints, cancellationToken).ConfigureAwait(false);
}
}
private async Task TriggerErrorHooksAsync<T>(IReadOnlyList<Hook> hooks, HookContext<T> context, Exception exception,
FlagEvaluationOptions? options, CancellationToken cancellationToken = default)
{
foreach (var hook in hooks)
{
try
{
await hook.ErrorAsync(context, exception, options?.HookHints, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
this.ErrorHookError(hook.GetType().Name, e);
}
}
}
private async Task TriggerFinallyHooksAsync<T>(IReadOnlyList<Hook> hooks, HookContext<T> context,
FlagEvaluationOptions? options, CancellationToken cancellationToken = default)
{
foreach (var hook in hooks)
{
try
{
await hook.FinallyAsync(context, options?.HookHints, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
this.FinallyHookError(hook.GetType().Name, e);
}
}
}
[LoggerMessage(100, LogLevel.Debug, "Hook {HookName} returned null, nothing to merge back into context")]
partial void HookReturnedNull(string hookName);
[LoggerMessage(101, LogLevel.Error, "Error while evaluating flag {FlagKey}")]
partial void FlagEvaluationError(string flagKey, Exception exception);
[LoggerMessage(102, LogLevel.Error, "Error while evaluating flag {FlagKey}: {ErrorType}")]
partial void FlagEvaluationErrorWithDescription(string flagKey, string errorType, Exception exception);
[LoggerMessage(103, LogLevel.Error, "Error while executing Error hook {HookName}")]
partial void ErrorHookError(string hookName, Exception exception);
[LoggerMessage(104, LogLevel.Error, "Error while executing Finally hook {HookName}")]
partial void FinallyHookError(string hookName, Exception exception);
}
}