1
1
package dev .openfeature .contrib .providers .statsig ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+ import java .util .concurrent .Future ;
6
+
7
+ import org .jetbrains .annotations .NotNull ;
8
+
3
9
import com .statsig .sdk .APIFeatureGate ;
4
10
import com .statsig .sdk .DynamicConfig ;
5
11
import com .statsig .sdk .EvaluationReason ;
6
12
import com .statsig .sdk .Layer ;
7
13
import com .statsig .sdk .Statsig ;
8
14
import com .statsig .sdk .StatsigUser ;
15
+
9
16
import dev .openfeature .sdk .EvaluationContext ;
10
17
import dev .openfeature .sdk .EventProvider ;
11
18
import dev .openfeature .sdk .Metadata ;
12
19
import dev .openfeature .sdk .MutableContext ;
13
20
import dev .openfeature .sdk .ProviderEvaluation ;
14
- import dev .openfeature .sdk .ProviderState ;
15
21
import dev .openfeature .sdk .Structure ;
16
22
import dev .openfeature .sdk .Value ;
17
- import dev .openfeature .sdk .exceptions .GeneralError ;
18
- import dev .openfeature .sdk .exceptions .ProviderNotReadyError ;
19
23
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
20
24
import lombok .AllArgsConstructor ;
21
25
import lombok .Getter ;
22
26
import lombok .SneakyThrows ;
23
27
import lombok .extern .slf4j .Slf4j ;
24
- import org .jetbrains .annotations .NotNull ;
25
-
26
- import java .util .ArrayList ;
27
- import java .util .List ;
28
- import java .util .concurrent .Future ;
29
- import java .util .concurrent .atomic .AtomicBoolean ;
30
28
31
29
/**
32
30
* Provider implementation for Statsig.
@@ -36,18 +34,9 @@ public class StatsigProvider extends EventProvider {
36
34
37
35
@ Getter
38
36
private static final String NAME = "Statsig" ;
39
-
40
- private static final String PROVIDER_NOT_YET_INITIALIZED = "provider not yet initialized" ;
41
- private static final String UNKNOWN_ERROR = "unknown error" ;
42
37
private static final String FEATURE_CONFIG_KEY = "feature_config" ;
43
-
44
38
private final StatsigProviderConfig statsigProviderConfig ;
45
39
46
- @ Getter
47
- private ProviderState state = ProviderState .NOT_READY ;
48
-
49
- private final AtomicBoolean isInitialized = new AtomicBoolean (false );
50
-
51
40
/**
52
41
* Constructor.
53
42
* @param statsigProviderConfig StatsigProvider Config
@@ -63,19 +52,12 @@ public StatsigProvider(StatsigProviderConfig statsigProviderConfig) {
63
52
*/
64
53
@ Override
65
54
public void initialize (EvaluationContext evaluationContext ) throws Exception {
66
- boolean initialized = isInitialized .getAndSet (true );
67
- if (initialized && ProviderState .READY .equals (state )) {
68
- log .debug ("already initialized" );
69
- return ;
70
- }
71
-
72
55
Future <Void > initFuture = Statsig .initializeAsync (statsigProviderConfig .getSdkKey (),
73
56
statsigProviderConfig .getOptions ());
74
57
initFuture .get ();
75
58
76
59
statsigProviderConfig .postInit ();
77
- state = ProviderState .READY ;
78
- log .info ("finished initializing provider, state: {}" , state );
60
+ log .info ("finished initializing provider" );
79
61
}
80
62
81
63
@ Override
@@ -87,7 +69,6 @@ public Metadata getMetadata() {
87
69
@ Override
88
70
@ SuppressFBWarnings (value = {"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE" }, justification = "reason can be null" )
89
71
public ProviderEvaluation <Boolean > getBooleanEvaluation (String key , Boolean defaultValue , EvaluationContext ctx ) {
90
- verifyEvaluation ();
91
72
StatsigUser user = ContextTransformer .transform (ctx );
92
73
Boolean evaluatedValue = defaultValue ;
93
74
Value featureConfigValue = ctx .getValue (FEATURE_CONFIG_KEY );
@@ -136,7 +117,6 @@ private boolean assumeFailure(APIFeatureGate featureGate) {
136
117
137
118
@ Override
138
119
public ProviderEvaluation <String > getStringEvaluation (String key , String defaultValue , EvaluationContext ctx ) {
139
- verifyEvaluation ();
140
120
StatsigUser user = ContextTransformer .transform (ctx );
141
121
FeatureConfig featureConfig = parseFeatureConfig (ctx );
142
122
String evaluatedValue = defaultValue ;
@@ -159,7 +139,6 @@ public ProviderEvaluation<String> getStringEvaluation(String key, String default
159
139
160
140
@ Override
161
141
public ProviderEvaluation <Integer > getIntegerEvaluation (String key , Integer defaultValue , EvaluationContext ctx ) {
162
- verifyEvaluation ();
163
142
StatsigUser user = ContextTransformer .transform (ctx );
164
143
FeatureConfig featureConfig = parseFeatureConfig (ctx );
165
144
Integer evaluatedValue = defaultValue ;
@@ -182,7 +161,6 @@ public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defa
182
161
183
162
@ Override
184
163
public ProviderEvaluation <Double > getDoubleEvaluation (String key , Double defaultValue , EvaluationContext ctx ) {
185
- verifyEvaluation ();
186
164
StatsigUser user = ContextTransformer .transform (ctx );
187
165
FeatureConfig featureConfig = parseFeatureConfig (ctx );
188
166
Double evaluatedValue = defaultValue ;
@@ -206,7 +184,6 @@ public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double default
206
184
@ SneakyThrows
207
185
@ Override
208
186
public ProviderEvaluation <Value > getObjectEvaluation (String key , Value defaultValue , EvaluationContext ctx ) {
209
- verifyEvaluation ();
210
187
StatsigUser user = ContextTransformer .transform (ctx );
211
188
FeatureConfig featureConfig = parseFeatureConfig (ctx );
212
189
Value evaluatedValue = defaultValue ;
@@ -293,27 +270,11 @@ private static FeatureConfig parseFeatureConfig(EvaluationContext ctx) {
293
270
return new FeatureConfig (type , name );
294
271
}
295
272
296
- private void verifyEvaluation () throws ProviderNotReadyError , GeneralError {
297
- if (!ProviderState .READY .equals (state )) {
298
-
299
- /*
300
- According to spec Requirement 2.4.5:
301
- "The provider SHOULD indicate an error if flag resolution is attempted before the provider is ready."
302
- https://github.com/open-feature/spec/blob/main/specification/sections/02-providers.md#requirement-245
303
- */
304
- if (ProviderState .NOT_READY .equals (state )) {
305
- throw new ProviderNotReadyError (PROVIDER_NOT_YET_INITIALIZED );
306
- }
307
- throw new GeneralError (UNKNOWN_ERROR );
308
- }
309
- }
310
-
311
273
@ SneakyThrows
312
274
@ Override
313
275
public void shutdown () {
314
276
log .info ("shutdown" );
315
277
Statsig .shutdown ();
316
- state = ProviderState .NOT_READY ;
317
278
}
318
279
319
280
/**
0 commit comments