Skip to content

Commit aeb46ee

Browse files
devin-ai-integration[bot]weihao-statsiggraphite-app[bot]
authored
Add Kotlin examples to Java Core documentation (#3062)
* Add Kotlin examples to Java Core documentation Co-Authored-By: [email protected] <[email protected]> * Update Kotlin example to call shutdown().get() Co-Authored-By: [email protected] <[email protected]> * Update proxyAuth format to username:password in Java and Kotlin examples Co-Authored-By: [email protected] <[email protected]> * Update docs/server-core/java/_getFeatureGate.mdx Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com> * Update docs/server-core/java/_shutdown.mdx Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com> * Update docs/server-core/java/_getFeatureGate.mdx Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]> Co-authored-by: Weihao Ding <[email protected]> Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
1 parent 73117b1 commit aeb46ee

12 files changed

+422
-9
lines changed

docs/server-core/java/_checkGate.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
4+
<Tabs
5+
groupId="java-core-lang-choice"
6+
defaultValue="java"
7+
values={[
8+
{label: 'Java', value: 'java'},
9+
{label: 'Kotlin', value: 'kotlin'},
10+
]}>
11+
<TabItem value="java">
12+
113
```java
214
boolean isFeatureOn = statsig.checkGate(user, "example_gate");
315
```
16+
17+
</TabItem>
18+
<TabItem value="kotlin">
19+
20+
```kotlin
21+
val isFeatureOn = statsig.checkGate(user, "example_gate")
22+
```
23+
24+
</TabItem>
25+
</Tabs>

docs/server-core/java/_example.mdx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,16 @@ Create a `pom.xml` file with:
8787
</TabItem>
8888
</Tabs>
8989

90-
### Create Java Application
90+
### Create Application
91+
92+
<Tabs
93+
groupId="java-core-lang-choice"
94+
defaultValue="java"
95+
values={[
96+
{label: 'Java', value: 'java'},
97+
{label: 'Kotlin', value: 'kotlin'},
98+
]}>
99+
<TabItem value="java">
91100

92101
Create a file named `ExampleApp.java`:
93102

@@ -117,6 +126,38 @@ public class ExampleApp {
117126
}
118127
```
119128

129+
</TabItem>
130+
<TabItem value="kotlin">
131+
132+
Create a file named `ExampleApp.kt`:
133+
134+
```kotlin
135+
import com.statsig.*
136+
137+
fun main() {
138+
// Initialize Statsig
139+
val options = StatsigOptions.Builder().build()
140+
val statsig = Statsig("YOUR_SERVER_SECRET_KEY", options)
141+
statsig.initialize().get()
142+
143+
try {
144+
// Check a feature gate
145+
val isEnabled = statsig.checkGate("my_feature_gate", "user123")
146+
println("Feature gate is ${if (isEnabled) "enabled" else "disabled"}")
147+
148+
// Get a config
149+
val config = statsig.getConfig("my_config", "user123")
150+
println("Config value: ${config.getValue("some_parameter", "default_value")}")
151+
} finally {
152+
// Always shutdown Statsig when done
153+
statsig.shutdown().get()
154+
}
155+
}
156+
```
157+
158+
</TabItem>
159+
</Tabs>
160+
120161
Replace `YOUR_SERVER_SECRET_KEY` with your actual server secret key from the [Statsig Console](https://console.statsig.com/api_keys).
121162

122163
### Run the Application

docs/server-core/java/_flush.mdx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
4+
<Tabs
5+
groupId="java-core-lang-choice"
6+
defaultValue="java"
7+
values={[
8+
{label: 'Java', value: 'java'},
9+
{label: 'Kotlin', value: 'kotlin'},
10+
]}>
11+
<TabItem value="java">
12+
113
```java
214
// Method signature
315
public CompletableFuture<Void> flushEvents()
@@ -9,4 +21,23 @@ try {
921
} catch (InterruptedException | ExecutionException e) {
1022
System.err.println("Failed to flush events: " + e.getMessage());
1123
}
12-
```
24+
```
25+
26+
</TabItem>
27+
<TabItem value="kotlin">
28+
29+
```kotlin
30+
// Method signature
31+
fun flushEvents(): CompletableFuture<Void>
32+
33+
// example usage
34+
try {
35+
statsig.flushEvents().get()
36+
println("All events have been successfully flushed.")
37+
} catch (e: Exception) {
38+
System.err.println("Failed to flush events: ${e.message}")
39+
}
40+
```
41+
42+
</TabItem>
43+
</Tabs>

docs/server-core/java/_getConfig.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ While both `Experiment` and `DynamicConfig` classes share similar functionalitie
66

77
### Config/Experiment Usage
88

9+
import Tabs from "@theme/Tabs";
10+
import TabItem from "@theme/TabItem";
11+
12+
<Tabs
13+
groupId="java-core-lang-choice"
14+
defaultValue="java"
15+
values={[
16+
{label: 'Java', value: 'java'},
17+
{label: 'Kotlin', value: 'kotlin'},
18+
]}>
19+
<TabItem value="java">
20+
921
```java
1022
DynamicConfig config = statsig.getDynamicConfig(user, "awesome_product_details");
1123
```
24+
25+
</TabItem>
26+
<TabItem value="kotlin">
27+
28+
```kotlin
29+
val config = statsig.getDynamicConfig(user, "awesome_product_details")
30+
```
31+
32+
</TabItem>
33+
</Tabs>
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
4+
<Tabs
5+
groupId="java-core-lang-choice"
6+
defaultValue="java"
7+
values={[
8+
{label: 'Java', value: 'java'},
9+
{label: 'Kotlin', value: 'kotlin'},
10+
]}>
11+
<TabItem value="java">
12+
113
```java
214
Layer layer = statsig.getLayer(user, "layer_name");
315
```
416

517
```java
6-
Experiment experiment = statsig.getExperiment(user, "sample_exp")
18+
Experiment experiment = statsig.getExperiment(user, "sample_exp");
19+
```
20+
21+
</TabItem>
22+
<TabItem value="kotlin">
23+
24+
```kotlin
25+
val layer = statsig.getLayer(user, "layer_name")
26+
```
27+
28+
```kotlin
29+
val experiment = statsig.getExperiment(user, "sample_exp")
730
```
831

32+
</TabItem>
33+
</Tabs>
34+
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
4+
<Tabs
5+
groupId="java-core-lang-choice"
6+
defaultValue="java"
7+
values={[
8+
{label: 'Java', value: 'java'},
9+
{label: 'Kotlin', value: 'kotlin'},
10+
]}>
11+
<TabItem value="java">
12+
113
```java
2-
FeatureGate gate = statsig.getFeatureGate(user, "exmaple_gate");
3-
```
14+
FeatureGate gate = statsig.getFeatureGate(user, "example_gate");
15+
```
16+
17+
</TabItem>
18+
<TabItem value="kotlin">
19+
20+
```kotlin
21+
val gate = statsig.getFeatureGate(user, "example_gate")
22+
```
23+
24+
</TabItem>
25+
</Tabs>

docs/server-core/java/_initialize.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
import Tabs from "@theme/Tabs";
2+
import TabItem from "@theme/TabItem";
3+
14

25
### **Initialize the SDK**
36

47
After installation, you will need to initialize the SDK with a customized configuration using a [**Server Secret Key from the Statsig console**](https://console.statsig.com/api_keys).
58

9+
<Tabs
10+
groupId="java-core-lang-choice"
11+
defaultValue="java"
12+
values={[
13+
{label: 'Java', value: 'java'},
14+
{label: 'Kotlin', value: 'kotlin'},
15+
]}>
16+
<TabItem value="java">
17+
618
```java
719
import com.statsig.*;
820

@@ -17,4 +29,24 @@ Statsig myStasigServer = new Statsig(SECRET_KEY, options);
1729
myStasigServer.initialize().get();
1830
```
1931

32+
</TabItem>
33+
<TabItem value="kotlin">
34+
35+
```kotlin
36+
import com.statsig.*
37+
38+
// All StatsigOptions are optional, feel free to adjust them as needed
39+
val options = StatsigOptions.Builder()
40+
.setSpecsSyncIntervalMs(10000)
41+
.setEventLoggingFlushIntervalMs(10000)
42+
.setOutputLoggerLevel(OutputLogger.LogLevel.INFO)
43+
.build()
44+
45+
val myStatsigServer = Statsig(SECRET_KEY, options)
46+
myStatsigServer.initialize().get()
47+
```
48+
49+
</TabItem>
50+
</Tabs>
51+
2052
You can also initialize the Statsig client using the method <code>initialize_with_details</code>. This method initializes the client and returns a <code>StatsigInitializeDetails</code> struct, which includes metadata such as the success status, initialization source, and any failure details. The key difference between <code>initialize_with_details</code> and <code>initialize</code> is that this method provides additional debugging information. If initialization fails, the <code>init_success</code> field will be <code>false</code> and the <code>failure_details</code> field may be populated.

docs/server-core/java/_logEvent.mdx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11

2+
import Tabs from "@theme/Tabs";
3+
import TabItem from "@theme/TabItem";
4+
5+
<Tabs
6+
groupId="java-core-lang-choice"
7+
defaultValue="java"
8+
values={[
9+
{label: 'Java', value: 'java'},
10+
{label: 'Kotlin', value: 'kotlin'},
11+
]}>
12+
<TabItem value="java">
13+
214
```java
3-
StatsigUser user = new StatsigUser("user123");
15+
StatsigUser user = new StatsigUser.Builder().setUserID("user123").build();
416
String eventName = "purchase";
517
String value = "100";
618
Map<String, String> metadata = new HashMap<>();
719
metadata.put("item_id", "12345");
820
metadata.put("category", "electronics");
921

1022
statsig.logEvent(user, eventName, value, metadata);
11-
```
23+
```
24+
25+
</TabItem>
26+
<TabItem value="kotlin">
27+
28+
```kotlin
29+
val user = StatsigUser.Builder().setUserID("user123").build()
30+
val eventName = "purchase"
31+
val value = "100"
32+
val metadata = mutableMapOf(
33+
"item_id" to "12345",
34+
"category" to "electronics"
35+
)
36+
37+
statsig.logEvent(user, eventName, value, metadata)
38+
```
39+
40+
</TabItem>
41+
</Tabs>

docs/server-core/java/_options.mdx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ When `true`, disables all network functions: event & exposure logging, spec down
7979

8080
### Example Usage
8181

82+
import Tabs from "@theme/Tabs";
83+
import TabItem from "@theme/TabItem";
84+
85+
<Tabs
86+
groupId="java-core-lang-choice"
87+
defaultValue="java"
88+
values={[
89+
{label: 'Java', value: 'java'},
90+
{label: 'Kotlin', value: 'kotlin'},
91+
]}>
92+
<TabItem value="java">
93+
8294
```java
8395
import com.statsig.StatsigOptions;
8496
import com.statsig.ProxyConfig;
@@ -95,11 +107,43 @@ proxyConfig.setProxyHost("proxy.example.com");
95107
proxyConfig.setProxyPort(8080);
96108
proxyConfig.setProxyProtocol("https");
97109
// Optional: Set proxy authentication
98-
// proxyConfig.setProxyAuth("Basic base64encodedcredentials");
110+
// proxyConfig.setProxyAuth("username:password");
99111

100112
optionsBuilder.setProxyConfig(proxyConfig);
101113
StatsigOptions options = optionsBuilder.build();
102114

103115
// Pass the options object into statsig.initialize()
104116
Statsig.initialize("secret-key", options);
105117
```
118+
119+
</TabItem>
120+
<TabItem value="kotlin">
121+
122+
```kotlin
123+
import com.statsig.StatsigOptions
124+
import com.statsig.ProxyConfig
125+
126+
// Initialize StatsigOptions with custom parameters
127+
val optionsBuilder = StatsigOptions.Builder()
128+
.setEnvironment("development")
129+
.setInitTimeoutMs(3000)
130+
.setDisableAllLogging(false)
131+
132+
// Configure proxy settings if needed
133+
val proxyConfig = ProxyConfig().apply {
134+
proxyHost = "proxy.example.com"
135+
proxyPort = 8080
136+
proxyProtocol = "https"
137+
// Optional: Set proxy authentication
138+
// proxyAuth = "username:password"
139+
}
140+
141+
optionsBuilder.setProxyConfig(proxyConfig)
142+
val options = optionsBuilder.build()
143+
144+
// Pass the options object into statsig.initialize()
145+
Statsig("secret-key", options)
146+
```
147+
148+
</TabItem>
149+
</Tabs>

0 commit comments

Comments
 (0)