Skip to content

Commit 6039699

Browse files
committed
0.3.0
Signed-off-by: liran2000 <liran2000@gmail.com>
1 parent 06c5798 commit 6039699

File tree

6 files changed

+140
-110
lines changed

6 files changed

+140
-110
lines changed

providers/statsig/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@ As it is limited, evaluation context based tests are limited.
7474
See [statsigProviderTest](./src/test/java/dev/openfeature/contrib/providers/statsig/StatsigProviderTest.java)
7575
for more information.
7676

77+
## Release Notes
7778

79+
### 0.3.0
80+
Migrated to according to [Migration guide](https://docs.statsig.com/server-core/migration-guides/java#java-migration-steps).

providers/statsig/pom.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>dev.openfeature.contrib.providers</groupId>
1212
<artifactId>statsig</artifactId>
13-
<version>0.2.1</version> <!--x-release-please-version -->
13+
<version>0.3.0</version> <!--x-release-please-version -->
1414

1515
<name>statsig</name>
1616
<description>Statsig provider for Java</description>
@@ -19,8 +19,9 @@
1919
<dependencies>
2020
<dependency>
2121
<groupId>com.statsig</groupId>
22-
<artifactId>serversdk</artifactId>
23-
<version>1.18.1</version>
22+
<artifactId>javacore</artifactId>
23+
<version>0.12.1</version>
24+
<classifier>uber</classifier>
2425
</dependency>
2526

2627
<dependency>
@@ -36,5 +37,12 @@
3637
<scope>test</scope>
3738
</dependency>
3839

40+
<dependency>
41+
<groupId>org.mockito</groupId>
42+
<artifactId>mockito-core</artifactId>
43+
<version>5.20.0</version>
44+
<scope>test</scope>
45+
</dependency>
46+
3947
</dependencies>
4048
</project>

providers/statsig/src/main/java/dev/openfeature/contrib/providers/statsig/ContextTransformer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.openfeature.contrib.providers.statsig;
22

3-
import com.statsig.sdk.StatsigUser;
3+
import com.statsig.StatsigUser;
44
import dev.openfeature.sdk.EvaluationContext;
55
import dev.openfeature.sdk.Structure;
66
import dev.openfeature.sdk.Value;
@@ -23,7 +23,8 @@ static StatsigUser transform(EvaluationContext ctx) {
2323
if (ctx.getTargetingKey() == null) {
2424
throw new TargetingKeyMissingError("targeting key is required.");
2525
}
26-
StatsigUser user = new StatsigUser(ctx.getTargetingKey());
26+
StatsigUser.Builder user = new StatsigUser.Builder()
27+
.setUserID(ctx.getTargetingKey());
2728
Map<String, String> customMap = new HashMap<>();
2829
ctx.asObjectMap().forEach((k, v) -> {
2930
switch (k) {
@@ -64,6 +65,6 @@ static StatsigUser transform(EvaluationContext ctx) {
6465
privateAttributesStructure.asObjectMap().forEach((k, v) -> privateMap.put(k, String.valueOf(v)));
6566
user.setPrivateAttributes(privateMap);
6667
}
67-
return user;
68+
return user.build();
6869
}
6970
}

providers/statsig/src/main/java/dev/openfeature/contrib/providers/statsig/StatsigProvider.java

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package dev.openfeature.contrib.providers.statsig;
22

3-
import com.statsig.sdk.APIFeatureGate;
4-
import com.statsig.sdk.DynamicConfig;
5-
import com.statsig.sdk.EvaluationReason;
6-
import com.statsig.sdk.Layer;
7-
import com.statsig.sdk.Statsig;
8-
import com.statsig.sdk.StatsigUser;
3+
import com.statsig.DynamicConfig;
4+
import com.statsig.FeatureGate;
5+
import com.statsig.Layer;
6+
import com.statsig.Statsig;
7+
import com.statsig.StatsigUser;
98
import dev.openfeature.sdk.EvaluationContext;
109
import dev.openfeature.sdk.EventProvider;
1110
import dev.openfeature.sdk.Metadata;
@@ -14,14 +13,11 @@
1413
import dev.openfeature.sdk.Structure;
1514
import dev.openfeature.sdk.Value;
1615
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17-
import java.util.ArrayList;
18-
import java.util.List;
19-
import java.util.concurrent.Future;
16+
import java.util.concurrent.CompletableFuture;
2017
import lombok.AllArgsConstructor;
2118
import lombok.Getter;
2219
import lombok.SneakyThrows;
2320
import lombok.extern.slf4j.Slf4j;
24-
import org.jetbrains.annotations.NotNull;
2521

2622
/** Provider implementation for Statsig. */
2723
@Slf4j
@@ -33,6 +29,9 @@ public class StatsigProvider extends EventProvider {
3329
private static final String FEATURE_CONFIG_KEY = "feature_config";
3430
private final StatsigProviderConfig statsigProviderConfig;
3531

32+
@Getter
33+
private Statsig statsig;
34+
3635
/**
3736
* Constructor.
3837
*
@@ -50,8 +49,8 @@ public StatsigProvider(StatsigProviderConfig statsigProviderConfig) {
5049
*/
5150
@Override
5251
public void initialize(EvaluationContext evaluationContext) throws Exception {
53-
Future<Void> initFuture =
54-
Statsig.initializeAsync(statsigProviderConfig.getSdkKey(), statsigProviderConfig.getOptions());
52+
statsig = new Statsig(statsigProviderConfig.getSdkKey(), statsigProviderConfig.getOptions());
53+
CompletableFuture<Void> initFuture = statsig.initialize();
5554
initFuture.get();
5655

5756
statsigProviderConfig.postInit();
@@ -74,13 +73,14 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
7473
Value featureConfigValue = ctx.getValue(FEATURE_CONFIG_KEY);
7574
String reason = null;
7675
if (featureConfigValue == null) {
77-
APIFeatureGate featureGate = Statsig.getFeatureGate(user, key);
78-
reason = featureGate.getReason().getReason();
76+
FeatureGate featureGate = statsig.getFeatureGate(user, key);
77+
reason = featureGate.getEvaluationDetails().getReason();
78+
evaluatedValue = featureGate.getValue();
7979

8080
// in case of evaluation failure, remain with default value.
81-
if (!assumeFailure(featureGate)) {
82-
evaluatedValue = featureGate.getValue();
83-
}
81+
// if (!assumeFailure(featureGate)) {
82+
// evaluatedValue = featureGate.getValue();
83+
// }
8484
} else {
8585
FeatureConfig featureConfig = parseFeatureConfig(ctx);
8686
switch (featureConfig.getType()) {
@@ -107,13 +107,13 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
107107
https://github.com/statsig-io/java-server-sdk/issues/22#issuecomment-2002346349
108108
failure is assumed by reason, since success status is not returned.
109109
*/
110-
private boolean assumeFailure(APIFeatureGate featureGate) {
111-
EvaluationReason reason = featureGate.getReason();
112-
return EvaluationReason.DEFAULT.equals(reason)
113-
|| EvaluationReason.UNINITIALIZED.equals(reason)
114-
|| EvaluationReason.UNRECOGNIZED.equals(reason)
115-
|| EvaluationReason.UNSUPPORTED.equals(reason);
116-
}
110+
// private boolean assumeFailure(FeatureGate featureGate) {
111+
// EvaluationReason reason = featureGate.getEvaluationDetails().getReason();
112+
// return EvaluationReason.DEFAULT.equals(reason)
113+
// || EvaluationReason.UNINITIALIZED.equals(reason)
114+
// || EvaluationReason.UNRECOGNIZED.equals(reason)
115+
// || EvaluationReason.UNSUPPORTED.equals(reason);
116+
// }
117117

118118
@Override
119119
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) {
@@ -198,26 +198,19 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
198198

199199
@SneakyThrows
200200
protected DynamicConfig fetchDynamicConfig(StatsigUser user, FeatureConfig featureConfig) {
201-
return Statsig.getConfigAsync(user, featureConfig.getName()).get();
201+
return statsig.getDynamicConfig(user, featureConfig.getName());
202202
}
203203

204204
@SneakyThrows
205205
protected Layer fetchLayer(StatsigUser user, FeatureConfig featureConfig) {
206-
return Statsig.getLayerAsync(user, featureConfig.getName()).get();
206+
return statsig.getLayer(user, featureConfig.getName());
207207
}
208208

209209
private Value toValue(DynamicConfig dynamicConfig) {
210210
MutableContext mutableContext = new MutableContext();
211211
mutableContext.add("name", dynamicConfig.getName());
212212
mutableContext.add("value", Structure.mapToStructure(dynamicConfig.getValue()));
213213
mutableContext.add("ruleID", dynamicConfig.getRuleID());
214-
mutableContext.add("groupName", dynamicConfig.getGroupName());
215-
List<Value> secondaryExposures = new ArrayList<>();
216-
dynamicConfig.getSecondaryExposures().forEach(secondaryExposure -> {
217-
Value value = Value.objectToValue(secondaryExposure);
218-
secondaryExposures.add(value);
219-
});
220-
mutableContext.add("secondaryExposures", secondaryExposures);
221214
return new Value(mutableContext);
222215
}
223216

@@ -227,17 +220,11 @@ private Value toValue(Layer layer) {
227220
mutableContext.add("value", Structure.mapToStructure(layer.getValue()));
228221
mutableContext.add("ruleID", layer.getRuleID());
229222
mutableContext.add("groupName", layer.getGroupName());
230-
List<Value> secondaryExposures = new ArrayList<>();
231-
layer.getSecondaryExposures().forEach(secondaryExposure -> {
232-
Value value = Value.objectToValue(secondaryExposure);
233-
secondaryExposures.add(value);
234-
});
235-
mutableContext.add("secondaryExposures", secondaryExposures);
236-
mutableContext.add("allocatedExperiment", layer.getAllocatedExperiment());
223+
mutableContext.add("allocatedExperiment", layer.getAllocatedExperimentName());
237224
return new Value(mutableContext);
238225
}
239226

240-
@NotNull private static FeatureConfig parseFeatureConfig(EvaluationContext ctx) {
227+
private static FeatureConfig parseFeatureConfig(EvaluationContext ctx) {
241228
Value featureConfigValue = ctx.getValue(FEATURE_CONFIG_KEY);
242229
if (featureConfigValue == null) {
243230
throw new IllegalArgumentException("feature config not found at evaluation context.");
@@ -262,8 +249,10 @@ private Value toValue(Layer layer) {
262249
@SneakyThrows
263250
@Override
264251
public void shutdown() {
265-
log.info("shutdown");
266-
Statsig.shutdown();
252+
log.info("shutdown begin");
253+
CompletableFuture<Void> shutdownFuture = statsig.shutdown();
254+
shutdownFuture.get();
255+
log.info("shutdown end");
267256
}
268257

269258
/** Feature config, as required for evaluation. */

providers/statsig/src/main/java/dev/openfeature/contrib/providers/statsig/StatsigProviderConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.openfeature.contrib.providers.statsig;
22

3-
import com.statsig.sdk.StatsigOptions;
3+
import com.statsig.StatsigOptions;
44
import lombok.Builder;
55
import lombok.Getter;
66

@@ -10,7 +10,7 @@
1010
public class StatsigProviderConfig {
1111

1212
@Builder.Default
13-
private StatsigOptions options = new StatsigOptions();
13+
private StatsigOptions options = new StatsigOptions.Builder().build();
1414

1515
// Only holding temporary for initialization
1616
private String sdkKey;

0 commit comments

Comments
 (0)