-
Notifications
You must be signed in to change notification settings - Fork 62
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
Open
liran2000
wants to merge
4
commits into
open-feature:main
Choose a base branch
from
liran2000:feature/statsig-1627
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−103
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,22 @@ | ||
| 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; | ||
| import dev.openfeature.sdk.MutableContext; | ||
| import dev.openfeature.sdk.ProviderEvaluation; | ||
| 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 +28,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 +48,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(); | ||
|
|
@@ -65,22 +63,15 @@ public Metadata getMetadata() { | |
|
|
||
| @SneakyThrows | ||
| @Override | ||
| @SuppressFBWarnings( | ||
| value = {"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"}, | ||
| justification = "reason can be null") | ||
| public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { | ||
| StatsigUser user = ContextTransformer.transform(ctx); | ||
| Boolean evaluatedValue = defaultValue; | ||
| Value featureConfigValue = ctx.getValue(FEATURE_CONFIG_KEY); | ||
| String reason = null; | ||
| if (featureConfigValue == null) { | ||
| APIFeatureGate featureGate = Statsig.getFeatureGate(user, key); | ||
| reason = featureGate.getReason().getReason(); | ||
|
|
||
| // in case of evaluation failure, remain with default value. | ||
| if (!assumeFailure(featureGate)) { | ||
| evaluatedValue = featureGate.getValue(); | ||
| } | ||
| FeatureGate featureGate = statsig.getFeatureGate(user, key); | ||
| reason = featureGate.getEvaluationDetails().getReason(); | ||
| evaluatedValue = featureGate.getValue(); | ||
| } else { | ||
| FeatureConfig featureConfig = parseFeatureConfig(ctx); | ||
| switch (featureConfig.getType()) { | ||
|
|
@@ -103,18 +94,6 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa | |
| .build(); | ||
| } | ||
|
|
||
| /* | ||
| 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); | ||
| } | ||
|
|
||
| @Override | ||
| public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { | ||
| StatsigUser user = ContextTransformer.transform(ctx); | ||
|
|
@@ -198,26 +177,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 +199,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 +228,12 @@ private Value toValue(Layer layer) { | |
| @SneakyThrows | ||
| @Override | ||
| public void shutdown() { | ||
| log.info("shutdown"); | ||
| Statsig.shutdown(); | ||
| log.info("shutdown begin"); | ||
| if (statsig != null) { | ||
| CompletableFuture<Void> shutdownFuture = statsig.shutdown(); | ||
| shutdownFuture.get(); | ||
| } | ||
| log.info("shutdown end"); | ||
| } | ||
|
|
||
| /** Feature config, as required for evaluation. */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this uber? Does this include all of the transitive compiled versions for various platforms?
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.
From docs:
Since we are like a library here for multi-platform, this includes all, and consumers can control it more precisely if wanted.