Skip to content

spotify/confidence-sdk-java

Java Confidence SDK

Java library for Confidence. This library is designed to work in a java backend environment. For Android, please visit Confidence-SDK-Android.

Install

Maven

<dependency>
    <groupId>com.spotify.confidence</groupId>
    <artifactId>sdk-java</artifactId>
    <version>0.2.2</version>
</dependency>

Note: we strongly recommend to adopt the latest non-SNAPSHOT release available here.

Depending on a development snapshot

We deploy snapshots from the main branch to Sonatype OSSRH. To use a snapshot, add the following repository to your pom.xml:

<distributionManagement>
    <snapshotRepository>
        <id>central-portal-snapshots</id>
        <url>https://central.sonatype.com/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

Usage

The SDK is instantiated using a client secret that is configured in the Confidence UI or via the management console.

Resolving flags

Flag values are evaluated remotely and returned to the application:

final Confidence confidence = Confidence.builder("<CLIENT_TOKEN>").build();
confidence.setContext(Map.of("country", ConfidenceValue.of("SE")));
final String propertyValue =
    confidence
        .withContext(
            Map.of(
                "user_id", ConfidenceValue.of("<some-user-id>"),
                "country", ConfidenceValue.of("SE")))
        .getValue("flag-name.property-name", "defaultValue");

Tracking events

Events are emitted to the Confidence backend:

confidence.track("my-event", ConfidenceValue.of(Map.of("field", ConfidenceValue.of("data"))));

Testing with ConfidenceStub

For testing code that uses Confidence, we provide ConfidenceStub - a stub implementation that allows configuring predefined values and evaluation results for flags. It also tracks method calls and provides access to the call history for verification.

Basic usage with predefined values:

// Create a ConfidenceStub instance
ConfidenceStub stub = ConfidenceStub.createStub();

// Configure a predefined value for a flag
stub.configureValue("flag-name.property-name", "predefinedValue");

// Retrieve the value using the stub
String value = stub.getValue("flag-name.property-name", "defaultValue");
System.out.println("Retrieved value: " + value);

// Verify the call history
List<String> callHistory = stub.getCallHistory();
System.out.println("Call history: " + callHistory);

OpenFeature

The library includes a Provider for the OpenFeature Java SDK, that can be used to resolve feature flag values from the Confidence platform.

Creating an OpenFeature Provider

To create a ConfidenceFeatureProvider, use the new constructor that takes a Confidence.Builder:

import com.spotify.confidence.Confidence;
import com.spotify.confidence.ConfidenceFeatureProvider;
import dev.openfeature.sdk.OpenFeatureAPI;

// Create the provider using the new constructor with Builder pattern
ConfidenceFeatureProvider provider = new ConfidenceFeatureProvider(
    Confidence.builder("<CLIENT_SECRET>")
);

// For custom provider setup with resolve deadline and managed channel:
ConfidenceFeatureProvider provider = new ConfidenceFeatureProvider(
    Confidence.builder("<CLIENT_SECRET>")
        .resolveDeadlineMs(1000)  // 1 second timeout for flag resolution
        .flagResolverManagedChannel("uri.to.resolver.sidecar.com", <port>)
);

// Set the provider with OpenFeature
OpenFeatureAPI.getInstance().setProvider(provider);

Note: The new constructor automatically calls buildForProvider() internally, which optimizes the Confidence instance for use with OpenFeature. All previous constructors taking String clientSecret or Confidence instances directly are now deprecated.

To learn more about the basic concepts (flags, targeting key, evaluation contexts), the OpenFeature reference documentation can be useful.

Telemetry

In order to improve the services provided by Confidence, the SDK collects a very limited amount of telemetry data. This data is sent in the form of an additional gRPC header with each resolve request. The data does not contain any information that can be used to link the data to a specific end user.

Please refer to the Telemetry class to understand the data that is collected.

To opt out of this behavior, you can disable telemetry by setting the disableTelemetry flag to true when building the Confidence instance.

final Confidence confidence = Confidence.Builder("<CLIENT_SECRET>")
        .disableTelemetry(true)
        .build();

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 10

Languages