Skip to content

Commit 49d47b8

Browse files
feat: flagd support resolver type from env vars (#792)
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent f7333ec commit 49d47b8

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

providers/flagd/README.md

+15-14
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,21 @@ variables.
7777

7878
Given below are the supported configurations:
7979

80-
| Option name | Environment variable name | Type & Values | Default | Compatible resolver |
81-
|-----------------------|--------------------------------|------------------------|-----------|---------------------|
82-
| host | FLAGD_HOST | String | localhost | rpc & in-process |
83-
| port | FLAGD_PORT | int | 8013 | rpc & in-process |
84-
| tls | FLAGD_TLS | boolean | false | rpc & in-process |
85-
| socketPath | FLAGD_SOCKET_PATH | String | null | rpc & in-process |
86-
| certPath | FLAGD_SERVER_CERT_PATH | String | null | rpc & in-process |
87-
| deadline | FLAGD_DEADLINE_MS | int | 500 | rpc & in-process |
88-
| selector | FLAGD_SOURCE_SELECTOR | String | null | in-process |
89-
| cache | FLAGD_CACHE | String - lru, disabled | lru | rpc |
90-
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | rpc |
91-
| maxEventStreamRetries | FLAGD_MAX_EVENT_STREAM_RETRIES | int | 5 | rpc |
92-
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | int | 1000 | rpc |
93-
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | String | null | in-process |
80+
| Option name | Environment variable name | Type & Values | Default | Compatible resolver |
81+
|-----------------------|--------------------------------|--------------------------|-----------|---------------------|
82+
| resolver | FLAGD_RESOLVER | String - rpc, in-process | rpc | |
83+
| host | FLAGD_HOST | String | localhost | rpc & in-process |
84+
| port | FLAGD_PORT | int | 8013 | rpc & in-process |
85+
| tls | FLAGD_TLS | boolean | false | rpc & in-process |
86+
| socketPath | FLAGD_SOCKET_PATH | String | null | rpc & in-process |
87+
| certPath | FLAGD_SERVER_CERT_PATH | String | null | rpc & in-process |
88+
| deadline | FLAGD_DEADLINE_MS | int | 500 | rpc & in-process |
89+
| selector | FLAGD_SOURCE_SELECTOR | String | null | in-process |
90+
| cache | FLAGD_CACHE | String - lru, disabled | lru | rpc |
91+
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | rpc |
92+
| maxEventStreamRetries | FLAGD_MAX_EVENT_STREAM_RETRIES | int | 5 | rpc |
93+
| retryBackoffMs | FLAGD_RETRY_BACKOFF_MS | int | 1000 | rpc |
94+
| offlineFlagSourcePath | FLAGD_OFFLINE_FLAG_SOURCE_PATH | String | null | in-process |
9495

9596
> [!NOTE]
9697
> Some configurations are only applicable for RPC resolver.

providers/flagd/schemas

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/Config.java

+22
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package dev.openfeature.contrib.providers.flagd;
22

33
import dev.openfeature.contrib.providers.flagd.resolver.grpc.cache.CacheType;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
import java.util.function.Function;
47

58
/**
69
* Helper class to hold configuration default values.
710
*/
11+
@Slf4j
812
public final class Config {
913
static final Evaluator DEFAULT_RESOLVER_TYPE = Evaluator.RPC;
1014
static final String DEFAULT_PORT = "8013";
@@ -14,6 +18,7 @@ public final class Config {
1418
static final int DEFAULT_DEADLINE = 500;
1519
static final int DEFAULT_MAX_CACHE_SIZE = 1000;
1620

21+
static final String RESOLVER_ENV_VAR = "FLAGD_RESOLVER";
1722
static final String HOST_ENV_VAR_NAME = "FLAGD_HOST";
1823
static final String PORT_ENV_VAR_NAME = "FLAGD_PORT";
1924
static final String TLS_ENV_VAR_NAME = "FLAGD_TLS";
@@ -55,6 +60,23 @@ static int fallBackToEnvOrDefault(String key, int defaultValue) {
5560
}
5661
}
5762

63+
static Evaluator fromValueProvider(Function<String, String> provider) {
64+
final String resolverVar = provider.apply(RESOLVER_ENV_VAR);
65+
if (resolverVar == null) {
66+
return DEFAULT_RESOLVER_TYPE;
67+
}
68+
69+
switch (resolverVar.toLowerCase()) {
70+
case "in-process":
71+
return Evaluator.IN_PROCESS;
72+
case "rpc":
73+
return Evaluator.RPC;
74+
default:
75+
log.warn("Unsupported resolver variable: {}", resolverVar);
76+
return DEFAULT_RESOLVER_TYPE;
77+
}
78+
}
79+
5880
/**
5981
* flagd evaluator type.
6082
*/

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdOptions.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_MAX_CACHE_SIZE;
1616
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_MAX_EVENT_STREAM_RETRIES;
1717
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_PORT;
18-
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_RESOLVER_TYPE;
1918
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_TLS;
2019
import static dev.openfeature.contrib.providers.flagd.Config.HOST_ENV_VAR_NAME;
2120
import static dev.openfeature.contrib.providers.flagd.Config.MAX_CACHE_SIZE_ENV_VAR_NAME;
@@ -27,6 +26,7 @@
2726
import static dev.openfeature.contrib.providers.flagd.Config.SOURCE_SELECTOR_ENV_VAR_NAME;
2827
import static dev.openfeature.contrib.providers.flagd.Config.TLS_ENV_VAR_NAME;
2928
import static dev.openfeature.contrib.providers.flagd.Config.fallBackToEnvOrDefault;
29+
import static dev.openfeature.contrib.providers.flagd.Config.fromValueProvider;
3030

3131
/**
3232
* FlagdOptions is a builder to build flagd provider options.
@@ -40,7 +40,7 @@ public class FlagdOptions {
4040
* flagd resolving type.
4141
*/
4242
@Builder.Default
43-
private Config.Evaluator resolverType = DEFAULT_RESOLVER_TYPE;
43+
private Config.Evaluator resolverType = fromValueProvider(System::getenv);
4444

4545
/**
4646
* flagd connection host.

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/FlagdOptionsTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.junit.jupiter.api.Test;
55
import org.mockito.Mockito;
66

7+
import java.util.function.Function;
8+
79
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_CACHE;
810
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_HOST;
911
import static dev.openfeature.contrib.providers.flagd.Config.DEFAULT_MAX_CACHE_SIZE;
@@ -31,6 +33,7 @@ public void TestDefaults() {
3133
assertNull(builder.getSelector());
3234
assertNull(builder.getOpenTelemetry());
3335
assertNull(builder.getOfflineFlagSourcePath());
36+
assertEquals(Config.Evaluator.RPC, builder.getResolverType());
3437
}
3538

3639
@Test
@@ -48,6 +51,7 @@ public void TestBuilderOptions() {
4851
.selector("app=weatherApp")
4952
.offlineFlagSourcePath("some-path")
5053
.openTelemetry(openTelemetry)
54+
.resolverType(Config.Evaluator.IN_PROCESS)
5155
.build();
5256

5357
assertEquals("https://hosted-flagd", flagdOptions.getHost());
@@ -60,5 +64,32 @@ public void TestBuilderOptions() {
6064
assertEquals("app=weatherApp", flagdOptions.getSelector());
6165
assertEquals("some-path", flagdOptions.getOfflineFlagSourcePath());
6266
assertEquals(openTelemetry, flagdOptions.getOpenTelemetry());
67+
assertEquals(Config.Evaluator.IN_PROCESS, flagdOptions.getResolverType());
6368
}
69+
70+
71+
@Test
72+
public void testValueProviderForEdgeCase_valid() {
73+
Function<String, String> valueProvider = s -> "in-process";
74+
assertEquals(Config.Evaluator.IN_PROCESS, Config.fromValueProvider(valueProvider));
75+
76+
valueProvider = s -> "IN-PROCESS";
77+
assertEquals(Config.Evaluator.IN_PROCESS, Config.fromValueProvider(valueProvider));
78+
79+
valueProvider = s -> "rpc";
80+
assertEquals(Config.Evaluator.RPC, Config.fromValueProvider(valueProvider));
81+
82+
valueProvider = s -> "RPC";
83+
assertEquals(Config.Evaluator.RPC, Config.fromValueProvider(valueProvider));
84+
}
85+
86+
@Test
87+
public void testValueProviderForEdgeCase_invalid() {
88+
Function<String, String> dummy = s -> "some-other";
89+
assertEquals(Config.DEFAULT_RESOLVER_TYPE, Config.fromValueProvider(dummy));
90+
91+
dummy = s -> null;
92+
assertEquals(Config.DEFAULT_RESOLVER_TYPE, Config.fromValueProvider(dummy));
93+
}
94+
6495
}

0 commit comments

Comments
 (0)