-
Notifications
You must be signed in to change notification settings - Fork 74
feat(statsig)!: Migrate to Java Core #1656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |
| </parent> | ||
| <groupId>dev.openfeature.contrib.providers</groupId> | ||
| <artifactId>statsig</artifactId> | ||
| <version>0.2.1</version> <!--x-release-please-version --> | ||
| <version>0.3.0</version> <!--x-release-please-version --> | ||
|
|
||
| <name>statsig</name> | ||
| <description>Statsig provider for Java</description> | ||
|
|
@@ -19,8 +19,9 @@ | |
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.statsig</groupId> | ||
| <artifactId>serversdk</artifactId> | ||
| <version>1.18.1</version> | ||
| <artifactId>javacore</artifactId> | ||
| <version>0.12.1</version> | ||
| <classifier>uber</classifier> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this uber? Does this include all of the transitive compiled versions for various platforms?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From docs:
Since we are like a library here for multi-platform, this includes all, and consumers can control it more precisely if wanted. |
||
| </dependency> | ||
|
|
||
| <dependency> | ||
|
|
@@ -36,5 +37,12 @@ | |
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <version>5.20.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| </dependencies> | ||
| </project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,10 @@ | ||
| package dev.openfeature.contrib.providers.statsig; | ||
|
|
||
| import com.statsig.sdk.APIFeatureGate; | ||
| import com.statsig.sdk.DynamicConfig; | ||
| import com.statsig.sdk.EvaluationReason; | ||
| import com.statsig.sdk.Layer; | ||
| import com.statsig.sdk.Statsig; | ||
| import com.statsig.sdk.StatsigUser; | ||
| import com.statsig.DynamicConfig; | ||
| import com.statsig.FeatureGate; | ||
| import com.statsig.Layer; | ||
| import com.statsig.Statsig; | ||
| import com.statsig.StatsigUser; | ||
| import dev.openfeature.sdk.EvaluationContext; | ||
| import dev.openfeature.sdk.EventProvider; | ||
| import dev.openfeature.sdk.Metadata; | ||
|
|
@@ -14,14 +13,11 @@ | |
| import dev.openfeature.sdk.Structure; | ||
| import dev.openfeature.sdk.Value; | ||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.SneakyThrows; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| /** Provider implementation for Statsig. */ | ||
| @Slf4j | ||
|
|
@@ -33,6 +29,9 @@ public class StatsigProvider extends EventProvider { | |
| private static final String FEATURE_CONFIG_KEY = "feature_config"; | ||
| private final StatsigProviderConfig statsigProviderConfig; | ||
|
|
||
| @Getter | ||
| private Statsig statsig; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
|
|
@@ -50,8 +49,8 @@ public StatsigProvider(StatsigProviderConfig statsigProviderConfig) { | |
| */ | ||
| @Override | ||
| public void initialize(EvaluationContext evaluationContext) throws Exception { | ||
| Future<Void> initFuture = | ||
| Statsig.initializeAsync(statsigProviderConfig.getSdkKey(), statsigProviderConfig.getOptions()); | ||
| statsig = new Statsig(statsigProviderConfig.getSdkKey(), statsigProviderConfig.getOptions()); | ||
| CompletableFuture<Void> initFuture = statsig.initialize(); | ||
| initFuture.get(); | ||
|
|
||
| statsigProviderConfig.postInit(); | ||
|
|
@@ -74,13 +73,14 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa | |
| Value featureConfigValue = ctx.getValue(FEATURE_CONFIG_KEY); | ||
| String reason = null; | ||
| if (featureConfigValue == null) { | ||
| APIFeatureGate featureGate = Statsig.getFeatureGate(user, key); | ||
| reason = featureGate.getReason().getReason(); | ||
| FeatureGate featureGate = statsig.getFeatureGate(user, key); | ||
| reason = featureGate.getEvaluationDetails().getReason(); | ||
| evaluatedValue = featureGate.getValue(); | ||
|
|
||
| // in case of evaluation failure, remain with default value. | ||
| if (!assumeFailure(featureGate)) { | ||
| evaluatedValue = featureGate.getValue(); | ||
| } | ||
| // if (!assumeFailure(featureGate)) { | ||
| // evaluatedValue = featureGate.getValue(); | ||
| // } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } else { | ||
| FeatureConfig featureConfig = parseFeatureConfig(ctx); | ||
| switch (featureConfig.getType()) { | ||
|
|
@@ -107,13 +107,13 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa | |
| https://github.com/statsig-io/java-server-sdk/issues/22#issuecomment-2002346349 | ||
| failure is assumed by reason, since success status is not returned. | ||
| */ | ||
| private boolean assumeFailure(APIFeatureGate featureGate) { | ||
| EvaluationReason reason = featureGate.getReason(); | ||
| return EvaluationReason.DEFAULT.equals(reason) | ||
| || EvaluationReason.UNINITIALIZED.equals(reason) | ||
| || EvaluationReason.UNRECOGNIZED.equals(reason) | ||
| || EvaluationReason.UNSUPPORTED.equals(reason); | ||
| } | ||
| // private boolean assumeFailure(FeatureGate featureGate) { | ||
| // EvaluationReason reason = featureGate.getEvaluationDetails().getReason(); | ||
| // return EvaluationReason.DEFAULT.equals(reason) | ||
| // || EvaluationReason.UNINITIALIZED.equals(reason) | ||
| // || EvaluationReason.UNRECOGNIZED.equals(reason) | ||
| // || EvaluationReason.UNSUPPORTED.equals(reason); | ||
| // } | ||
|
liran2000 marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Override | ||
| public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { | ||
|
|
@@ -198,26 +198,19 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa | |
|
|
||
| @SneakyThrows | ||
| protected DynamicConfig fetchDynamicConfig(StatsigUser user, FeatureConfig featureConfig) { | ||
| return Statsig.getConfigAsync(user, featureConfig.getName()).get(); | ||
| return statsig.getDynamicConfig(user, featureConfig.getName()); | ||
| } | ||
|
|
||
| @SneakyThrows | ||
| protected Layer fetchLayer(StatsigUser user, FeatureConfig featureConfig) { | ||
| return Statsig.getLayerAsync(user, featureConfig.getName()).get(); | ||
| return statsig.getLayer(user, featureConfig.getName()); | ||
| } | ||
|
|
||
| private Value toValue(DynamicConfig dynamicConfig) { | ||
| MutableContext mutableContext = new MutableContext(); | ||
| mutableContext.add("name", dynamicConfig.getName()); | ||
| mutableContext.add("value", Structure.mapToStructure(dynamicConfig.getValue())); | ||
| mutableContext.add("ruleID", dynamicConfig.getRuleID()); | ||
| mutableContext.add("groupName", dynamicConfig.getGroupName()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are these provided in a different way? If so document in the README.md
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. documented |
||
| List<Value> secondaryExposures = new ArrayList<>(); | ||
| dynamicConfig.getSecondaryExposures().forEach(secondaryExposure -> { | ||
| Value value = Value.objectToValue(secondaryExposure); | ||
| secondaryExposures.add(value); | ||
| }); | ||
| mutableContext.add("secondaryExposures", secondaryExposures); | ||
| return new Value(mutableContext); | ||
| } | ||
|
|
||
|
|
@@ -227,17 +220,11 @@ private Value toValue(Layer layer) { | |
| mutableContext.add("value", Structure.mapToStructure(layer.getValue())); | ||
| mutableContext.add("ruleID", layer.getRuleID()); | ||
| mutableContext.add("groupName", layer.getGroupName()); | ||
| List<Value> secondaryExposures = new ArrayList<>(); | ||
| layer.getSecondaryExposures().forEach(secondaryExposure -> { | ||
| Value value = Value.objectToValue(secondaryExposure); | ||
| secondaryExposures.add(value); | ||
| }); | ||
| mutableContext.add("secondaryExposures", secondaryExposures); | ||
| mutableContext.add("allocatedExperiment", layer.getAllocatedExperiment()); | ||
| mutableContext.add("allocatedExperiment", layer.getAllocatedExperimentName()); | ||
| return new Value(mutableContext); | ||
| } | ||
|
|
||
| @NotNull private static FeatureConfig parseFeatureConfig(EvaluationContext ctx) { | ||
| private static FeatureConfig parseFeatureConfig(EvaluationContext ctx) { | ||
| Value featureConfigValue = ctx.getValue(FEATURE_CONFIG_KEY); | ||
| if (featureConfigValue == null) { | ||
| throw new IllegalArgumentException("feature config not found at evaluation context."); | ||
|
|
@@ -262,8 +249,10 @@ private Value toValue(Layer layer) { | |
| @SneakyThrows | ||
| @Override | ||
| public void shutdown() { | ||
| log.info("shutdown"); | ||
| Statsig.shutdown(); | ||
| log.info("shutdown begin"); | ||
| CompletableFuture<Void> shutdownFuture = statsig.shutdown(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check if statsig != null? |
||
| shutdownFuture.get(); | ||
| log.info("shutdown end"); | ||
| } | ||
|
|
||
| /** Feature config, as required for evaluation. */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a minor grammatical error in the release notes. It should be "Migrated according to..." instead of "Migrated to according to...".