-
Notifications
You must be signed in to change notification settings - Fork 52
feat: Add Statsig provider #641
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2e41a9e
Statsig provider initial draft
liran2000 f463aee
add Statsig provider
liran2000 9b2acd4
minor updates
liran2000 d5bb56f
change concept to feature config
liran2000 b541c98
add Object evaluation support
liran2000 440463a
minor updates
liran2000 e2fbedc
docs updates
liran2000 8c06bb3
remove try catch blocks
liran2000 50b4731
update fields scope
liran2000 fb54330
updates
liran2000 19fa93a
Update providers/statsig/src/main/java/dev/openfeature/contrib/provid…
toddbaert dce0a7e
Update providers/statsig/src/main/java/dev/openfeature/contrib/provid…
toddbaert 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Changelog |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Unofficial Statsig OpenFeature Provider for Java | ||
|
||
[Statsig](https://statsig.com/) OpenFeature Provider can provide usage for Statsig via OpenFeature Java SDK. | ||
|
||
## Installation | ||
|
||
<!-- x-release-please-start-version --> | ||
|
||
```xml | ||
|
||
<dependency> | ||
<groupId>dev.openfeature.contrib.providers</groupId> | ||
<artifactId>statsig</artifactId> | ||
<version>0.0.1</version> | ||
</dependency> | ||
``` | ||
|
||
<!-- x-release-please-end-version --> | ||
|
||
## Concepts | ||
* Boolean evaluation gets [gate](https://docs.statsig.com/server/javaSdk#checking-a-gate) status. | ||
* String/Integer/Double evaluations evaluation gets [Dynamic config](https://docs.statsig.com/server/javaSdk#reading-a-dynamic-config) or [Layer](https://docs.statsig.com/server/javaSdk#getting-an-layerexperiment) evaluation. | ||
As the key represents an inner attribute, feature config is required as a parameter with data needed for evaluation. | ||
For an example of dynamic config of product alias, need to differentiate between dynamic config or layer, and the dynamic config name. | ||
* Object evaluation gets a structure representing the dynamic config or layer. | ||
* [Private Attributes](https://docs.statsig.com/server/javaSdk#private-attributes) are supported as 'privateAttributes' context key. | ||
|
||
## Usage | ||
Statsig OpenFeature Provider is based on [Statsig Java SDK documentation](https://docs.statsig.com/server/javaSdk). | ||
|
||
### Usage Example | ||
|
||
``` | ||
StatsigOptions statsigOptions = new StatsigOptions(); | ||
StatsigProviderConfig statsigProviderConfig = StatsigProviderConfig.builder().sdkKey(sdkKey) | ||
.options(statsigOptions).build(); | ||
statsigProvider = new StatsigProvider(statsigProviderConfig); | ||
OpenFeatureAPI.getInstance().setProviderAndWait(statsigProvider); | ||
|
||
boolean featureEnabled = client.getBooleanValue(FLAG_NAME, false); | ||
|
||
MutableContext evaluationContext = new MutableContext(); | ||
MutableContext featureConfig = new MutableContext(); | ||
featureConfig.add("type", "CONFIG"); | ||
featureConfig.add("name", "product"); | ||
evaluationContext.add("feature_config", featureConfig); | ||
String value = statsigProvider.getStringEvaluation("alias", "fallback", evaluationContext).getValue()); | ||
|
||
MutableContext evaluationContext = new MutableContext(); | ||
evaluationContext.setTargetingKey("test-id"); | ||
evaluationContext.add("Email", "[email protected]"); | ||
MutableContext privateAttributes = new MutableContext(); | ||
privateAttributes.add("locale", locale); | ||
evaluationContext.add("privateAttributes", privateAttributes); | ||
featureEnabled = client.getBooleanValue(USERS_FLAG_NAME, false, evaluationContext); | ||
``` | ||
|
||
See [StatsigProviderTest](./src/test/java/dev/openfeature/contrib/providers/statsig/StatsigProviderTest.java) | ||
for more information. | ||
|
||
## Notes | ||
Some Statsig custom operations are supported from the Statsig client via: | ||
|
||
```java | ||
Statsig... | ||
``` | ||
|
||
## Statsig Provider Tests Strategies | ||
|
||
Unit test based on Statsig [Local Overrides](https://docs.statsig.com/server/javaSdk#local-overrides) and mocking. | ||
As it is limited, evaluation context based tests are limited. | ||
See [statsigProviderTest](./src/test/java/dev/openfeature/contrib/providers/statsig/StatsigProviderTest.java) | ||
for more information. |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This file is needed to avoid errors throw by findbugs when working with lombok. | ||
lombok.addSuppressWarnings = true | ||
lombok.addLombokGeneratedAnnotation = true | ||
config.stopBubbling = true | ||
lombok.extern.findbugs.addSuppressFBWarnings = true |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>dev.openfeature.contrib</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>0.1.0</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<groupId>dev.openfeature.contrib.providers</groupId> | ||
<artifactId>statsig</artifactId> | ||
<version>0.0.1</version> <!--x-release-please-version --> | ||
|
||
<name>statsig</name> | ||
<description>Statsig provider for Java</description> | ||
<url>https://statsig.com/</url> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.statsig</groupId> | ||
<artifactId>serversdk</artifactId> | ||
<version>1.10.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>2.0.11</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j2-impl</artifactId> | ||
<version>2.22.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
69 changes: 69 additions & 0 deletions
69
...s/statsig/src/main/java/dev/openfeature/contrib/providers/statsig/ContextTransformer.java
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.openfeature.contrib.providers.statsig; | ||
|
||
import com.statsig.sdk.StatsigUser; | ||
import dev.openfeature.sdk.EvaluationContext; | ||
import dev.openfeature.sdk.Structure; | ||
import dev.openfeature.sdk.Value; | ||
import dev.openfeature.sdk.exceptions.TargetingKeyMissingError; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Transformer from OpenFeature context to statsig User. | ||
*/ | ||
class ContextTransformer { | ||
public static final String CONTEXT_APP_VERSION = "appVersion"; | ||
public static final String CONTEXT_COUNTRY = "country"; | ||
public static final String CONTEXT_EMAIL = "email"; | ||
public static final String CONTEXT_IP = "ip"; | ||
public static final String CONTEXT_LOCALE = "locale"; | ||
public static final String CONTEXT_USER_AGENT = "userAgent"; | ||
public static final String CONTEXT_PRIVATE_ATTRIBUTES = "privateAttributes"; | ||
|
||
static StatsigUser transform(EvaluationContext ctx) { | ||
if (ctx.getTargetingKey() == null) { | ||
throw new TargetingKeyMissingError("targeting key is required."); | ||
} | ||
StatsigUser user = new StatsigUser(ctx.getTargetingKey()); | ||
Map<String, String> customMap = new HashMap<>(); | ||
ctx.asObjectMap().forEach((k, v) -> { | ||
switch (k) { | ||
case CONTEXT_APP_VERSION: | ||
user.setAppVersion(String.valueOf(v)); | ||
break; | ||
case CONTEXT_COUNTRY: | ||
user.setCountry(String.valueOf(v)); | ||
break; | ||
case CONTEXT_EMAIL: | ||
user.setEmail(String.valueOf(v)); | ||
break; | ||
case CONTEXT_IP: | ||
user.setIp(String.valueOf(v)); | ||
break; | ||
case CONTEXT_USER_AGENT: | ||
user.setUserAgent(String.valueOf(v)); | ||
break; | ||
case CONTEXT_LOCALE: | ||
user.setLocale(String.valueOf(v)); | ||
break; | ||
default: | ||
if (!CONTEXT_PRIVATE_ATTRIBUTES.equals(k)) { | ||
customMap.put(k, String.valueOf(v)); | ||
} | ||
break; | ||
} | ||
}); | ||
user.setCustomIDs(customMap); | ||
|
||
Map<String, String> privateMap = new HashMap<>(); | ||
Value privateAttributes = ctx.getValue(CONTEXT_PRIVATE_ATTRIBUTES); | ||
if (privateAttributes != null && privateAttributes.isStructure()) { | ||
Structure privateAttributesStructure = privateAttributes.asStructure(); | ||
privateAttributesStructure.asObjectMap().forEach((k, v) -> privateMap.put(k, String.valueOf(v))); | ||
user.setPrivateAttributes(privateMap); | ||
} | ||
return user; | ||
} | ||
|
||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.