Skip to content

Commit

Permalink
feat: Telemetry ID improvements (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria authored Jun 4, 2024
1 parent 42cec7f commit 8d22dc7
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 26 deletions.
11 changes: 5 additions & 6 deletions src/main/java/com/spotify/confidence/Confidence.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ protected Stream<Map.Entry<String, ConfidenceValue>> contextEntries() {

@Override
public ConfidenceValue.Struct getContext() {

return contextEntries()
.collect(
Collector.of(
Expand Down Expand Up @@ -121,7 +120,7 @@ public <T> FlagEvaluation<T> getEvaluation(String key, T defaultValue) {
try {
final FlagPath flagPath = getPath(key);
final String requestFlagName = "flags/" + flagPath.getFlag();
final ResolveFlagsResponse response = resolveFlags(requestFlagName).get();
final ResolveFlagsResponse response = resolveFlags(requestFlagName, false).get();
if (response.getResolvedFlagsList().isEmpty()) {
final String errorMessage =
String.format("No active flag '%s' was found", flagPath.getFlag());
Expand Down Expand Up @@ -177,8 +176,8 @@ public <T> FlagEvaluation<T> getEvaluation(String key, T defaultValue) {
}
}

CompletableFuture<ResolveFlagsResponse> resolveFlags(String flagName) {
return client().resolveFlags(flagName, getContext());
CompletableFuture<ResolveFlagsResponse> resolveFlags(String flagName, Boolean isProvider) {
return client().resolveFlags(flagName, getContext(), isProvider);
}

@VisibleForTesting
Expand Down Expand Up @@ -221,8 +220,8 @@ public void flush() {

@Override
public CompletableFuture<ResolveFlagsResponse> resolveFlags(
String flag, ConfidenceValue.Struct context) {
return flagResolverClient.resolveFlags(flag, context);
String flag, ConfidenceValue.Struct context, Boolean isProvider) {
return flagResolverClient.resolveFlags(flag, context, isProvider);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public ProviderEvaluation<Value> getObjectEvaluation(
Map.of(
OPEN_FEATURE_RESOLVE_CONTEXT_KEY,
ConfidenceValue.Struct.fromProto(evaluationContext)))
.resolveFlags(requestFlagName)
.resolveFlags(requestFlagName, true)
.get();

if (resolveFlagResponse.getResolvedFlagsList().isEmpty()) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/spotify/confidence/FlagResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
interface FlagResolver {
void close();

public CompletableFuture<ResolveFlagsResponse> resolve(String flag, Struct context);
public CompletableFuture<ResolveFlagsResponse> resolve(
String flag, Struct context, Boolean isProvider);
}
3 changes: 2 additions & 1 deletion src/main/java/com/spotify/confidence/FlagResolverClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
import java.util.concurrent.CompletableFuture;

interface FlagResolverClient extends Closeable {
CompletableFuture<ResolveFlagsResponse> resolveFlags(String flag, ConfidenceValue.Struct context);
CompletableFuture<ResolveFlagsResponse> resolveFlags(
String flag, ConfidenceValue.Struct context, Boolean isProvider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public FlagResolverClientImpl(FlagResolver grpcFlagResolver) {
}

public CompletableFuture<ResolveFlagsResponse> resolveFlags(
String flagName, ConfidenceValue.Struct context) {
String flagName, ConfidenceValue.Struct context, Boolean isProvider) {
final Struct.Builder evaluationContextBuilder = context.toProto().getStructValue().toBuilder();
if (context.asMap().containsKey(OPEN_FEATURE_RESOLVE_CONTEXT_KEY)) {
final Value openFeatureEvaluationContext =
Expand All @@ -26,7 +26,7 @@ public CompletableFuture<ResolveFlagsResponse> resolveFlags(
evaluationContextBuilder.removeFields(OPEN_FEATURE_RESOLVE_CONTEXT_KEY);
}

return this.grpcFlagResolver.resolve(flagName, evaluationContextBuilder.build());
return this.grpcFlagResolver.resolve(flagName, evaluationContextBuilder.build(), isProvider);
}

@Override
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/com/spotify/confidence/GrpcFlagResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Strings;
import com.google.protobuf.Struct;
import com.spotify.confidence.shaded.flags.resolver.v1.*;
import com.spotify.confidence.shaded.flags.resolver.v1.Sdk.Builder;
import io.grpc.ManagedChannel;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand All @@ -11,7 +12,7 @@
public class GrpcFlagResolver implements FlagResolver {
private final ManagedChannel managedChannel;
private final String clientSecret;
private final Sdk sdk;
private final Builder sdkBuilder = Sdk.newBuilder().setVersion(ConfidenceUtils.getSdkVersion());

private final FlagResolverServiceGrpc.FlagResolverServiceFutureStub stub;

Expand All @@ -20,24 +21,26 @@ public GrpcFlagResolver(String clientSecret, ManagedChannel managedChannel) {
throw new IllegalArgumentException("clientSecret must be a non-empty string.");
}
this.clientSecret = clientSecret;
this.sdk =
Sdk.newBuilder()
.setId(SdkId.SDK_ID_JAVA_PROVIDER)
.setVersion(ConfidenceUtils.getSdkVersion())
.build();
this.managedChannel = managedChannel;
this.stub = FlagResolverServiceGrpc.newFutureStub(managedChannel);
}

public CompletableFuture<ResolveFlagsResponse> resolve(String flag, Struct context) {
public CompletableFuture<ResolveFlagsResponse> resolve(
String flag, Struct context, Boolean isProvider) {
return GrpcUtil.toCompletableFuture(
stub.withDeadlineAfter(10, TimeUnit.SECONDS)
.resolveFlags(
ResolveFlagsRequest.newBuilder()
.setClientSecret(this.clientSecret)
.addAllFlags(List.of(flag))
.setEvaluationContext(context)
.setSdk(sdk)
.setSdk(
sdkBuilder
.setId(
isProvider
? SdkId.SDK_ID_JAVA_PROVIDER
: SdkId.SDK_ID_JAVA_CONFIDENCE)
.build())
.setApply(true)
.build()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void setup() {
public void testCloseChildShouldThrowFromResolveFlags() throws IOException {
final Confidence child = root.withContext(Map.of("child-key", ConfidenceValue.of("child")));
child.close();
assertThrows(IllegalStateException.class, () -> child.resolveFlags("test").get());
assertThrows(IllegalStateException.class, () -> child.resolveFlags("test", false).get());
}

@Test
Expand All @@ -50,16 +50,16 @@ public void testCloseChildShouldNotAffectParent()
throws IOException, ExecutionException, InterruptedException {
final Confidence child = root.withContext(Map.of("child-key", ConfidenceValue.of("child")));
child.close();
root.resolveFlags("test").get();
root.resolveFlags("test", false).get();
root.track("test", ConfidenceValue.of(Map.of("messageKey", ConfidenceValue.of("parent"))));
}

@Test
public void testCloseParentShouldAffectChild() throws IOException {
final Confidence child = root.withContext(Map.of("child-key", ConfidenceValue.of("child")));
root.close();
assertThrows(IllegalStateException.class, () -> child.resolveFlags("test").get());
assertThrows(IllegalStateException.class, () -> root.resolveFlags("test").get());
assertThrows(IllegalStateException.class, () -> child.resolveFlags("test", false).get());
assertThrows(IllegalStateException.class, () -> root.resolveFlags("test", false).get());
assertTrue(fakeEngine.closed);
assertTrue(fakeFlagResolverClient.closed);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/spotify/confidence/ConfidenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ void internalError() {
public static class FailingFlagResolverClient implements FlagResolverClient {

@Override
public CompletableFuture<ResolveFlagsResponse> resolveFlags(String flag, Struct context) {
public CompletableFuture<ResolveFlagsResponse> resolveFlags(
String flag, Struct context, Boolean isProvider) {
throw new RuntimeException("Crashing while performing network call");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class FakeFlagResolver implements FlagResolver {
public void close() {}

@Override
public CompletableFuture<ResolveFlagsResponse> resolve(String flag, Struct context) {
public CompletableFuture<ResolveFlagsResponse> resolve(
String flag, Struct context, Boolean isProvider) {
this.context = context;
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class FakeFlagResolverClient implements FlagResolverClient {

@Override
public CompletableFuture<ResolveFlagsResponse> resolveFlags(
String flag, ConfidenceValue.Struct context) {
String flag, ConfidenceValue.Struct context, Boolean isProvider) {
resolves.put(flag, context);
return CompletableFuture.completedFuture(response);
}
Expand Down

0 comments on commit 8d22dc7

Please sign in to comment.