Skip to content

Commit

Permalink
feat: Resolve Debug to redirect to UI tool (#163)
Browse files Browse the repository at this point in the history
* feat: Resolve Debug to redirect to UI tool on web
  • Loading branch information
nickybondarenko authored Dec 4, 2024
1 parent b85663f commit 6aeb5bc
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ConfidenceFeatureProviderTest {
public void setup() {
fakeEngine = new FakeEventSenderEngine(new FakeClock());
fakeFlagResolverClient = new ResolverClientTestUtils.FakeFlagResolverClient();
root = Confidence.create(fakeEngine, fakeFlagResolverClient);
root = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void beforeEach() {
final FlagResolverClientImpl flagResolver =
new FlagResolverClientImpl(
new GrpcFlagResolver("fake-secret", channel, telemetryInterceptor), telemetry);
final Confidence confidence = Confidence.create(fakeEventSender, flagResolver);
final Confidence confidence = Confidence.create(fakeEventSender, flagResolver, "clientKey");
final FeatureProvider featureProvider = new ConfidenceFeatureProvider(confidence);

openFeatureAPI = OpenFeatureAPI.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void beforeEach() {
final FakeEventSenderEngine fakeEventSender = new FakeEventSenderEngine(new FakeClock());
this.fakeFlagResolver = new FakeFlagResolver();
final FlagResolverClientImpl flagResolver = new FlagResolverClientImpl(fakeFlagResolver);
this.confidence = Confidence.create(fakeEventSender, flagResolver);
this.confidence = Confidence.create(fakeEventSender, flagResolver, "clientKey");
final FeatureProvider featureProvider = new ConfidenceFeatureProvider(confidence);

final OpenFeatureAPI openFeatureAPI = OpenFeatureAPI.getInstance();
Expand Down
26 changes: 22 additions & 4 deletions sdk-java/src/main/java/com/spotify/confidence/Confidence.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.grpc.StatusRuntimeException;
import java.io.Closeable;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Map;
Expand Down Expand Up @@ -131,6 +133,15 @@ public <T> FlagEvaluation<T> getEvaluation(String key, T defaultValue) {
}

final ResolvedFlag resolvedFlag = response.getResolvedFlags(0);
final String clientKey = client().clientSecret;
final String flag = resolvedFlag.getFlag();
final String context = URLEncoder.encode(getContext().toString(), StandardCharsets.UTF_8);
final String logMessage =
String.format(
"See resolves for '%s' in Confidence: "
+ "https://app.confidence.spotify.com/flags/resolver-test?client-key=%s&flag=flags/%s&context=%s",
flag, clientKey, flag, context);
log.debug(logMessage);
if (!requestFlagName.equals(resolvedFlag.getFlag())) {
final String errorMessage =
String.format(
Expand Down Expand Up @@ -192,11 +203,14 @@ CompletableFuture<ResolveFlagsResponse> resolveFlags(String flagName) {

@VisibleForTesting
static Confidence create(
EventSenderEngine eventSenderEngine, FlagResolverClient flagResolverClient) {
EventSenderEngine eventSenderEngine,
FlagResolverClient flagResolverClient,
String clientSecret) {
final Closer closer = Closer.create();
closer.register(eventSenderEngine);
closer.register(flagResolverClient);
return new RootInstance(new ClientDelegate(closer, flagResolverClient, eventSenderEngine));
return new RootInstance(
new ClientDelegate(closer, flagResolverClient, eventSenderEngine, clientSecret));
}

public static Confidence.Builder builder(String clientSecret) {
Expand All @@ -207,14 +221,17 @@ private static class ClientDelegate implements FlagResolverClient, EventSenderEn
private final Closeable closeable;
private final FlagResolverClient flagResolverClient;
private final EventSenderEngine eventSenderEngine;
private String clientSecret;

private ClientDelegate(
Closeable closeable,
FlagResolverClient flagResolverClient,
EventSenderEngine eventSenderEngine) {
EventSenderEngine eventSenderEngine,
String clientSecret) {
this.closeable = closeable;
this.flagResolverClient = flagResolverClient;
this.eventSenderEngine = eventSenderEngine;
this.clientSecret = clientSecret;
}

@Override
Expand Down Expand Up @@ -355,7 +372,8 @@ public Confidence build() {
new EventSenderEngineImpl(clientSecret, DEFAULT_CHANNEL, Instant::now);
closer.register(flagResolverClient);
closer.register(eventSenderEngine);
return new RootInstance(new ClientDelegate(closer, flagResolverClient, eventSenderEngine));
return new RootInstance(
new ClientDelegate(closer, flagResolverClient, eventSenderEngine, clientSecret));
}

private void registerChannelForShutdown(ManagedChannel channel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ConfidenceContextTest {

@Test
public void testThrowInvalidContextInMessage() {
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient);
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
assertThrows(
Exceptions.InvalidContextInMessaageError.class,
() ->
Expand All @@ -24,7 +24,7 @@ public void testThrowInvalidContextInMessage() {

@Test
public void getContextContainsParentContextValues() {
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient);
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
root.updateContextEntry("page", ConfidenceValue.of("http://.."));
final EventSender confidence =
root.withContext(ImmutableMap.of("pants", ConfidenceValue.of("yellow")));
Expand All @@ -41,7 +41,7 @@ public void getContextContainsParentContextValues() {

@Test
public void setContextOverwritesContext() {
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient);
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
root.updateContextEntry("page", ConfidenceValue.of("http://.."));
final EventSender confidence =
root.withContext(ImmutableMap.of("pants", ConfidenceValue.of("yellow")));
Expand All @@ -65,7 +65,7 @@ public void setContextOverwritesContext() {

@Test
public void parentContextFieldCanBeOverridden() {
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient);
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
root.updateContextEntry("pants-color", ConfidenceValue.of("yellow"));
final EventSender confidence =
root.withContext(ImmutableMap.of("pants-color", ConfidenceValue.of("blue")));
Expand All @@ -83,7 +83,7 @@ public void parentContextFieldCanBeOverridden() {

@Test
public void parentContextFieldCanBeOverriddenOrRemoved() {
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient);
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
root.updateContextEntry("pants-color", ConfidenceValue.of("yellow"));
final EventSender confidence =
root.withContext(ImmutableMap.of("shirt-color", ConfidenceValue.of("blue")));
Expand All @@ -98,7 +98,7 @@ public void parentContextFieldCanBeOverriddenOrRemoved() {

@Test
public void multiLevelContexts() {
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient);
final Confidence root = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
final int numberOfLevels = 9;
Confidence last = root;
for (int i = 0; i < numberOfLevels; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void beforeEach() {
final FlagResolverClientImpl flagResolver =
new FlagResolverClientImpl(
new GrpcFlagResolver("fake-secret", channel, telemetryInterceptor), telemetry);
confidence = Confidence.create(fakeEventSender, flagResolver);
confidence = Confidence.create(fakeEventSender, flagResolver, "");
}

@AfterAll
Expand Down Expand Up @@ -478,7 +478,7 @@ public void resolveDoesNotContainHeaderWithTelemetryDataWhenDisabled() {
final FlagResolverClientImpl flagResolver =
new FlagResolverClientImpl(
new GrpcFlagResolver("fake-secret", channel, nullTelemetryInterceptor));
confidence = Confidence.create(fakeEventSender, flagResolver);
confidence = Confidence.create(fakeEventSender, flagResolver, "clientKey");

mockSampleResponse();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ConfidenceResourceManagementTest {
public void setup() {
fakeEngine = new FakeEventSenderEngine(new FakeClock());
fakeFlagResolverClient = new ResolverClientTestUtils.FakeFlagResolverClient();
root = Confidence.create(fakeEngine, fakeFlagResolverClient);
root = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class ConfidenceTest {

@BeforeEach
void beforeEach() {
confidence = Confidence.create(fakeEngine, fakeFlagResolverClient);
confidence = Confidence.create(fakeEngine, fakeFlagResolverClient, "clientKey");
}

@Test
Expand Down Expand Up @@ -244,7 +244,8 @@ void flagWithErroneousSchema() {

@Test
void internalError() {
final Confidence confidence = Confidence.create(fakeEngine, new FailingFlagResolverClient());
final Confidence confidence =
Confidence.create(fakeEngine, new FailingFlagResolverClient(), "clientKey");
final Integer value = confidence.getValue("no-match-flag", 20);
assertEquals(20, value);

Expand Down

0 comments on commit 6aeb5bc

Please sign in to comment.