Skip to content

Commit 34069d3

Browse files
docs: fix inaccuracies in README code examples (#745)
* docs: fix inaccuracies in README code examples Signed-off-by: Jonathan Norris <jonathan.norris@dynatrace.com> * docs: convert README Main examples to async Task Main with await Address review feedback from @askpt: use idiomatic async/await in the TraceEnricherHook and MetricsHook examples instead of sync-over-async (.GetAwaiter().GetResult()). Signed-off-by: Jonathan Norris <jonathan.norris@dynatrace.com> --------- Signed-off-by: Jonathan Norris <jonathan.norris@dynatrace.com>
1 parent 7d89d55 commit 34069d3

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

README.md

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ builder = EvaluationContext.Builder();
175175
builder.Set("region", "us-east-1");
176176
EvaluationContext reqCtx = builder.Build();
177177
178-
bool flagValue = await client.GetBooleanValuAsync("some-flag", false, reqCtx);
178+
bool flagValue = await client.GetBooleanValueAsync("some-flag", false, reqCtx);
179179
180180
```
181181

@@ -300,7 +300,7 @@ For example, a flag enhancing the appearance of a UI component might drive user
300300
301301
```csharp
302302
var client = Api.Instance.GetClient();
303-
client.Track("visited-promo-page", trackingEventDetails: new TrackingEventDetailsBuilder().SetValue(99.77).Set("currency", "USD").Build());
303+
client.Track("visited-promo-page", trackingEventDetails: TrackingEventDetails.Builder().SetValue(99.77).Set("currency", "USD").Build());
304304
```
305305
306306
Note that some providers may not support tracking; check the documentation for your provider for more information.
@@ -364,7 +364,7 @@ public class MyProvider : FeatureProvider
364364
// resolve a string flag value
365365
}
366366
367-
public override Task<ResolutionDetails<int>> ResolveIntegerValueAsync(string flagKey, int defaultValue, EvaluationContext context = null)
367+
public override Task<ResolutionDetails<int>> ResolveIntegerValueAsync(string flagKey, int defaultValue, EvaluationContext? context = null, CancellationToken cancellationToken = default)
368368
{
369369
// resolve an int flag value
370370
}
@@ -391,25 +391,30 @@ To satisfy the interface, all methods (`Before`/`After`/`Finally`/`Error`) need
391391
```csharp
392392
public class MyHook : Hook
393393
{
394-
public ValueTask<EvaluationContext> BeforeAsync<T>(HookContext<T> context,
395-
IReadOnlyDictionary<string, object> hints = null)
394+
public override ValueTask<EvaluationContext> BeforeAsync<T>(HookContext<T> context,
395+
IReadOnlyDictionary<string, object>? hints = null,
396+
CancellationToken cancellationToken = default)
396397
{
397398
// code to run before flag evaluation
398399
}
399400
400-
public ValueTask AfterAsync<T>(HookContext<T> context, FlagEvaluationDetails<T> details,
401-
IReadOnlyDictionary<string, object>? hints = null)
401+
public override ValueTask AfterAsync<T>(HookContext<T> context, FlagEvaluationDetails<T> details,
402+
IReadOnlyDictionary<string, object>? hints = null,
403+
CancellationToken cancellationToken = default)
402404
{
403405
// code to run after successful flag evaluation
404406
}
405407
406-
public ValueTask ErrorAsync<T>(HookContext<T> context, Exception error,
407-
IReadOnlyDictionary<string, object>? hints = null)
408+
public override ValueTask ErrorAsync<T>(HookContext<T> context, Exception error,
409+
IReadOnlyDictionary<string, object>? hints = null,
410+
CancellationToken cancellationToken = default)
408411
{
409412
// code to run if there's an error during before hooks or during flag evaluation
410413
}
411414
412-
public ValueTask FinallyAsync<T>(HookContext<T> context, FlagEvaluationDetails<T> evaluationDetails, IReadOnlyDictionary<string, object> hints = null)
415+
public override ValueTask FinallyAsync<T>(HookContext<T> context, FlagEvaluationDetails<T> evaluationDetails,
416+
IReadOnlyDictionary<string, object>? hints = null,
417+
CancellationToken cancellationToken = default)
413418
{
414419
// code to run after all other stages, regardless of success/failure
415420
}
@@ -421,15 +426,17 @@ Hooks support passing per-evaluation data between that stages using `hook data`.
421426
```csharp
422427
class TimingHook : Hook
423428
{
424-
public ValueTask<EvaluationContext> BeforeAsync<T>(HookContext<T> context,
425-
IReadOnlyDictionary<string, object>? hints = null)
429+
public override ValueTask<EvaluationContext> BeforeAsync<T>(HookContext<T> context,
430+
IReadOnlyDictionary<string, object>? hints = null,
431+
CancellationToken cancellationToken = default)
426432
{
427433
context.Data.Set("beforeTime", DateTime.Now);
428434
return ValueTask.FromResult(context.EvaluationContext);
429435
}
430436
431-
public ValueTask AfterAsync<T>(HookContext<T> context, FlagEvaluationDetails<T> details,
432-
IReadOnlyDictionary<string, object>? hints = null)
437+
public override ValueTask AfterAsync<T>(HookContext<T> context, FlagEvaluationDetails<T> details,
438+
IReadOnlyDictionary<string, object>? hints = null,
439+
CancellationToken cancellationToken = default)
433440
{
434441
var beforeTime = context.Data.Get("beforeTime") as DateTime?;
435442
var duration = DateTime.Now - beforeTime;
@@ -656,6 +663,7 @@ Below are the tags added to the trace event:
656663
The following example demonstrates the use of the `TraceEnricherHook` with the `OpenFeature dotnet-sdk`. The traces are sent to a `jaeger` OTLP collector running at `localhost:4317`.
657664
658665
```csharp
666+
using System.Threading.Tasks;
659667
using OpenFeature.Contrib.Providers.Flagd;
660668
using OpenFeature.Hooks;
661669
using OpenTelemetry.Exporter;
@@ -666,7 +674,7 @@ using OpenTelemetry.Trace;
666674
namespace OpenFeatureTestApp
667675
{
668676
class Hello {
669-
static void Main(string[] args) {
677+
static async Task Main(string[] args) {
670678
671679
// set up the OpenTelemetry OTLP exporter
672680
var tracerProvider = Sdk.CreateTracerProviderBuilder()
@@ -684,14 +692,14 @@ namespace OpenFeatureTestApp
684692
var flagdProvider = new FlagdProvider(new Uri("http://localhost:8013"));
685693
686694
// Set the flagdProvider as the provider for the OpenFeature SDK
687-
OpenFeature.Api.Instance.SetProvider(flagdProvider);
695+
await OpenFeature.Api.Instance.SetProviderAsync(flagdProvider);
688696
689697
var client = OpenFeature.Api.Instance.GetClient("my-app");
690698
691-
var val = client.GetBooleanValueAsync("myBoolFlag", false, null);
699+
var val = await client.GetBooleanValueAsync("myBoolFlag", false);
692700
693701
// Print the value of the 'myBoolFlag' feature flag
694-
System.Console.WriteLine(val.Result.ToString());
702+
System.Console.WriteLine(val);
695703
}
696704
}
697705
}
@@ -738,6 +746,7 @@ Consider the following code example for usage.
738746
The following example demonstrates the use of the `MetricsHook` with the `OpenFeature dotnet-sdk`. The metrics are sent to the `console`.
739747
740748
```csharp
749+
using System.Threading.Tasks;
741750
using OpenFeature.Contrib.Providers.Flagd;
742751
using OpenFeature;
743752
using OpenFeature.Hooks;
@@ -747,7 +756,7 @@ using OpenTelemetry.Metrics;
747756
namespace OpenFeatureTestApp
748757
{
749758
class Hello {
750-
static void Main(string[] args) {
759+
static async Task Main(string[] args) {
751760
752761
// set up the OpenTelemetry OTLP exporter
753762
var meterProvider = Sdk.CreateMeterProviderBuilder()
@@ -762,14 +771,14 @@ namespace OpenFeatureTestApp
762771
var flagdProvider = new FlagdProvider(new Uri("http://localhost:8013"));
763772
764773
// Set the flagdProvider as the provider for the OpenFeature SDK
765-
OpenFeature.Api.Instance.SetProvider(flagdProvider);
774+
await OpenFeature.Api.Instance.SetProviderAsync(flagdProvider);
766775
767776
var client = OpenFeature.Api.Instance.GetClient("my-app");
768777
769-
var val = client.GetBooleanValueAsync("myBoolFlag", false, null);
778+
var val = await client.GetBooleanValueAsync("myBoolFlag", false);
770779
771780
// Print the value of the 'myBoolFlag' feature flag
772-
System.Console.WriteLine(val.Result.ToString());
781+
System.Console.WriteLine(val);
773782
}
774783
}
775784
}

0 commit comments

Comments
 (0)