Skip to content

Commit 881cb66

Browse files
ghelyardeleteLater
andauthored
feat: add DisableEvents option (#21)
* 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]>
1 parent 74cb695 commit 881cb66

File tree

6 files changed

+61
-16
lines changed

6 files changed

+61
-16
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ a [preview version](https://www.nuget.org/packages/FeatBit.ServerSdk/absoluteLat
3535

3636
### Prerequisite
3737

38-
Before using the SDK, you need to obtain the environment secret and SDK URLs.
38+
Before using the SDK, you need to obtain the environment secret and SDK URLs.
3939

4040
Follow the documentation below to retrieve these values
4141

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

207207
> **Note**
208-
> 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.
208+
> 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.
209209
210210
Variation calls take the feature flag key, a FbUser, and a default value. If any error makes it impossible to
211211
evaluate the flag (for instance, the feature flag key does not match any existing flag), default value is returned.
@@ -247,7 +247,7 @@ var options = new FbOptionsBuilder()
247247
var client = new FbClient(options);
248248
```
249249

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

281+
### Disable Events Collection
282+
283+
By default, the SDK automatically sends events (flag evaluation events and metric events for A/B testing) to the FeatBit
284+
server, unless the SDK is in offline mode.
285+
286+
If you prefer to disable this event collection while the SDK is in online mode, you
287+
can configure this behavior using the `DisableEvents` option.
288+
289+
```csharp
290+
var options = new FbOptionsBuilder()
291+
.DisableEvents(true)
292+
.Build();
293+
```
294+
281295
### Experiments (A/B/n Testing)
282296

283297
We support automatic experiments for pageviews and clicks, you just need to set your experiment on our SaaS platform,

src/FeatBit.ServerSdk/FbClient.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,20 @@ public FbClient(FbOptions options)
123123
_store = new DefaultMemoryStore();
124124
_evaluator = new Evaluator(_store);
125125

126+
_eventProcessor = _options.Offline || _options.DisableEvents
127+
? new NullEventProcessor()
128+
: new DefaultEventProcessor(options);
129+
126130
if (_options.Offline)
127131
{
128132
_dataSynchronizer = new NullDataSynchronizer();
129-
_eventProcessor = new NullEventProcessor();
130133

131134
// use bootstrap provider to populate store
132135
_options.BootstrapProvider.Populate(_store);
133136
}
134137
else
135138
{
136139
_dataSynchronizer = new WebSocketDataSynchronizer(options, _store);
137-
_eventProcessor = new DefaultEventProcessor(options);
138140
}
139141

140142
_logger = options.LoggerFactory.CreateLogger<FbClient>();

src/FeatBit.ServerSdk/Options/FbOptions.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed class FbOptions
1717
public TimeSpan StartWaitTime { get; set; }
1818

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

31+
/// <summary>
32+
/// Whether to disable events collection.
33+
/// </summary>
34+
public bool DisableEvents { get; set; }
35+
3136
/// <summary>
3237
/// The SDK key for your FeatBit environment.
3338
/// </summary>
@@ -132,6 +137,7 @@ public static FbOptions Default(string secret)
132137
internal FbOptions(
133138
TimeSpan startWaitTime,
134139
bool offline,
140+
bool disableEvents,
135141
string envSecret,
136142
Uri streamingUri,
137143
Uri eventUri,
@@ -151,6 +157,7 @@ internal FbOptions(
151157
{
152158
StartWaitTime = startWaitTime;
153159
Offline = offline;
160+
DisableEvents = disableEvents;
154161

155162
EnvSecret = envSecret;
156163
StreamingUri = streamingUri;
@@ -175,10 +182,10 @@ internal FbOptions(
175182

176183
internal FbOptions ShallowCopy()
177184
{
178-
var newOptions = new FbOptions(StartWaitTime, Offline, EnvSecret, StreamingUri, EventUri, ConnectTimeout,
179-
CloseTimeout, KeepAliveInterval, ReconnectRetryDelays, MaxFlushWorker, AutoFlushInterval, FlushTimeout,
180-
MaxEventsInQueue, MaxEventPerRequest, MaxSendEventAttempts, SendEventRetryInterval, BootstrapProvider,
181-
LoggerFactory);
185+
var newOptions = new FbOptions(StartWaitTime, Offline, DisableEvents, EnvSecret, StreamingUri, EventUri,
186+
ConnectTimeout, CloseTimeout, KeepAliveInterval, ReconnectRetryDelays, MaxFlushWorker,
187+
AutoFlushInterval, FlushTimeout, MaxEventsInQueue, MaxEventPerRequest, MaxSendEventAttempts,
188+
SendEventRetryInterval, BootstrapProvider, LoggerFactory);
182189

183190
return newOptions;
184191
}

src/FeatBit.ServerSdk/Options/FbOptionsBuilder.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class FbOptionsBuilder
1010
{
1111
private TimeSpan _startWaitTime;
1212
private bool _offline;
13+
private bool _disableEvents;
1314

1415
private readonly string _envSecret;
1516

@@ -41,6 +42,7 @@ public FbOptionsBuilder(string envSecret)
4142
{
4243
_startWaitTime = TimeSpan.FromSeconds(5);
4344
_offline = false;
45+
_disableEvents = false;
4446

4547
_envSecret = envSecret;
4648

@@ -70,10 +72,10 @@ public FbOptionsBuilder(string envSecret)
7072

7173
public FbOptions Build()
7274
{
73-
return new FbOptions(_startWaitTime, _offline, _envSecret, _streamingUri, _eventUri, _connectTimeout,
74-
_closeTimeout, _keepAliveInterval, _reconnectRetryDelays, _maxFlushWorker, _autoFlushInterval,
75-
_flushTimeout, _maxEventsInQueue, _maxEventPerRequest, _maxSendEventAttempts, _sendEventRetryInterval,
76-
_bootstrapProvider, _loggerFactory);
75+
return new FbOptions(_startWaitTime, _offline, _disableEvents, _envSecret, _streamingUri, _eventUri,
76+
_connectTimeout, _closeTimeout, _keepAliveInterval, _reconnectRetryDelays, _maxFlushWorker,
77+
_autoFlushInterval, _flushTimeout, _maxEventsInQueue, _maxEventPerRequest, _maxSendEventAttempts,
78+
_sendEventRetryInterval, _bootstrapProvider, _loggerFactory);
7779
}
7880

7981
public FbOptionsBuilder StartWaitTime(TimeSpan timeout)
@@ -122,6 +124,12 @@ public FbOptionsBuilder CloseTimeout(TimeSpan timeout)
122124
return this;
123125
}
124126

127+
public FbOptionsBuilder DisableEvents(bool disableEvents)
128+
{
129+
_disableEvents = disableEvents;
130+
return this;
131+
}
132+
125133
public FbOptionsBuilder MaxFlushWorker(int maxFlushWorker)
126134
{
127135
_maxFlushWorker = maxFlushWorker;

tests/FeatBit.ServerSdk.Tests/FbClientOfflineTests.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public async Task CreateAndClose()
2020
}
2121

2222
[Fact]
23-
public void UseNullEventProcessor()
23+
public void UseNullDataSource()
2424
{
2525
var options = new FbOptionsBuilder()
2626
.Offline(true)
@@ -32,7 +32,7 @@ public void UseNullEventProcessor()
3232
}
3333

3434
[Fact]
35-
public void UseNullDataSource()
35+
public void UseNullEventProcessor()
3636
{
3737
var options = new FbOptionsBuilder()
3838
.Offline(true)
@@ -43,6 +43,19 @@ public void UseNullDataSource()
4343
Assert.IsType<NullEventProcessor>(client._eventProcessor);
4444
}
4545

46+
[Fact]
47+
public void UseNullEventProcessorWhenEventsAreDisabled()
48+
{
49+
var options = new FbOptionsBuilder()
50+
.Offline(false)
51+
.DisableEvents(true)
52+
.Build();
53+
54+
var client = new FbClient(options);
55+
56+
Assert.IsType<NullEventProcessor>(client._eventProcessor);
57+
}
58+
4659
[Fact]
4760
public void ClientIsInitialized()
4861
{

tests/FeatBit.ServerSdk.Tests/Options/FbOptionsBuilderTests.HasDefaultValues.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
StartWaitTime: 00:00:05,
33
Offline: false,
4+
DisableEvents: false,
45
EnvSecret: secret,
56
StreamingUri: ws://localhost:5100,
67
EventUri: http://localhost:5100,

0 commit comments

Comments
 (0)