Skip to content

Commit

Permalink
feat: add DisableEvents option (#21)
Browse files Browse the repository at this point in the history
* feat: add DisableEvents option

Signed-off-by: George Helyar <[email protected]>

* nit changes

* add doc

* typos

---------

Signed-off-by: George Helyar <[email protected]>
Co-authored-by: deleteLater <[email protected]>
  • Loading branch information
ghelyar and deleteLater authored Aug 29, 2024
1 parent 74cb695 commit 881cb66
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ a [preview version](https://www.nuget.org/packages/FeatBit.ServerSdk/absoluteLat

### Prerequisite

Before using the SDK, you need to obtain the environment secret and SDK URLs.
Before using the SDK, you need to obtain the environment secret and SDK URLs.

Follow the documentation below to retrieve these values

Expand Down Expand Up @@ -205,7 +205,7 @@ describing how the value was determined for each type.
- JsonVariation/JsonVariationDetail (in consideration)

> **Note**
> Since the current version does not have native support for retrieving JSON variations, you can utilize the `StringVariation` method as an alternative to obtain the JSON string.
> Since the current version does not have native support for retrieving JSON variations, you can use the `StringVariation` method as an alternative to get the JSON string.
Variation calls take the feature flag key, a FbUser, and a default value. If any error makes it impossible to
evaluate the flag (for instance, the feature flag key does not match any existing flag), default value is returned.
Expand Down Expand Up @@ -247,7 +247,7 @@ var options = new FbOptionsBuilder()
var client = new FbClient(options);
```

When you put the SDK in offline mode, no insight message is sent to the server and all feature flag evaluations return
When you put the SDK in offline mode, no events are sent to the server and all feature flag evaluations return
fallback values because there are no feature flags or segments available. If you want to use your own data source in
this case, the sdk allows users to populate feature flags and segments data from a JSON string. Here is an
example: [featbit-bootstrap.json](/tests/FeatBit.ServerSdk.Tests/Bootstrapping/featbit-bootstrap.json).
Expand Down Expand Up @@ -278,6 +278,20 @@ var options = new FbOptionsBuilder()
var client = new FbClient(options);
```

### Disable Events Collection

By default, the SDK automatically sends events (flag evaluation events and metric events for A/B testing) to the FeatBit
server, unless the SDK is in offline mode.

If you prefer to disable this event collection while the SDK is in online mode, you
can configure this behavior using the `DisableEvents` option.

```csharp
var options = new FbOptionsBuilder()
.DisableEvents(true)
.Build();
```

### Experiments (A/B/n Testing)

We support automatic experiments for pageviews and clicks, you just need to set your experiment on our SaaS platform,
Expand Down
6 changes: 4 additions & 2 deletions src/FeatBit.ServerSdk/FbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,20 @@ public FbClient(FbOptions options)
_store = new DefaultMemoryStore();
_evaluator = new Evaluator(_store);

_eventProcessor = _options.Offline || _options.DisableEvents
? new NullEventProcessor()
: new DefaultEventProcessor(options);

if (_options.Offline)
{
_dataSynchronizer = new NullDataSynchronizer();
_eventProcessor = new NullEventProcessor();

// use bootstrap provider to populate store
_options.BootstrapProvider.Populate(_store);
}
else
{
_dataSynchronizer = new WebSocketDataSynchronizer(options, _store);
_eventProcessor = new DefaultEventProcessor(options);
}

_logger = options.LoggerFactory.CreateLogger<FbClient>();
Expand Down
17 changes: 12 additions & 5 deletions src/FeatBit.ServerSdk/Options/FbOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class FbOptions
public TimeSpan StartWaitTime { get; set; }

/// <summary>
/// Whether or not this client is offline. If true, no calls to FeatBit will be made.
/// Whether this client is offline. If true, no calls to FeatBit will be made.
/// </summary>
/// <value>Defaults to <c>false</c></value>
public bool Offline { get; set; }
Expand All @@ -28,6 +28,11 @@ public sealed class FbOptions
/// <value>Defaults to <see cref="NullBootstrapProvider"/></value>
internal IBootstrapProvider BootstrapProvider { get; set; }

/// <summary>
/// Whether to disable events collection.
/// </summary>
public bool DisableEvents { get; set; }

/// <summary>
/// The SDK key for your FeatBit environment.
/// </summary>
Expand Down Expand Up @@ -132,6 +137,7 @@ public static FbOptions Default(string secret)
internal FbOptions(
TimeSpan startWaitTime,
bool offline,
bool disableEvents,
string envSecret,
Uri streamingUri,
Uri eventUri,
Expand All @@ -151,6 +157,7 @@ internal FbOptions(
{
StartWaitTime = startWaitTime;
Offline = offline;
DisableEvents = disableEvents;

EnvSecret = envSecret;
StreamingUri = streamingUri;
Expand All @@ -175,10 +182,10 @@ internal FbOptions(

internal FbOptions ShallowCopy()
{
var newOptions = new FbOptions(StartWaitTime, Offline, EnvSecret, StreamingUri, EventUri, ConnectTimeout,
CloseTimeout, KeepAliveInterval, ReconnectRetryDelays, MaxFlushWorker, AutoFlushInterval, FlushTimeout,
MaxEventsInQueue, MaxEventPerRequest, MaxSendEventAttempts, SendEventRetryInterval, BootstrapProvider,
LoggerFactory);
var newOptions = new FbOptions(StartWaitTime, Offline, DisableEvents, EnvSecret, StreamingUri, EventUri,
ConnectTimeout, CloseTimeout, KeepAliveInterval, ReconnectRetryDelays, MaxFlushWorker,
AutoFlushInterval, FlushTimeout, MaxEventsInQueue, MaxEventPerRequest, MaxSendEventAttempts,
SendEventRetryInterval, BootstrapProvider, LoggerFactory);

return newOptions;
}
Expand Down
16 changes: 12 additions & 4 deletions src/FeatBit.ServerSdk/Options/FbOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class FbOptionsBuilder
{
private TimeSpan _startWaitTime;
private bool _offline;
private bool _disableEvents;

private readonly string _envSecret;

Expand Down Expand Up @@ -41,6 +42,7 @@ public FbOptionsBuilder(string envSecret)
{
_startWaitTime = TimeSpan.FromSeconds(5);
_offline = false;
_disableEvents = false;

_envSecret = envSecret;

Expand Down Expand Up @@ -70,10 +72,10 @@ public FbOptionsBuilder(string envSecret)

public FbOptions Build()
{
return new FbOptions(_startWaitTime, _offline, _envSecret, _streamingUri, _eventUri, _connectTimeout,
_closeTimeout, _keepAliveInterval, _reconnectRetryDelays, _maxFlushWorker, _autoFlushInterval,
_flushTimeout, _maxEventsInQueue, _maxEventPerRequest, _maxSendEventAttempts, _sendEventRetryInterval,
_bootstrapProvider, _loggerFactory);
return new FbOptions(_startWaitTime, _offline, _disableEvents, _envSecret, _streamingUri, _eventUri,
_connectTimeout, _closeTimeout, _keepAliveInterval, _reconnectRetryDelays, _maxFlushWorker,
_autoFlushInterval, _flushTimeout, _maxEventsInQueue, _maxEventPerRequest, _maxSendEventAttempts,
_sendEventRetryInterval, _bootstrapProvider, _loggerFactory);
}

public FbOptionsBuilder StartWaitTime(TimeSpan timeout)
Expand Down Expand Up @@ -122,6 +124,12 @@ public FbOptionsBuilder CloseTimeout(TimeSpan timeout)
return this;
}

public FbOptionsBuilder DisableEvents(bool disableEvents)
{
_disableEvents = disableEvents;
return this;
}

public FbOptionsBuilder MaxFlushWorker(int maxFlushWorker)
{
_maxFlushWorker = maxFlushWorker;
Expand Down
17 changes: 15 additions & 2 deletions tests/FeatBit.ServerSdk.Tests/FbClientOfflineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task CreateAndClose()
}

[Fact]
public void UseNullEventProcessor()
public void UseNullDataSource()
{
var options = new FbOptionsBuilder()
.Offline(true)
Expand All @@ -32,7 +32,7 @@ public void UseNullEventProcessor()
}

[Fact]
public void UseNullDataSource()
public void UseNullEventProcessor()
{
var options = new FbOptionsBuilder()
.Offline(true)
Expand All @@ -43,6 +43,19 @@ public void UseNullDataSource()
Assert.IsType<NullEventProcessor>(client._eventProcessor);
}

[Fact]
public void UseNullEventProcessorWhenEventsAreDisabled()
{
var options = new FbOptionsBuilder()
.Offline(false)
.DisableEvents(true)
.Build();

var client = new FbClient(options);

Assert.IsType<NullEventProcessor>(client._eventProcessor);
}

[Fact]
public void ClientIsInitialized()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
StartWaitTime: 00:00:05,
Offline: false,
DisableEvents: false,
EnvSecret: secret,
StreamingUri: ws://localhost:5100,
EventUri: http://localhost:5100,
Expand Down

0 comments on commit 881cb66

Please sign in to comment.