diff --git a/dd-openfeature/README.md b/dd-openfeature/README.md index 715e0252729..60c735c7cb1 100644 --- a/dd-openfeature/README.md +++ b/dd-openfeature/README.md @@ -70,6 +70,32 @@ Settings are read from the Datadog Java agent configuration when attached, then | Span enrichment | No | Yes | | `ExposureHook` accessors `getFlagKey()`, `getVariant()`, `getAllocationKey()`, `getTargetingKey()` | The next release after 1.68.1 | The next release after 1.68.1 | +## Flag selection + +To load only part of the flag configuration, give the provider a flag selection. It then loads the flags that have any of the tags, or whose key matches any of the key patterns: + +```java +import com.datadog.openfeature.FlagSelection; +import com.datadog.openfeature.Provider; +import dev.openfeature.sdk.OpenFeatureAPI; +import java.util.List; + +OpenFeatureAPI.getInstance().setProviderAndWait(new Provider(new Provider.Options() + .flagSelection(new FlagSelection() + .tags(List.of("checkout")) + .keyPatterns(List.of("checkout.*"))))); +``` + +* Tags match exactly and case-sensitively. A key pattern matches the whole key, case-sensitively: `*` matches any run of characters, and every other character is literal. +* Evaluating a flag outside the selection returns the default value with `FLAG_NOT_FOUND`. A selection that matches no flag loads an empty configuration. +* Leading and trailing Unicode white space is trimmed from every value, and empty and duplicate values are dropped. A null list is treated as absent. +* Each list accepts at most 31 values, and each tag and key pattern is at most 200 UTF-8 bytes. A selection over a limit, one holding a null or non-`String` value or an unpaired UTF-16 surrogate, or one that would make the request URL longer than 8192 bytes, is not sent. The provider logs an error, for example `flagSelection was not sent (too_many_values: each list accepts at most 31 values). Loading the full flag configuration.`, and loads all the flags. +* The server can also ignore a selection. It then returns all the flags, and the provider logs `flagSelection was ignored by the server (). Loaded the full flag configuration.` once per reason. A custom configuration endpoint must forward the `selected_flag_tags` and `selected_flag_key_patterns` query parameters, and the `DD-Flag-Selection-Ignored` response header. +* The selection is set in code only, and the provider captures it at construction. +* The providers of a class loader share one flag configuration. The first provider initialized decides the selection, including a provider without one, which loads all the flags. A later provider with a different selection uses the first selection, until the last provider shuts down; the first such provider logs a warning, once per class loader. +* The `remote_config` source does not support a selection: the provider logs a warning and loads all the flags. +* With `dd-java-agent`, dropped and ignored selections are also counted in the agent health metrics. + ## Exposures in your own data store The provider sends exposures to Datadog. To also record exposures in your own data store, register `ExposureHook` from `dd-openfeature`. It receives every completed evaluation, with the same exposure decision that the provider uses for Datadog. diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java index da9f400e14b..fe32150a5b5 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/DDEvaluator.java @@ -3,6 +3,7 @@ import com.datadog.openfeature.internal.FeatureFlagsRuntime; import com.datadog.openfeature.internal.bridge.v1.AgentBridge; import com.datadog.openfeature.internal.config.Settings; +import com.datadog.openfeature.internal.delivery.FlagSelectionRequest; import com.datadog.openfeature.internal.evaluation.ConfigurationStore; import com.datadog.openfeature.internal.evaluation.FlagEvaluator; import com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache; @@ -79,6 +80,7 @@ class DDEvaluator implements Evaluator, Consumer { private final Runnable configCallback; private final AgentBridge bridge; private final Settings settings; + private final FlagSelectionRequest flagSelection; private final FlagEvaluator core; private final ConfigurationStore configuration = new ConfigurationStore(); private volatile FeatureFlagsRuntime runtime; @@ -92,9 +94,19 @@ class DDEvaluator implements Evaluator, Consumer { final AgentBridge bridge, final Settings settings, final ExperimentationOptOutMode experimentationOptOutMode) { + this(configCallback, bridge, settings, experimentationOptOutMode, FlagSelectionRequest.NONE); + } + + DDEvaluator( + final Runnable configCallback, + final AgentBridge bridge, + final Settings settings, + final ExperimentationOptOutMode experimentationOptOutMode, + final FlagSelectionRequest flagSelection) { this.configCallback = configCallback; this.bridge = bridge; this.settings = settings; + this.flagSelection = flagSelection; // When span enrichment is off, the __dd_* span-enrichment metadata is attached only to // evaluations of allocations logging exposures, for the exposure hook. this.core = @@ -110,7 +122,7 @@ public boolean initialize( if (this.runtime != null) { return hasConfiguration(); } - acquired = FeatureFlagsRuntime.acquire(this.bridge, this.settings); + acquired = FeatureFlagsRuntime.acquire(this.bridge, this.settings, this.flagSelection); this.runtime = acquired; acquired.addConfigurationListener(this); } diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/FlagSelection.java b/dd-openfeature/src/main/java/com/datadog/openfeature/FlagSelection.java new file mode 100644 index 00000000000..53b6b352f9f --- /dev/null +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/FlagSelection.java @@ -0,0 +1,41 @@ +package com.datadog.openfeature; + +import java.util.List; + +/** + * The flags a provider loads: the flags that have any of the tags, or whose key matches any of the + * key patterns. A selection with neither loads all the flags. + * + *

Set it with {@link Provider.Options#flagSelection(FlagSelection)}. + */ +public final class FlagSelection { + List tags; + List keyPatterns; + + /** Creates a selection with no tags and no key patterns. */ + public FlagSelection() {} + + /** + * Sets the tags. Tags match exactly and case-sensitively, and a flag matches when it has any of + * them. + * + * @param tags the tags, or {@code null} for none. + * @return this selection. + */ + public FlagSelection tags(final List tags) { + this.tags = tags; + return this; + } + + /** + * Sets the key patterns. A pattern matches the whole flag key, case-sensitively: {@code *} + * matches any run of characters, and every other character is literal. + * + * @param keyPatterns the key patterns, or {@code null} for none. + * @return this selection. + */ + public FlagSelection keyPatterns(final List keyPatterns) { + this.keyPatterns = keyPatterns; + return this; + } +} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/Provider.java b/dd-openfeature/src/main/java/com/datadog/openfeature/Provider.java index ae3f66d8ab0..a7c789f49e5 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/Provider.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/Provider.java @@ -9,6 +9,7 @@ import com.datadog.openfeature.internal.bridge.v1.AgentBridge; import com.datadog.openfeature.internal.bridge.v1.AgentBridges; import com.datadog.openfeature.internal.config.Settings; +import com.datadog.openfeature.internal.delivery.FlagSelectionRequest; import com.datadog.openfeature.internal.evaluation.ExperimentationOptOut; import com.datadog.openfeature.internal.flagevaluation.FlagEvaluationWriter; import dev.openfeature.sdk.ErrorCode; @@ -44,6 +45,7 @@ public class Provider extends EventProvider implements Metadata { private final AgentBridge bridge; private final Settings settings; private final ExperimentationOptOutMode experimentationOptOutMode; + private final FlagSelectionRequest flagSelection; private final AtomicReference initializationState = new AtomicReference<>(InitializationState.NOT_STARTED); private final FlagEvalMetrics flagEvalMetrics; @@ -88,6 +90,11 @@ public Provider(final Options options) { this.settings.isEnabled() ? ExperimentationOptOut.resolve(options.experimentationOptOutMode, this.settings) : ExperimentationOptOutMode.DISABLED; + final FlagSelection selection = options.flagSelection; + this.flagSelection = + selection == null + ? FlagSelectionRequest.NONE + : FlagSelectionRequest.of(selection.tags, selection.keyPatterns); FlagEvalMetrics metrics = null; FlagEvalMetricsHook hook = null; try { @@ -309,7 +316,11 @@ private Evaluator buildEvaluator() { return evaluator; } return new DDEvaluator( - this::onConfigurationChange, this.bridge, this.settings, this.experimentationOptOutMode); + this::onConfigurationChange, + this.bridge, + this.settings, + this.experimentationOptOutMode, + this.flagSelection); } private FlagEvaluationWriter flagEvaluationPipeline() { @@ -405,6 +416,7 @@ public static class Options { private TimeUnit unit = SECONDS; private Boolean evaluationTelemetryOptOutParameterEnabled; private ExperimentationOptOutMode experimentationOptOutMode = ExperimentationOptOutMode.DEFAULT; + private FlagSelection flagSelection; /** * Sets per-subject experimentation opt-out. {@link ExperimentationOptOutMode#DEFAULT} reads @@ -463,6 +475,34 @@ private boolean isEvaluationTelemetryOptOutParameterEnabled(final Settings setti return settings.isEvaluationTelemetryPerSubjectOptOutEnabled(); } + /** + * Loads only the selected flags: the flags that have any of the selection's tags, or whose key + * matches any of its key patterns. Evaluating a flag outside the selection returns the default + * value with {@code FLAG_NOT_FOUND}. Without a selection, or with an empty one, all the flags + * are loaded. + * + *

Leading and trailing Unicode white space is trimmed from every value, empty values are + * dropped, and duplicates are removed. A null list is treated as absent. Each list accepts at + * most 31 values, and each tag and key pattern is at most 200 UTF-8 bytes after trimming. A + * selection over a limit, holding a null or non-String value or an unpaired UTF-16 surrogate, + * or making the request URL longer than 8192 bytes once percent-encoded, is not sent: the + * provider logs an error and loads all the flags. The server can also ignore a selection, and + * then returns all the flags. + * + *

The provider captures the selection at construction. The providers of a class loader share + * one flag configuration: the first provider initialized decides the selection until the last + * one shuts down, and a later provider with a different selection uses the first one. The first + * such provider logs a warning, once per class loader. The {@code remote_config} configuration + * source does not support a selection and loads all the flags. + * + * @param selection the flag selection, or {@code null} for all the flags. + * @return these options. + */ + public Options flagSelection(final FlagSelection selection) { + this.flagSelection = selection; + return this; + } + /** * Sets how long provider initialization waits for the initial flag configuration. * diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/FeatureFlagsRuntime.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/FeatureFlagsRuntime.java index 96b5235ad6c..79b149f1b5a 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/FeatureFlagsRuntime.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/FeatureFlagsRuntime.java @@ -10,6 +10,7 @@ import com.datadog.openfeature.internal.delivery.CdnConfigurationSource; import com.datadog.openfeature.internal.delivery.DirectIntakeTransport; import com.datadog.openfeature.internal.delivery.FallbackTransport; +import com.datadog.openfeature.internal.delivery.FlagSelectionRequest; import com.datadog.openfeature.internal.delivery.RemoteConfigurationService; import com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache; import com.datadog.openfeature.internal.exposure.ExposureEvent; @@ -32,7 +33,8 @@ * The Feature Flags runtime: configuration delivery and event pipelines. A single runtime is shared * by all the providers of a class loader, and stopped when the last one releases it, or when the * application shuts down: by the Datadog Java agent shutdown when attached, or by a JVM shutdown - * hook otherwise. + * hook otherwise. The flag selection of the provider that starts the runtime applies to all of + * them. */ public final class FeatureFlagsRuntime { private static final Logger LOGGER = LoggerFactory.getLogger(FeatureFlagsRuntime.class); @@ -42,6 +44,9 @@ public final class FeatureFlagsRuntime { private static int references; private final ConfigurationService source; + private final RuntimeServices services; + private final FlagSelectionRequest claimedSelection; + private final FlagSelectionRequest sentSelection; private final ExposureDeduplicationCache exposureCache = new ExposureDeduplicationCache(); @Nullable private final ExposurePipeline exposures; @Nullable private final FlagEvaluationPipeline evaluations; @@ -50,31 +55,52 @@ public final class FeatureFlagsRuntime { private final AtomicBoolean stopped = new AtomicBoolean(); @Nullable private volatile AutoCloseable shutdownRegistration; + /** + * @param claimedSelection the selection of the provider starting the runtime. + * @param sentSelection what the configuration source does with it: sends it, or reports why not. + */ FeatureFlagsRuntime( final ConfigurationService source, + final RuntimeServices services, + final FlagSelectionRequest claimedSelection, + final FlagSelectionRequest sentSelection, @Nullable final ExposurePipeline exposures, @Nullable final FlagEvaluationPipeline evaluations) { this.source = source; + this.services = services; + this.claimedSelection = claimedSelection; + this.sentSelection = sentSelection; this.exposures = exposures; this.evaluations = evaluations; } + /** Acquires the shared runtime for a provider without a flag selection. */ + public static FeatureFlagsRuntime acquire(final AgentBridge bridge, final Settings settings) { + return acquire(bridge, settings, FlagSelectionRequest.NONE); + } + /** * Acquires the shared runtime, starting it on first acquisition. Each acquisition must be paired * with a {@link #release()}. * * @param bridge the bridge to the Datadog Java agent. * @param settings the SDK settings. + * @param selection the flag selection of the acquiring provider. The first acquisition, which + * starts the runtime, claims its selection until the last release. * @return the shared runtime. * @throws IllegalStateException if Feature Flags is disabled or misconfigured. */ - public static FeatureFlagsRuntime acquire(final AgentBridge bridge, final Settings settings) { + public static FeatureFlagsRuntime acquire( + final AgentBridge bridge, final Settings settings, final FlagSelectionRequest selection) { synchronized (SHARED_LOCK) { if (shared == null) { - final FeatureFlagsRuntime created = create(bridge, settings); + final FeatureFlagsRuntime created = create(bridge, settings, selection); + created.reportSelection(selection); created.start(); created.registerShutdown(bridge); shared = created; + } else { + shared.reportSelection(selection); } references++; return shared; @@ -95,6 +121,11 @@ public static void release() { } static FeatureFlagsRuntime create(final AgentBridge bridge, final Settings settings) { + return create(bridge, settings, FlagSelectionRequest.NONE); + } + + static FeatureFlagsRuntime create( + final AgentBridge bridge, final Settings settings, final FlagSelectionRequest selection) { final String sourceName = settings.configurationSource(); if (sourceName == null) { throw new IllegalStateException("Feature Flags is disabled"); @@ -109,8 +140,14 @@ static FeatureFlagsRuntime create(final AgentBridge bridge, final Settings setti LOGGER.debug("Feature Flags runtime starting: configurationSource={}", sourceName); final RuntimeServices services = new RuntimeServices(bridge.healthMetrics()); final ConfigurationService source; + final FlagSelectionRequest sentSelection; final EventTransport transport; if (remoteConfig) { + // Before the connector check, so that the warning is logged when the provider falls back to + // the caller defaults too. + if (!selection.isNone()) { + FlagSelectionRequest.warnRemoteConfiguration(); + } final ConfigurationSource remoteConfiguration; try { remoteConfiguration = bridge.remoteConfiguration(); @@ -121,10 +158,13 @@ static FeatureFlagsRuntime create(final AgentBridge bridge, final Settings setti throw new RemoteConfigurationUnavailableException(null); } source = new RemoteConfigurationService(remoteConfiguration); + sentSelection = FlagSelectionRequest.NONE; // Remote Configuration never falls back to direct intake. transport = bridge.eventProxy(); } else { - source = new CdnConfigurationSource(settings, services); + final CdnConfigurationSource cdn = new CdnConfigurationSource(settings, services, selection); + source = cdn; + sentSelection = cdn.selection(); final EventTransport proxy = bridge.eventProxy(); final EventTransport direct = DirectIntakeTransport.create(settings); transport = @@ -133,7 +173,7 @@ static FeatureFlagsRuntime create(final AgentBridge bridge, final Settings setti if (transport == null) { LOGGER.warn( "Feature Flags event delivery is disabled: set DD_API_KEY or attach the Datadog Java agent"); - return new FeatureFlagsRuntime(source, null, null); + return new FeatureFlagsRuntime(source, services, selection, sentSelection, null, null); } final Map context = eventContext(settings); final ExposurePipeline exposures = @@ -161,7 +201,31 @@ static FeatureFlagsRuntime create(final AgentBridge bridge, final Settings setti transport.getClass().getSimpleName(), exposures != null ? "enabled" : "disabled", evaluations != null ? "enabled" : "disabled"); - return new FeatureFlagsRuntime(source, exposures, evaluations); + return new FeatureFlagsRuntime( + source, services, selection, sentSelection, exposures, evaluations); + } + + /** + * Reports what happens to the selection of an acquiring provider: an equal selection is sent, or + * not, as the claimed one is. A different one is not used: it reports its own drop, and warns + * only if it would load other flags, since a dropped selection and no selection both load every + * flag. Remote Configuration uses no selection at all, so only its own warning applies. + */ + private void reportSelection(final FlagSelectionRequest selection) { + if (this.source instanceof RemoteConfigurationService) { + if (!selection.isNone()) { + FlagSelectionRequest.warnRemoteConfiguration(); + } + return; + } + if (selection.equals(this.claimedSelection)) { + this.sentSelection.reportNotSent(this.services); + return; + } + selection.reportNotSent(this.services); + if (!selection.selectedFlags().equals(this.sentSelection.selectedFlags())) { + FlagSelectionRequest.warnSecondSelection(); + } } /** Signals an unavailable RC connector before any runtime resources have been started. */ diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSource.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSource.java index 93930a3d6fd..e5c79b0cb59 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSource.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSource.java @@ -4,6 +4,7 @@ import com.datadog.openfeature.internal.ConfigurationService; import com.datadog.openfeature.internal.RuntimeServices; +import com.datadog.openfeature.internal.bridge.v1.HealthMetrics; import com.datadog.openfeature.internal.config.Settings; import com.datadog.openfeature.internal.ufc.ServerConfiguration; import com.datadog.openfeature.internal.ufc.UniversalFlagConfigParser; @@ -44,6 +45,8 @@ public final class CdnConfigurationSource implements ConfigurationService { private static final long WARNING_INTERVAL_NANOS = TimeUnit.MINUTES.toNanos(5); private final URI endpoint; + private final FlagSelectionRequest selection; + private final boolean selectionSent; @Nullable private final String apiKey; private final long pollIntervalMillis; private final Duration requestTimeout; @@ -51,6 +54,7 @@ public final class CdnConfigurationSource implements ConfigurationService { private final ScheduledExecutorService executor; private final Retries.Sleeper retrySleeper; private final DoubleSupplier jitter; + private final RuntimeServices services; private final Object lifecycleLock = new Object(); private final AtomicBoolean polling = new AtomicBoolean(); private final CountDownLatch closedSignal = new CountDownLatch(1); @@ -62,9 +66,13 @@ public final class CdnConfigurationSource implements ConfigurationService { private volatile String etag; private long lastWarningNanos; - public CdnConfigurationSource(final Settings settings, final RuntimeServices services) { + public CdnConfigurationSource( + final Settings settings, + final RuntimeServices services, + final FlagSelectionRequest selection) { this( endpoint(settings), + selection, isManagedEndpoint(settings) ? settings.apiKey() : null, TimeUnit.SECONDS.toMillis(settings.agentlessPollIntervalSeconds()), Duration.ofSeconds(settings.agentlessRequestTimeoutSeconds()), @@ -75,7 +83,8 @@ public CdnConfigurationSource(final Settings settings, final RuntimeServices ser Executors.newSingleThreadScheduledExecutor( task -> services.newThread("configuration", task)), null, - () -> ThreadLocalRandom.current().nextDouble(1 - RETRY_JITTER, 1 + RETRY_JITTER)); + () -> ThreadLocalRandom.current().nextDouble(1 - RETRY_JITTER, 1 + RETRY_JITTER), + services); } CdnConfigurationSource( @@ -87,7 +96,35 @@ public CdnConfigurationSource(final Settings settings, final RuntimeServices ser final ScheduledExecutorService executor, @Nullable final Retries.Sleeper retrySleeper, final DoubleSupplier jitter) { - this.endpoint = endpoint; + this( + endpoint, + FlagSelectionRequest.NONE, + apiKey, + pollIntervalMillis, + requestTimeout, + httpClient, + executor, + retrySleeper, + jitter, + new RuntimeServices(HealthMetrics.NOOP)); + } + + CdnConfigurationSource( + final URI endpoint, + final FlagSelectionRequest selection, + @Nullable final String apiKey, + final long pollIntervalMillis, + final Duration requestTimeout, + final HttpClient httpClient, + final ScheduledExecutorService executor, + @Nullable final Retries.Sleeper retrySleeper, + final DoubleSupplier jitter, + final RuntimeServices services) { + // The endpoint is fixed for the life of the source, so the URL check holds for every request. + final URI selected = selection.appendTo(endpoint); + this.endpoint = selected == null ? endpoint : selected; + this.selection = selected == null ? FlagSelectionRequest.urlTooLong() : selection; + this.selectionSent = selected != null && selection.hasValues(); this.apiKey = apiKey; this.pollIntervalMillis = pollIntervalMillis; this.requestTimeout = requestTimeout; @@ -98,6 +135,15 @@ public CdnConfigurationSource(final Settings settings, final RuntimeServices ser ? retrySleeper : delay -> this.closedSignal.await(delay, TimeUnit.MILLISECONDS); this.jitter = jitter; + this.services = services; + } + + /** + * @return the selection that this source sends, {@link FlagSelectionRequest#NONE}, or the reason + * it is not sent. + */ + public FlagSelectionRequest selection() { + return this.selection; } @Override @@ -238,6 +284,11 @@ private boolean apply(final HttpResponse response) { warn("Feature Flags configuration endpoint returned HTTP {}", status); return false; } + final String ignoredReason = + this.selectionSent + ? FlagSelectionRequest.ignoredReasonOfFields( + response.headers().allValues(FlagSelectionRequest.IGNORED_HEADER)) + : null; final ServerConfiguration configuration; try { configuration = UniversalFlagConfigParser.parseJsonApi(Gzip.decode(response)); @@ -257,6 +308,10 @@ private boolean apply(final HttpResponse response) { final String nextEtag = response.headers().firstValue("ETag").orElse(null); this.etag = nextEtag == null || nextEtag.trim().isEmpty() ? null : nextEtag; LOGGER.debug("Feature Flags agentless configuration applied: etag={}", this.etag); + // Reported last, so that "Loaded the full flag configuration" is already true. + if (ignoredReason != null) { + FlagSelectionRequest.reportIgnored(this.services, ignoredReason); + } return true; } diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/delivery/FlagSelectionRequest.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/delivery/FlagSelectionRequest.java new file mode 100644 index 00000000000..cf6ede25d6c --- /dev/null +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/delivery/FlagSelectionRequest.java @@ -0,0 +1,478 @@ +package com.datadog.openfeature.internal.delivery; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptyList; +import static java.util.Collections.unmodifiableList; + +import com.datadog.openfeature.internal.RuntimeServices; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The flag selection sent with the agentless configuration request, canonicalized and validated + * like every other Datadog SDK does, or the reason it is not sent. Its diagnostics are reported + * once per class loader, like the runtime shared by the providers. + */ +public final class FlagSelectionRequest { + private static final Logger LOGGER = LoggerFactory.getLogger(FlagSelectionRequest.class); + + // The endpoint limits, shared by every SDK: + // https://github.com/DataDog/ffe-system-test-data/blob/main/flag-selection/README.md + static final int MAX_VALUES = 31; + static final int MAX_VALUE_BYTES = 200; + // Fastly answers URLs over 8 KB with 414 URI Too Long: + // https://docs.fastly.com/products/network-services-resource-limits + static final int MAX_URL_BYTES = 8192; + + static final String IGNORED_HEADER = "DD-Flag-Selection-Ignored"; + static final String DROPPED_METRIC = "flag_selection.dropped"; + static final String IGNORED_METRIC = "flag_selection.ignored"; + + private static final String MALFORMED_SELECTION = "malformed_selection"; + private static final String TOO_MANY_VALUES = "too_many_values"; + private static final String INVALID_KEY_PATTERN = "invalid_key_pattern"; + private static final String INVALID_TAG = "invalid_tag"; + private static final String UNKNOWN_REASON = "unknown"; + private static final Set IGNORED_REASONS = + Set.of( + MALFORMED_SELECTION, + TOO_MANY_VALUES, + INVALID_KEY_PATTERN, + INVALID_TAG, + "tag_load_failed"); + + private static final String MALFORMED_LIMIT = + "flagSelection must be an object with optional tags and keyPatterns string lists"; + private static final String VALUES_LIMIT = "each list accepts at most " + MAX_VALUES + " values"; + private static final String KEY_PATTERN_LIMIT = + "each key pattern is at most " + MAX_VALUE_BYTES + " UTF-8 bytes"; + private static final String TAG_LIMIT = "each tag is at most " + MAX_VALUE_BYTES + " UTF-8 bytes"; + private static final String URL_LIMIT = "the request URL is at most " + MAX_URL_BYTES + " bytes"; + + private static final Set REPORTED = ConcurrentHashMap.newKeySet(); + private static final Object[] NO_VALUES = new Object[0]; + private static final char[] HEX = "0123456789ABCDEF".toCharArray(); + + /** No selection: all the flags are loaded. */ + public static final FlagSelectionRequest NONE = new FlagSelectionRequest(null, null); + + private static final FlagSelectionRequest URL_TOO_LONG = + new FlagSelectionRequest(TOO_MANY_VALUES, URL_LIMIT); + + private final List tags; + private final List keyPatterns; + // The UTF-8 encodings of the values, computed once for sorting and reused for the request URL. + private final List utf8Tags; + private final List utf8KeyPatterns; + @Nullable private final String dropReason; + @Nullable private final String dropLimit; + + private FlagSelectionRequest( + @Nullable final String dropReason, @Nullable final String dropLimit) { + this(emptyList(), emptyList(), dropReason, dropLimit); + } + + private FlagSelectionRequest( + final List> tags, + final List> keyPatterns, + @Nullable final String dropReason, + @Nullable final String dropLimit) { + this.tags = keys(tags); + this.keyPatterns = keys(keyPatterns); + this.utf8Tags = values(tags); + this.utf8KeyPatterns = values(keyPatterns); + this.dropReason = dropReason; + this.dropLimit = dropLimit; + } + + /** + * Canonicalizes and validates a selection. Never throws: an element that is not a string, or a + * string with an unpaired surrogate, is reported as {@code malformed_selection}. + * + * @param tags the tags, or {@code null} for none. + * @param keyPatterns the key patterns, or {@code null} for none. + * @return the canonical selection, {@link #NONE}, or the reason it is not sent. + */ + public static FlagSelectionRequest of( + @Nullable final List tags, @Nullable final List keyPatterns) { + final Object[] rawTags; + final Object[] rawKeyPatterns; + try { + rawTags = tags == null ? NO_VALUES : Objects.requireNonNull(tags.toArray()); + rawKeyPatterns = + keyPatterns == null ? NO_VALUES : Objects.requireNonNull(keyPatterns.toArray()); + } catch (final RuntimeException e) { + // For example a list that the application modifies concurrently, or a toArray() that breaks + // its contract by returning null. + return dropped(MALFORMED_SELECTION, MALFORMED_LIMIT); + } + if (!areWellFormedStrings(rawTags) || !areWellFormedStrings(rawKeyPatterns)) { + return dropped(MALFORMED_SELECTION, MALFORMED_LIMIT); + } + final List> canonicalTags = canonicalize(rawTags); + final List> canonicalKeyPatterns = canonicalize(rawKeyPatterns); + if (canonicalTags.size() > MAX_VALUES || canonicalKeyPatterns.size() > MAX_VALUES) { + return dropped(TOO_MANY_VALUES, VALUES_LIMIT); + } + if (anyOverLimit(canonicalKeyPatterns)) { + return dropped(INVALID_KEY_PATTERN, KEY_PATTERN_LIMIT); + } + if (anyOverLimit(canonicalTags)) { + return dropped(INVALID_TAG, TAG_LIMIT); + } + if (canonicalTags.isEmpty() && canonicalKeyPatterns.isEmpty()) { + return NONE; + } + return new FlagSelectionRequest(canonicalTags, canonicalKeyPatterns, null, null); + } + + /** + * @return the selection that is not sent because the request URL would be over the limit. + */ + static FlagSelectionRequest urlTooLong() { + return URL_TOO_LONG; + } + + private static FlagSelectionRequest dropped(final String reason, final String limit) { + return new FlagSelectionRequest(reason, limit); + } + + private static boolean areWellFormedStrings(final Object[] values) { + for (final Object value : values) { + if (!(value instanceof String) || hasUnpairedSurrogate((String) value)) { + return false; + } + } + return true; + } + + // String.getBytes(UTF_8) replaces an unpaired surrogate with '?', so check before encoding. + private static boolean hasUnpairedSurrogate(final String value) { + for (int i = 0; i < value.length(); i++) { + final char c = value.charAt(i); + if (Character.isHighSurrogate(c) + && i + 1 < value.length() + && Character.isLowSurrogate(value.charAt(i + 1))) { + i++; + } else if (Character.isSurrogate(c)) { + return true; + } + } + return false; + } + + private static List> canonicalize(final Object[] values) { + final Map unique = new LinkedHashMap<>(); + for (final Object value : values) { + final String trimmed = trimWhiteSpace((String) value); + if (!trimmed.isEmpty()) { + unique.computeIfAbsent(trimmed, v -> v.getBytes(UTF_8)); + } + } + final List> entries = new ArrayList<>(unique.entrySet()); + // UTF-8 byte order, which differs from String.compareTo outside the Basic Multilingual Plane. + entries.sort((left, right) -> Arrays.compareUnsigned(left.getValue(), right.getValue())); + return entries; + } + + private static List keys(final List> entries) { + final List keys = new ArrayList<>(entries.size()); + for (final Map.Entry entry : entries) { + keys.add(entry.getKey()); + } + return unmodifiableList(keys); + } + + private static List values(final List> entries) { + final List values = new ArrayList<>(entries.size()); + for (final Map.Entry entry : entries) { + values.add(entry.getValue()); + } + return unmodifiableList(values); + } + + private static String trimWhiteSpace(final String value) { + int start = 0; + int end = value.length(); + while (start < end && isWhiteSpace(value.charAt(start))) { + start++; + } + while (end > start && isWhiteSpace(value.charAt(end - 1))) { + end--; + } + return value.substring(start, end); + } + + // The Unicode White_Space property, which String.trim() and String.strip() both differ from: + // https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt + private static boolean isWhiteSpace(final char c) { + return (c >= '\t' && c <= '\r') + || c == ' ' + || c == '\u0085' + || c == '\u00A0' + || c == '\u1680' + || (c >= '\u2000' && c <= '\u200A') + || c == '\u2028' + || c == '\u2029' + || c == '\u202F' + || c == '\u205F' + || c == '\u3000'; + } + + private static boolean anyOverLimit(final List> entries) { + for (final Map.Entry entry : entries) { + if (entry.getValue().length > MAX_VALUE_BYTES) { + return true; + } + } + return false; + } + + /** + * @return whether there is no selection: nothing to send, and nothing dropped. + */ + public boolean isNone() { + return this.dropReason == null && !hasValues(); + } + + boolean hasValues() { + return !this.tags.isEmpty() || !this.keyPatterns.isEmpty(); + } + + /** + * @return this selection if it selects flags, or {@link #NONE} if every flag loads, because there + * is no selection or it is not sent. + */ + public FlagSelectionRequest selectedFlags() { + return hasValues() ? this : NONE; + } + + List tags() { + return this.tags; + } + + List keyPatterns() { + return this.keyPatterns; + } + + @Nullable + String dropReason() { + return this.dropReason; + } + + /** + * Appends the selection to the query of the endpoint, as repeated {@code selected_flag_tags} and + * {@code selected_flag_key_patterns} parameters, before any fragment. + * + * @param endpoint the configuration endpoint. + * @return the endpoint with the selection, the endpoint itself when there is nothing to send, or + * {@code null} when the request URL would be over {@link #MAX_URL_BYTES}. + */ + @Nullable + URI appendTo(final URI endpoint) { + if (!hasValues()) { + return endpoint; + } + final StringBuilder url = + new StringBuilder() + .append(endpoint.getScheme()) + .append("://") + .append(endpoint.getRawAuthority()) + .append(endpoint.getRawPath()) + .append('?'); + final String query = endpoint.getRawQuery(); + boolean first = query == null || query.isEmpty(); + if (!first) { + url.append(query); + } + for (final byte[] tag : this.utf8Tags) { + appendParameter(url, first, "selected_flag_tags", tag); + first = false; + } + for (final byte[] keyPattern : this.utf8KeyPatterns) { + appendParameter(url, first, "selected_flag_key_patterns", keyPattern); + first = false; + } + final String fragment = endpoint.getRawFragment(); + if (fragment != null) { + url.append('#').append(fragment); + } + final URI selected = URI.create(url.toString()); + // Every character is ASCII once percent-encoded, so the length is the byte count. + return selected.toASCIIString().length() > MAX_URL_BYTES ? null : selected; + } + + private static void appendParameter( + final StringBuilder url, final boolean first, final String name, final byte[] utf8Value) { + if (!first) { + url.append('&'); + } + url.append(name).append('='); + // URLEncoder is form encoding: it sends a space as '+' and keeps '*'. + for (final byte b : utf8Value) { + final int octet = b & 0xFF; + if (isUnreserved(octet)) { + url.append((char) octet); + } else { + url.append('%').append(HEX[octet >> 4]).append(HEX[octet & 0xF]); + } + } + } + + // RFC 3986 unreserved characters: https://www.rfc-editor.org/rfc/rfc3986#section-2.3 + private static boolean isUnreserved(final int octet) { + return (octet >= 'A' && octet <= 'Z') + || (octet >= 'a' && octet <= 'z') + || (octet >= '0' && octet <= '9') + || octet == '-' + || octet == '.' + || octet == '_' + || octet == '~'; + } + + /** + * Reports a selection that is not sent: logs it at each call, once per provider initialization, + * and counts it once per reason. + * + * @param services the runtime services counting the drop. + */ + public void reportNotSent(final RuntimeServices services) { + final String reason = this.dropReason; + if (reason == null) { + return; + } + if (REPORTED.add("dropped:" + reason)) { + count(services, DROPPED_METRIC, reason); + } + error( + "flagSelection was not sent ({}: {}). Loading the full flag configuration.", + reason, + this.dropLimit); + } + + /** + * Sanitizes every {@code DD-Flag-Selection-Ignored} response header field. Repeated fields are + * joined with ", ", as other SDKs read them, so they are never a known reason. + * + * @param values the header values, in order; empty if absent. + * @return the known reason, {@code unknown} for any other value, or {@code null} if absent or + * empty. + */ + @Nullable + static String ignoredReasonOfFields(final List values) { + return ignoredReason(values.isEmpty() ? null : String.join(", ", values)); + } + + /** + * Sanitizes the {@code DD-Flag-Selection-Ignored} response header, so that only a known reason + * reaches the log and the metric. + * + * @param header the header value, or {@code null} if absent. + * @return the known reason, {@code unknown} for any other value, or {@code null} if absent or + * empty. + */ + @Nullable + static String ignoredReason(@Nullable final String header) { + if (header == null || header.isEmpty()) { + return null; + } + // Every known reason is ASCII and under the 32-byte cap, so an exact match enforces the cap. + return IGNORED_REASONS.contains(header) ? header : UNKNOWN_REASON; + } + + /** + * Reports a selection that the server ignored, once per reason. + * + * @param services the runtime services counting it. + * @param reason the sanitized reason. + */ + static void reportIgnored(final RuntimeServices services, final String reason) { + if (REPORTED.add("ignored:" + reason)) { + count(services, IGNORED_METRIC, reason); + error( + "flagSelection was ignored by the server ({}). Loaded the full flag configuration.", + reason); + } + } + + /** Warns, once, that a provider's selection differs from the one already in use. */ + public static void warnSecondSelection() { + if (REPORTED.add("second_selection")) { + warn( + "flagSelection differs from the flag selection already in use in this process. Using" + + " the first selection."); + } + } + + /** Warns, once, that Remote Configuration delivery does not support a selection. */ + public static void warnRemoteConfiguration() { + if (REPORTED.add("remote_configuration")) { + warn( + "flagSelection is not supported when flags are delivered through Remote Config. Loading" + + " the full flag configuration."); + } + } + + static void resetReportedForTest() { + REPORTED.clear(); + } + + // A failing metrics or logging backend must not fail the provider. + private static void count( + final RuntimeServices services, final String metric, final String reason) { + try { + services.countMetric(metric, 1, reason); + } catch (final RuntimeException | LinkageError ignored) { + // Nothing more can be done. + } + } + + private static void error(final String message, final Object... arguments) { + try { + LOGGER.error(message, arguments); + } catch (final RuntimeException | LinkageError ignored) { + // Nothing more can be done. + } + } + + private static void warn(final String message) { + try { + LOGGER.warn(message); + } catch (final RuntimeException | LinkageError ignored) { + // Nothing more can be done. + } + } + + @Override + public boolean equals(final Object other) { + if (this == other) { + return true; + } + if (!(other instanceof FlagSelectionRequest)) { + return false; + } + final FlagSelectionRequest that = (FlagSelectionRequest) other; + return this.tags.equals(that.tags) + && this.keyPatterns.equals(that.keyPatterns) + && Objects.equals(this.dropReason, that.dropReason) + && Objects.equals(this.dropLimit, that.dropLimit); + } + + @Override + public int hashCode() { + int hash = this.tags.hashCode(); + hash = 31 * hash + this.keyPatterns.hashCode(); + hash = 31 * hash + Objects.hashCode(this.dropReason); + return 31 * hash + Objects.hashCode(this.dropLimit); + } +} diff --git a/dd-openfeature/src/main/java/datadog/trace/api/openfeature/Provider.java b/dd-openfeature/src/main/java/datadog/trace/api/openfeature/Provider.java index 14a739423ed..a55c92aecc8 100644 --- a/dd-openfeature/src/main/java/datadog/trace/api/openfeature/Provider.java +++ b/dd-openfeature/src/main/java/datadog/trace/api/openfeature/Provider.java @@ -52,6 +52,12 @@ public Options enableEvaluationTelemetryOptOutParameter(final boolean enabled) { return this; } + @Override + public Options flagSelection(final com.datadog.openfeature.FlagSelection selection) { + super.flagSelection(selection); + return this; + } + @Override public Options initTimeout(final long timeout, final TimeUnit unit) { super.initTimeout(timeout, unit); diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/EvaluationTelemetryOptOutTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/EvaluationTelemetryOptOutTest.java index ee9109c5a80..42609eebf87 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/EvaluationTelemetryOptOutTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/EvaluationTelemetryOptOutTest.java @@ -24,6 +24,7 @@ import com.datadog.openfeature.internal.bridge.v1.HealthMetrics; import com.datadog.openfeature.internal.bridge.v1.SpanEnricher; import com.datadog.openfeature.internal.config.TestSettings; +import com.datadog.openfeature.internal.delivery.FlagSelectionRequest; import com.datadog.openfeature.internal.exposure.ExposureDeduplicationCache; import com.datadog.openfeature.internal.exposure.ExposureEvent; import com.datadog.openfeature.internal.exposure.ExposurePipeline; @@ -721,9 +722,20 @@ private static FeatureFlagsRuntime capturingRuntime(final FlagEvaluationPipeline throws ReflectiveOperationException { final Constructor constructor = FeatureFlagsRuntime.class.getDeclaredConstructor( - ConfigurationService.class, ExposurePipeline.class, FlagEvaluationPipeline.class); + ConfigurationService.class, + RuntimeServices.class, + FlagSelectionRequest.class, + FlagSelectionRequest.class, + ExposurePipeline.class, + FlagEvaluationPipeline.class); constructor.setAccessible(true); - return constructor.newInstance(mock(ConfigurationService.class), null, evaluations); + return constructor.newInstance( + mock(ConfigurationService.class), + new RuntimeServices(HealthMetrics.NOOP), + FlagSelectionRequest.NONE, + FlagSelectionRequest.NONE, + null, + evaluations); } private static ServerConfiguration defaultConfiguration() { diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/FlagSelectionProviderTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/FlagSelectionProviderTest.java new file mode 100644 index 00000000000..bc2097132a0 --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/FlagSelectionProviderTest.java @@ -0,0 +1,163 @@ +package com.datadog.openfeature; + +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +import com.datadog.openfeature.internal.LocalHttpServer; +import com.datadog.openfeature.internal.delivery.FlagSelectionTestSupport; +import com.datadog.openfeature.testlog.CapturingServiceProvider; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.slf4j.event.Level; + +/** Checks the provider sends its flag selection on the agentless configuration request. */ +class FlagSelectionProviderTest { + private static final String CONFIG = + "{\"data\":{\"type\":\"universal-flag-configuration\",\"attributes\":" + + "{\"environment\":{\"name\":\"Test\"},\"flags\":{}}}}"; + + private LocalHttpServer cdn; + private final Map previousProperties = new HashMap<>(); + + @BeforeEach + void setUp() throws IOException { + this.cdn = new LocalHttpServer().otherwise(200, CONFIG); + setProperty("dd.feature.flags.enabled", "true"); + setProperty("dd.feature.flags.configuration.source", "agentless"); + setProperty( + "dd.feature.flags.configuration.source.agentless.base.url", + this.cdn.uri("/ufc").toString()); + setProperty("dd.api.key", ""); + FlagSelectionTestSupport.resetReported(); + CapturingServiceProvider.enable("com.datadog.openfeature", Level.WARN); + } + + @AfterEach + void tearDown() { + CapturingServiceProvider.reset(); + FlagSelectionTestSupport.resetReported(); + this.previousProperties.forEach((key, value) -> { + if (value == null) { + System.clearProperty(key); + } else { + System.setProperty(key, value); + } + }); + this.cdn.close(); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + @SuppressWarnings("deprecation") + void capturesTheSelectionAtConstruction(final boolean alias) throws Exception { + final List tags = new ArrayList<>(asList(" area:checkout", "mobile")); + final List keyPatterns = new ArrayList<>(singletonList("checkout.*")); + final Provider.Options options = + alias ? new datadog.trace.api.openfeature.Provider.Options() : new Provider.Options(); + options + .initTimeout(5, SECONDS) + .flagSelection(new FlagSelection().tags(tags).keyPatterns(keyPatterns)); + final Provider provider = + alias + ? new datadog.trace.api.openfeature.Provider( + (datadog.trace.api.openfeature.Provider.Options) options) + : new Provider(options); + tags.add("late"); + keyPatterns.clear(); + options.flagSelection(null); + + assertEquals( + "selected_flag_tags=area%3Acheckout&selected_flag_tags=mobile" + + "&selected_flag_key_patterns=checkout.%2A", + initializeAndGetQuery(provider)); + } + + @Test + void noSelectionLeavesTheRequestUnchanged() throws Exception { + final List> withoutValues = + asList( + options -> options, + options -> options.flagSelection(null), + options -> options.flagSelection(new FlagSelection()), + options -> options.flagSelection(new FlagSelection().tags(null).keyPatterns(null)), + options -> options.flagSelection(new FlagSelection().tags(asList(" ", "")))); + + for (final Function configure : withoutValues) { + final Provider provider = + new Provider(configure.apply(new Provider.Options().initTimeout(5, SECONDS))); + assertNull(initializeAndGetQuery(provider)); + } + assertEquals(withoutValues.size(), this.cdn.requests().size()); + assertEquals(0, messages().size(), messages().toString()); + } + + @Test + @SuppressWarnings({"unchecked", "rawtypes"}) + void malformedSelectionLoadsAllTheFlagsWithoutThrowing() throws Exception { + final List raw = new ArrayList(singletonList("area:checkout")); + raw.add(7); + final Provider provider = + assertDoesNotThrow(() -> + new Provider( + new Provider.Options() + .initTimeout(5, SECONDS) + .flagSelection(new FlagSelection().tags(raw)))); + + assertNull(initializeAndGetQuery(provider)); + assertEquals( + singletonList( + "ERROR flagSelection was not sent (malformed_selection: flagSelection must be an object" + + " with optional tags and keyPatterns string lists). Loading the full flag" + + " configuration."), + messages()); + } + + @Test + @SuppressWarnings("deprecation") + void aliasOptionsKeepTheirTypeWithAFlagSelection() { + final datadog.trace.api.openfeature.Provider.Options options = + new datadog.trace.api.openfeature.Provider.Options(); + + final datadog.trace.api.openfeature.Provider.Options returned = + options.flagSelection(new FlagSelection()); + + assertSame(options, returned); + } + + /** Initializes the provider, returns the raw query of its first request and shuts it down. */ + private String initializeAndGetQuery(final Provider provider) throws Exception { + final int before = this.cdn.requests().size(); + try { + provider.initialize(null); + } finally { + provider.shutdown(); + } + return this.cdn.requests().get(before).uri.getRawQuery(); + } + + private void setProperty(final String key, final String value) { + this.previousProperties.put(key, System.setProperty(key, value)); + } + + private static List messages() { + return CapturingServiceProvider.events().stream() + .filter(e -> e.message.startsWith("flagSelection")) + .map(e -> e.level + " " + e.message) + .collect(Collectors.toList()); + } +} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/FeatureFlagsRuntimeTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/FeatureFlagsRuntimeTest.java index 544b33782dd..028aea0a252 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/FeatureFlagsRuntimeTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/FeatureFlagsRuntimeTest.java @@ -1,12 +1,16 @@ package com.datadog.openfeature.internal; +import static java.util.Arrays.asList; import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonList; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -14,17 +18,25 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import com.datadog.openfeature.internal.FeatureFlagsRuntime.RemoteConfigurationUnavailableException; import com.datadog.openfeature.internal.bridge.v1.AgentBridge; import com.datadog.openfeature.internal.bridge.v1.ConfigurationSource; import com.datadog.openfeature.internal.bridge.v1.EventTransport; import com.datadog.openfeature.internal.bridge.v1.HealthMetrics; import com.datadog.openfeature.internal.bridge.v1.ShutdownSource; import com.datadog.openfeature.internal.bridge.v1.SpanEnricher; +import com.datadog.openfeature.internal.config.Settings; import com.datadog.openfeature.internal.config.TestSettings; +import com.datadog.openfeature.internal.delivery.FlagSelectionRequest; +import com.datadog.openfeature.internal.delivery.FlagSelectionTestSupport; +import com.datadog.openfeature.internal.delivery.FlagSelectionTestSupport.RecordingHealthMetrics; import com.datadog.openfeature.internal.flagevaluation.FlagEvalEvent; import com.datadog.openfeature.internal.ufc.ServerConfiguration; import com.datadog.openfeature.testlog.CapturingServiceProvider; +import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; @@ -580,6 +592,299 @@ void recordsExposuresOnlyWhenDeliveryIsEnabled() { null)); } + @Test + void firstSelectionWinsAndALaterDifferentOneWarnsOnce() throws Exception { + try (FlagSelectionScope scope = new FlagSelectionScope()) { + final FeatureFlagsRuntime first = scope.acquire(AREA_CHECKOUT); + assertSame(first, scope.acquire(FlagSelectionRequest.of(singletonList("area:search"), null))); + assertSame(first, scope.acquire(FlagSelectionRequest.NONE)); + assertSame(first, scope.acquire(FlagSelectionRequest.of(asList(" area:checkout"), null))); + + assertEquals(singletonList(SECOND_SELECTION_WARNING), scope.messages()); + assertEquals(1, scope.cdn.requests().size()); + assertEquals(AREA_CHECKOUT_QUERY, scope.cdn.requests().get(0).uri.getRawQuery()); + } + } + + @Test + void providerWithoutSelectionClaimsAllTheFlags() throws Exception { + try (FlagSelectionScope scope = new FlagSelectionScope()) { + final FeatureFlagsRuntime first = scope.acquire(FlagSelectionRequest.NONE); + assertSame(first, scope.acquire(FlagSelectionRequest.of(asList(" ", ""), null))); + assertSame(first, scope.acquire(AREA_CHECKOUT)); + + assertEquals(singletonList(SECOND_SELECTION_WARNING), scope.messages()); + assertNull(scope.cdn.requests().get(0).uri.getRawQuery()); + } + } + + @Test + void lastReleaseFreesTheClaim() throws Exception { + try (FlagSelectionScope scope = new FlagSelectionScope()) { + final FeatureFlagsRuntime first = scope.acquire(FlagSelectionRequest.NONE); + scope.releaseAll(); + final FeatureFlagsRuntime second = scope.acquire(AREA_CHECKOUT); + + assertNotSame(first, second, "the last release stops the runtime"); + assertTrue(scope.messages().isEmpty(), scope.messages().toString()); + assertNull(scope.cdn.requests().get(0).uri.getRawQuery()); + assertEquals(AREA_CHECKOUT_QUERY, scope.cdn.requests().get(1).uri.getRawQuery()); + } + } + + @Test + void droppedSelectionIsReportedAtEachProviderInitAndCountedOnce() throws Exception { + final FlagSelectionRequest tooMany = FlagSelectionRequest.of(values(32), null); + try (FlagSelectionScope scope = new FlagSelectionScope()) { + scope.acquire(tooMany); + scope.acquire(FlagSelectionRequest.of(values(33), null)); + // A dropped selection loads every flag, as no selection does, so they do not differ. + scope.acquire(FlagSelectionRequest.NONE); + + final String notSent = + "ERROR flagSelection was not sent (too_many_values: each list accepts at most 31" + + " values). Loading the full flag configuration."; + assertEquals(asList(notSent, notSent), scope.messages()); + assertEquals(singletonList("flag_selection.dropped|1|too_many_values"), scope.metrics.counts); + assertNull(scope.cdn.requests().get(0).uri.getRawQuery()); + } + } + + @Test + void overlongUrlIsReportedAsTooManyValues() throws Exception { + final char[] padding = new char[8150]; + Arrays.fill(padding, 'x'); + try (FlagSelectionScope scope = new FlagSelectionScope("/ufc?pad=" + new String(padding))) { + scope.acquire(AREA_CHECKOUT); + scope.acquire(AREA_CHECKOUT); + scope.acquire(FlagSelectionRequest.NONE); + + final String notSent = + "ERROR flagSelection was not sent (too_many_values: the request URL is at most 8192" + + " bytes). Loading the full flag configuration."; + assertEquals(asList(notSent, notSent), scope.messages()); + assertEquals(singletonList("flag_selection.dropped|1|too_many_values"), scope.metrics.counts); + assertEquals("pad=" + new String(padding), scope.cdn.requests().get(0).uri.getRawQuery()); + } + } + + @Test + void remoteConfigurationWarnsOnceWhenASelectionIsSet() { + FlagSelectionTestSupport.resetReported(); + CapturingServiceProvider.enable("com.datadog.openfeature", Level.WARN); + final RecordingHealthMetrics metrics = new RecordingHealthMetrics(); + final TestConfigurationSource source = new TestConfigurationSource(); + final AgentBridge bridge = new TestAgentBridge(source, null).withHealthMetrics(metrics); + final Settings remoteConfig = + TestSettings.of("feature.flags.configuration.source", "remote_config"); + try { + FeatureFlagsRuntime.acquire(bridge, remoteConfig, FlagSelectionRequest.NONE); + FeatureFlagsRuntime.acquire(bridge, remoteConfig, FlagSelectionRequest.of(asList(" "), null)); + FeatureFlagsRuntime.release(); + FeatureFlagsRuntime.release(); + assertTrue(flagSelectionMessages().isEmpty(), "no selection, no warning"); + + final FlagSelectionRequest dropped = FlagSelectionRequest.of(values(32), null); + FeatureFlagsRuntime.acquire(bridge, remoteConfig, dropped); + FeatureFlagsRuntime.acquire(bridge, remoteConfig, dropped); + FeatureFlagsRuntime.release(); + FeatureFlagsRuntime.release(); + assertEquals( + singletonList(REMOTE_CONFIG_WARNING), flagSelectionMessages(), "an invalid selection"); + FeatureFlagsRuntime.acquire(bridge, remoteConfig, AREA_CHECKOUT); + FeatureFlagsRuntime.release(); + + assertEquals(singletonList(REMOTE_CONFIG_WARNING), flagSelectionMessages()); + assertTrue(metrics.counts.isEmpty()); + } finally { + CapturingServiceProvider.reset(); + FlagSelectionTestSupport.resetReported(); + } + } + + @Test + void remoteConfigurationWarnsAJoiningProviderWithASelection() { + FlagSelectionTestSupport.resetReported(); + CapturingServiceProvider.enable("com.datadog.openfeature", Level.WARN); + final AgentBridge bridge = new TestAgentBridge(new TestConfigurationSource(), null); + final Settings legacyRemoteConfig = + TestSettings.of("experimental.flagging.provider.enabled", "true"); + try { + FeatureFlagsRuntime.acquire(bridge, legacyRemoteConfig, FlagSelectionRequest.NONE); + FeatureFlagsRuntime.acquire(bridge, legacyRemoteConfig, AREA_CHECKOUT); + FeatureFlagsRuntime.acquire(bridge, legacyRemoteConfig, AREA_CHECKOUT); + + assertEquals(singletonList(REMOTE_CONFIG_WARNING), flagSelectionMessages()); + } finally { + FeatureFlagsRuntime.release(); + FeatureFlagsRuntime.release(); + FeatureFlagsRuntime.release(); + CapturingServiceProvider.reset(); + FlagSelectionTestSupport.resetReported(); + } + } + + @Test + void remoteConfigurationWarningPrecedesTheConnectorCheck() { + FlagSelectionTestSupport.resetReported(); + CapturingServiceProvider.enable("com.datadog.openfeature", Level.WARN); + try { + assertThrows( + RemoteConfigurationUnavailableException.class, + () -> + FeatureFlagsRuntime.create( + AgentBridge.NONE, + TestSettings.of("feature.flags.configuration.source", "remote_config"), + AREA_CHECKOUT)); + + assertEquals(singletonList(REMOTE_CONFIG_WARNING), flagSelectionMessages()); + } finally { + CapturingServiceProvider.reset(); + FlagSelectionTestSupport.resetReported(); + } + } + + @Test + void failingSelectionDiagnosticsDoNotFailTheAcquisition() throws Exception { + try (FlagSelectionScope scope = new FlagSelectionScope()) { + CapturingServiceProvider.failOn("flagSelection"); + final FeatureFlagsRuntime first = + assertDoesNotThrow(() -> scope.acquire(FlagSelectionRequest.of(values(32), null))); + assertSame(first, assertDoesNotThrow(() -> scope.acquire(AREA_CHECKOUT))); + + assertEquals(2, scope.messages().size()); + } + } + + @Test + void aDroppedSelectionOfAJoiningProviderIsReportedAndDiffersFromASentOne() throws Exception { + try (FlagSelectionScope scope = new FlagSelectionScope()) { + scope.acquire(AREA_CHECKOUT); + scope.acquire(FlagSelectionRequest.of(values(32), null)); + + assertEquals( + asList( + "ERROR flagSelection was not sent (too_many_values: each list accepts at most 31" + + " values). Loading the full flag configuration.", + SECOND_SELECTION_WARNING), + scope.messages()); + assertEquals(singletonList("flag_selection.dropped|1|too_many_values"), scope.metrics.counts); + } + } + + @Test + void aMalformedSelectionOfAJoiningProviderIsReportedWithoutDifferingFromNoSelection() + throws Exception { + try (FlagSelectionScope scope = new FlagSelectionScope()) { + scope.acquire(FlagSelectionRequest.NONE); + scope.acquire(FlagSelectionRequest.of(asList("area:checkout", 42), null)); + + assertEquals( + singletonList( + "ERROR flagSelection was not sent (malformed_selection: flagSelection must be an" + + " object with optional tags and keyPatterns string lists). Loading the full flag" + + " configuration."), + scope.messages()); + assertEquals( + singletonList("flag_selection.dropped|1|malformed_selection"), scope.metrics.counts); + } + } + + @Test + void noSelectionOfAJoiningProviderDoesNotDifferFromADroppedOne() throws Exception { + try (FlagSelectionScope scope = new FlagSelectionScope()) { + scope.acquire(FlagSelectionRequest.of(values(32), null)); + scope.acquire(FlagSelectionRequest.NONE); + + assertEquals( + singletonList( + "ERROR flagSelection was not sent (too_many_values: each list accepts at most 31" + + " values). Loading the full flag configuration."), + scope.messages()); + } + } + + private static final FlagSelectionRequest AREA_CHECKOUT = + FlagSelectionRequest.of(singletonList("area:checkout"), null); + private static final String AREA_CHECKOUT_QUERY = "selected_flag_tags=area%3Acheckout"; + private static final String SECOND_SELECTION_WARNING = + "WARN flagSelection differs from the flag selection already in use in this process. Using" + + " the first selection."; + private static final String REMOTE_CONFIG_WARNING = + "WARN flagSelection is not supported when flags are delivered through Remote Config. Loading" + + " the full flag configuration."; + + private static List values(final int count) { + final List values = new ArrayList<>(); + for (int i = 0; i < count; i++) { + values.add("t" + i); + } + return values; + } + + private static List flagSelectionMessages() { + return CapturingServiceProvider.events().stream() + .filter(e -> e.message.startsWith("flagSelection")) + .map(e -> e.level + " " + e.message) + .collect(Collectors.toList()); + } + + /** Acquires agentless runtimes polling a local CDN, and releases them all when closed. */ + private static final class FlagSelectionScope implements AutoCloseable { + final LocalHttpServer cdn = new LocalHttpServer(); + final RecordingHealthMetrics metrics = new RecordingHealthMetrics(); + private final AgentBridge bridge = new TestAgentBridge(null, null).withHealthMetrics(metrics); + private final Settings settings; + private int acquired; + + FlagSelectionScope() throws IOException { + this("/ufc"); + } + + FlagSelectionScope(final String path) throws IOException { + this.cdn.otherwise(200, CDN_CONFIG); + this.settings = + TestSettings.of( + "feature.flags.configuration.source.agentless.base.url", + this.cdn.uri(path).toString()); + FlagSelectionTestSupport.resetReported(); + CapturingServiceProvider.enable("com.datadog.openfeature", Level.WARN); + } + + FeatureFlagsRuntime acquire(final FlagSelectionRequest selection) { + final FeatureFlagsRuntime runtime = + FeatureFlagsRuntime.acquire(this.bridge, this.settings, selection); + this.acquired++; + return runtime; + } + + void releaseAll() { + while (this.acquired > 0) { + this.acquired--; + FeatureFlagsRuntime.release(); + } + } + + List messages() { + return flagSelectionMessages(); + } + + @Override + public void close() { + try { + CapturingServiceProvider.reset(); + releaseAll(); + } finally { + this.cdn.close(); + FlagSelectionTestSupport.resetReported(); + } + } + } + + private static final String CDN_CONFIG = + "{\"data\":{\"type\":\"universal-flag-configuration\",\"attributes\":" + + "{\"environment\":{\"name\":\"Test\"},\"flags\":{}}}}"; + private static com.datadog.openfeature.internal.exposure.ExposureEvent exposure() { return new com.datadog.openfeature.internal.exposure.ExposureEvent( 1L, @@ -606,6 +911,7 @@ private static final class TestAgentBridge implements AgentBridge { private final EventTransport eventProxy; private boolean agentShutdown; private RuntimeException shutdownFailure; + private HealthMetrics healthMetrics = HealthMetrics.NOOP; volatile Runnable shutdownFlush; volatile boolean shutdownUnregistered; @@ -632,7 +938,7 @@ public EventTransport eventProxy() { @Override public HealthMetrics healthMetrics() { - return HealthMetrics.NOOP; + return this.healthMetrics; } @Override @@ -654,6 +960,11 @@ public ShutdownSource shutdown() { }; } + TestAgentBridge withHealthMetrics(final HealthMetrics healthMetrics) { + this.healthMetrics = healthMetrics; + return this; + } + TestAgentBridge withAgentShutdown() { this.agentShutdown = true; return this; diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/LocalHttpServer.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/LocalHttpServer.java index c2d2e9dbdc6..7256945639b 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/LocalHttpServer.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/LocalHttpServer.java @@ -25,7 +25,7 @@ public final class LocalHttpServer implements AutoCloseable { private final ConcurrentLinkedQueue responses = new ConcurrentLinkedQueue<>(); private final List requests = new CopyOnWriteArrayList<>(); private final BlockingQueue received = new LinkedBlockingQueue<>(); - private volatile Response fallback = new Response(404, null, Map.of(), false); + private volatile Response fallback = new Response(404, null, List.of(), false); public LocalHttpServer() throws IOException { this.server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); @@ -79,11 +79,12 @@ public void close() { private static Response response( final int status, final String body, final boolean gzip, final String... headers) { - final Map headerMap = new java.util.HashMap<>(); + // A list rather than a map, so that a header name can repeat. + final List> headerList = new java.util.ArrayList<>(); for (int i = 0; i < headers.length; i += 2) { - headerMap.put(headers[i], headers[i + 1]); + headerList.add(new java.util.AbstractMap.SimpleImmutableEntry<>(headers[i], headers[i + 1])); } - return new Response(status, body, headerMap, gzip); + return new Response(status, body, headerList, gzip); } private void handle(final HttpExchange exchange) throws IOException { @@ -99,7 +100,7 @@ private void handle(final HttpExchange exchange) throws IOException { if (response == null) { response = this.fallback; } - for (final Map.Entry header : response.headers.entrySet()) { + for (final Map.Entry header : response.headers) { exchange.getResponseHeaders().add(header.getKey(), header.getValue()); } byte[] body = response.body == null ? new byte[0] : response.body.getBytes(UTF_8); @@ -146,13 +147,13 @@ public String bodyAsString() { private static final class Response { final int status; final String body; - final Map headers; + final List> headers; final boolean gzip; Response( final int status, final String body, - final Map headers, + final List> headers, final boolean gzip) { this.status = status; this.body = body; diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSourceFlagSelectionTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSourceFlagSelectionTest.java new file mode 100644 index 00000000000..0a7b760b0d6 --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSourceFlagSelectionTest.java @@ -0,0 +1,301 @@ +package com.datadog.openfeature.internal.delivery; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +import com.datadog.openfeature.internal.LocalHttpServer; +import com.datadog.openfeature.internal.config.TestSettings; +import com.datadog.openfeature.internal.delivery.FlagSelectionTestSupport.RecordingHealthMetrics; +import com.datadog.openfeature.internal.ufc.ServerConfiguration; +import com.datadog.openfeature.testlog.CapturingServiceProvider; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.time.Duration; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ScheduledExecutorService; +import java.util.function.Consumer; +import java.util.stream.Collectors; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.event.Level; +import org.tabletest.junit.TableTest; + +class CdnConfigurationSourceFlagSelectionTest { + private static final String CONFIG = + "{\"data\":{\"type\":\"universal-flag-configuration\",\"attributes\":" + + "{\"createdAt\":\"2024-04-17T19:40:53.716Z\",\"environment\":{\"name\":\"Test\"}," + + "\"flags\":{}}}}"; + private static final String IGNORED = "DD-Flag-Selection-Ignored"; + private static final FlagSelectionRequest SELECTION = + FlagSelectionRequest.of(asList("area:checkout", "a b"), singletonList("checkout.*")); + private static final String SELECTION_QUERY = + "selected_flag_tags=a%20b&selected_flag_tags=area%3Acheckout" + + "&selected_flag_key_patterns=checkout.%2A"; + + private LocalHttpServer server; + private final RecordingHealthMetrics metrics = new RecordingHealthMetrics(); + private final List dispatched = new CopyOnWriteArrayList<>(); + + @BeforeEach + void setUp() throws IOException { + this.server = new LocalHttpServer(); + FlagSelectionTestSupport.resetReported(); + CapturingServiceProvider.enable("com.datadog.openfeature", Level.WARN); + } + + @AfterEach + void tearDown() { + this.server.close(); + CapturingServiceProvider.reset(); + FlagSelectionTestSupport.resetReported(); + } + + @Test + void managedEndpointSendsTheSelectionAfterTheEnv() { + final URI managed = CdnConfigurationSource.endpoint(TestSettings.of("env", "a b")); + + assertEquals( + "https://ufc-server.ff-cdn.datadoghq.com/api/v2/feature-flagging/config/rules-based/server" + + "?dd_env=a+b&" + + SELECTION_QUERY, + SELECTION.appendTo(managed).toString()); + } + + @TableTest({ + "scenario | baseUrl | path | query ", + "custom path | '/ufc.json?token=a%2Bb' | '/ufc.json' | 'token=a%2Bb&'", + "default path, query | '/?dd_env=test' | '/api/v2/feature-flagging/config/rules-based/server' | 'dd_env=test&'", + "default path | '/' | '/api/v2/feature-flagging/config/rules-based/server' | '' " + }) + void mergesTheSelectionIntoTheCustomEndpointQuery( + final String baseUrl, final String path, final String query) { + this.server.enqueue(200, CONFIG); + final CdnConfigurationSource source = + new CdnConfigurationSource( + TestSettings.of( + "feature.flags.configuration.source.agentless.base.url", + this.server.uri(baseUrl).toString()), + this.metrics.services(), + SELECTION); + try { + source.start(this.dispatched::add); + } finally { + source.close(); + } + + final LocalHttpServer.Request request = this.server.requests().get(0); + assertEquals(path, request.uri.getRawPath()); + assertEquals(query + SELECTION_QUERY, request.uri.getRawQuery()); + assertEquals(SELECTION, source.selection()); + } + + @Test + void reportsTheIgnoredSelectionOnlyAfterTheConfigurationIsApplied() { + this.server.enqueue(200, CONFIG, IGNORED, "tag_load_failed"); + final List> messagesWhenApplied = new CopyOnWriteArrayList<>(); + final CdnConfigurationSource source = + source(SELECTION, configuration -> messagesWhenApplied.add(messages())); + + assertTrue(source.pollOnce()); + + assertEquals(singletonList(emptyList()), messagesWhenApplied); + assertEquals( + singletonList( + "ERROR flagSelection was ignored by the server (tag_load_failed). Loaded the full flag" + + " configuration."), + messages()); + assertEquals(singletonList("flag_selection.ignored|1|tag_load_failed"), this.metrics.counts); + } + + @Test + void reportsEachIgnoredReasonOncePerClassLoader() { + this.server + .enqueue(200, CONFIG, IGNORED, "too_many_values") + .enqueue(200, CONFIG, IGNORED, "too_many_values") + .enqueue(200, CONFIG, IGNORED, "invalid_tag"); + final CdnConfigurationSource source = source(SELECTION, this.dispatched::add); + final CdnConfigurationSource other = source(SELECTION, this.dispatched::add); + + assertTrue(source.pollOnce()); + assertTrue(other.pollOnce()); + assertTrue(source.pollOnce()); + + assertEquals(3, this.dispatched.size()); + assertEquals( + asList( + "ERROR flagSelection was ignored by the server (too_many_values). Loaded the full flag" + + " configuration.", + "ERROR flagSelection was ignored by the server (invalid_tag). Loaded the full flag" + + " configuration."), + messages()); + assertEquals( + asList("flag_selection.ignored|1|too_many_values", "flag_selection.ignored|1|invalid_tag"), + this.metrics.counts); + } + + @TableTest({ + "scenario | header ", + "wrong case | 'INVALID_TAG' ", + "unknown | 'server_busy' ", + "33 characters | 'invalid_taginvalid_taginvalid_tag'" + }) + void reportsAnUnexpectedIgnoredHeaderAsUnknown(final String header) { + this.server.enqueue(200, CONFIG, IGNORED, header); + + assertTrue(source(SELECTION, this.dispatched::add).pollOnce()); + + assertEquals( + singletonList( + "ERROR flagSelection was ignored by the server (unknown). Loaded the full flag" + + " configuration."), + messages()); + assertEquals(singletonList("flag_selection.ignored|1|unknown"), this.metrics.counts); + } + + @TableTest({ + "scenario | first | second ", + "distinct duplicates | 'invalid_tag' | 'tag_load_failed'", + "identical duplicates | 'invalid_tag' | 'invalid_tag' ", + "empty first value | '' | 'invalid_tag' " + }) + void reportsARepeatedIgnoredHeaderAsUnknown(final String first, final String second) { + this.server.enqueue(200, CONFIG, IGNORED, first, IGNORED, second); + + assertTrue(source(SELECTION, this.dispatched::add).pollOnce()); + + assertEquals(1, this.dispatched.size()); + assertEquals( + singletonList( + "ERROR flagSelection was ignored by the server (unknown). Loaded the full flag" + + " configuration."), + messages()); + assertEquals(singletonList("flag_selection.ignored|1|unknown"), this.metrics.counts); + } + + @Test + void readsTheIgnoredHeaderOnlyOnAnAppliedConfiguration() { + this.server + .enqueue(304, null, IGNORED, "invalid_tag") + .enqueue(404, null, IGNORED, "invalid_tag") + .enqueue(500, null, IGNORED, "invalid_tag") + .enqueue(500, null, IGNORED, "invalid_tag") + .enqueue(500, null, IGNORED, "invalid_tag") + .enqueue(200, "{not json", IGNORED, "invalid_tag") + .enqueue(200, CONFIG, IGNORED, ""); + final CdnConfigurationSource source = source(SELECTION, this.dispatched::add); + + assertTrue(source.pollOnce()); + assertFalse(source.pollOnce()); + assertFalse(source.pollOnce()); + assertFalse(source.pollOnce()); + assertTrue(source.pollOnce()); + + assertEquals(1, this.dispatched.size()); + assertTrue(messages().isEmpty(), messages().toString()); + assertTrue(this.metrics.counts.isEmpty()); + } + + @Test + void ignoresTheHeaderWithoutASelection() { + this.server.enqueue(200, CONFIG, IGNORED, "invalid_tag"); + final CdnConfigurationSource source = source(FlagSelectionRequest.NONE, this.dispatched::add); + + assertTrue(source.pollOnce()); + + assertEquals("/ufc", this.server.requests().get(0).uri.toString()); + assertTrue(messages().isEmpty()); + assertTrue(this.metrics.counts.isEmpty()); + } + + @Test + void keepsTheEtagOfTheSelectedConfiguration() { + this.server.enqueue(200, CONFIG, "ETag", "\"selected\"").enqueue(304, null); + final CdnConfigurationSource source = source(SELECTION, this.dispatched::add); + + assertTrue(source.pollOnce()); + assertTrue(source.pollOnce()); + + final List requests = this.server.requests(); + assertEquals(SELECTION_QUERY, requests.get(0).uri.getRawQuery()); + assertEquals(SELECTION_QUERY, requests.get(1).uri.getRawQuery()); + assertNull(requests.get(0).header("If-None-Match")); + assertEquals("\"selected\"", requests.get(1).header("If-None-Match")); + assertEquals(1, this.dispatched.size()); + } + + @Test + void sendsNoSelectionWhenTheUrlWouldBeOverTheLimit() { + // The endpoint fits in 8192 bytes, but not once the selection is added. + final char[] padding = new char[8150]; + Arrays.fill(padding, 'x'); + this.server.otherwise(200, CONFIG, IGNORED, "invalid_tag"); + final CdnConfigurationSource source = + source(this.server.uri("/ufc?pad=" + new String(padding)), SELECTION, this.dispatched::add); + + assertTrue(source.pollOnce()); + assertTrue(source.pollOnce()); + + assertEquals(2, this.server.requests().size()); + for (final LocalHttpServer.Request request : this.server.requests()) { + assertEquals("pad=" + new String(padding), request.uri.getRawQuery()); + } + assertEquals(FlagSelectionRequest.urlTooLong(), source.selection()); + assertTrue(messages().isEmpty(), "the runtime reports the dropped selection"); + assertTrue(this.metrics.counts.isEmpty(), "the header of an unselected request is not read"); + } + + @Test + void droppedSelectionIsNotSent() { + final FlagSelectionRequest dropped = FlagSelectionRequest.of(singletonList(null), null); + this.server.enqueue(200, CONFIG, IGNORED, "malformed_selection"); + final CdnConfigurationSource source = source(dropped, this.dispatched::add); + + assertTrue(source.pollOnce()); + + assertNull(this.server.requests().get(0).uri.getRawQuery()); + assertEquals(dropped, source.selection()); + assertTrue(this.metrics.counts.isEmpty()); + } + + private CdnConfigurationSource source( + final FlagSelectionRequest selection, final Consumer listener) { + return source(this.server.uri("/ufc"), selection, listener); + } + + private CdnConfigurationSource source( + final URI endpoint, + final FlagSelectionRequest selection, + final Consumer listener) { + final CdnConfigurationSource source = + new CdnConfigurationSource( + endpoint, + selection, + null, + 30_000, + Duration.ofSeconds(5), + HttpClient.newHttpClient(), + mock(ScheduledExecutorService.class), + delay -> {}, + () -> 1.0, + this.metrics.services()); + source.setListenerForTest(listener); + return source; + } + + private static List messages() { + return CapturingServiceProvider.events().stream() + .filter(e -> e.message.startsWith("flagSelection")) + .map(e -> e.level + " " + e.message) + .collect(Collectors.toList()); + } +} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSourceTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSourceTest.java index 5b2489282b7..ce83ce312ea 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSourceTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/CdnConfigurationSourceTest.java @@ -152,7 +152,8 @@ void customEndpointDoesNotSendApiKey() throws Exception { this.server.uri("/").toString(), "api-key", "secret"), - new RuntimeServices(HealthMetrics.NOOP)); + new RuntimeServices(HealthMetrics.NOOP), + FlagSelectionRequest.NONE); try { source.start(this.dispatched::add); } finally { diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionRequestTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionRequestTest.java new file mode 100644 index 00000000000..a1ffc0c6687 --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionRequestTest.java @@ -0,0 +1,471 @@ +package com.datadog.openfeature.internal.delivery; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.datadog.openfeature.internal.RuntimeServices; +import com.datadog.openfeature.internal.delivery.FlagSelectionTestSupport.RecordingHealthMetrics; +import com.datadog.openfeature.testlog.CapturingServiceProvider; +import java.net.URI; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.slf4j.event.Level; +import org.tabletest.junit.TableTest; + +class FlagSelectionRequestTest { + private static final URI ENDPOINT = URI.create("https://cdn.example/ufc"); + + @BeforeEach + void captureLogs() { + FlagSelectionTestSupport.resetReported(); + CapturingServiceProvider.enable("com.datadog.openfeature", Level.WARN); + } + + @AfterEach + void resetLogs() { + CapturingServiceProvider.reset(); + FlagSelectionTestSupport.resetReported(); + } + + @ParameterizedTest + @ValueSource( + ints = { + 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x20, 0x85, 0xA0, 0x1680, 0x2000, 0x2001, 0x2002, 0x2003, + 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x2028, 0x2029, 0x202F, 0x205F, + 0x3000 + }) + void trimsEveryUnicodeWhiteSpaceCharacter(final int whiteSpace) { + final String padding = String.valueOf((char) whiteSpace); + + final FlagSelectionRequest selection = + FlagSelectionRequest.of(singletonList(padding + "a b" + padding + padding), null); + + assertEquals(singletonList("a b"), selection.tags()); + } + + @ParameterizedTest + @ValueSource(ints = {0x1C, 0x1D, 0x1E, 0x1F, 0x180E, 0x200B, 0x2060, 0xFEFF}) + void keepsCharactersOutsideUnicodeWhiteSpace(final int character) { + final String value = (char) character + "a" + (char) character; + + final FlagSelectionRequest selection = FlagSelectionRequest.of(null, singletonList(value)); + + assertEquals(singletonList(value), selection.keyPatterns()); + } + + @Test + void sortsByUtf8BytesRatherThanUtf16CodeUnits() { + final FlagSelectionRequest selection = + FlagSelectionRequest.of(asList("\uD83D\uDE00", "\uFF21", "a", "Z", "\u00E9"), null); + + assertEquals(asList("Z", "a", "\u00E9", "\uFF21", "\uD83D\uDE00"), selection.tags()); + } + + @Test + void dropsEmptyValuesAndDuplicatesInEachList() { + final FlagSelectionRequest selection = + FlagSelectionRequest.of(asList(" b", "a", "", "b ", "\u3000"), asList("a", " a ")); + + assertEquals(asList("a", "b"), selection.tags()); + assertEquals(singletonList("a"), selection.keyPatterns()); + } + + @Test + void noListsAndEmptyListsSelectAllTheFlags() { + assertTrue(FlagSelectionRequest.of(null, null).isNone()); + assertTrue(FlagSelectionRequest.of(emptyList(), emptyList()).isNone()); + assertTrue(FlagSelectionRequest.of(asList(" ", ""), singletonList("\t")).isNone()); + assertTrue(FlagSelectionRequest.NONE.isNone()); + assertEquals(FlagSelectionRequest.NONE, FlagSelectionRequest.of(emptyList(), null)); + assertNull(FlagSelectionRequest.NONE.dropReason()); + assertFalse(FlagSelectionRequest.of(singletonList(null), null).isNone()); + assertFalse(FlagSelectionRequest.urlTooLong().isNone()); + } + + @Test + @SuppressWarnings({"unchecked", "rawtypes"}) + void malformedElementsAreReportedAsMalformedSelection() { + final List raw = new ArrayList(); + raw.add("a"); + raw.add(42); + final List> malformed = + asList( + asList("a", null), + raw, + singletonList("\uD83D"), + singletonList("a\uDE00"), + singletonList("\uDE00\uD83D"), + singletonList("\uD83Da")); + + for (final List values : malformed) { + assertEquals( + "malformed_selection", FlagSelectionRequest.of(values, null).dropReason(), "tags"); + assertEquals( + "malformed_selection", FlagSelectionRequest.of(null, values).dropReason(), "keyPatterns"); + } + } + + @Test + void aListThatFailsToBeReadIsMalformed() { + final List failing = + new AbstractList() { + @Override + public String get(final int index) { + throw new IllegalStateException("concurrently modified"); + } + + @Override + public int size() { + return 1; + } + }; + + assertEquals( + "malformed_selection", + assertDoesNotThrow(() -> FlagSelectionRequest.of(failing, null)).dropReason()); + } + + @Test + void aListThatBreaksTheToArrayContractIsMalformed() { + final List broken = + new AbstractList() { + @Override + public String get(final int index) { + return "area:checkout"; + } + + @Override + public int size() { + return 1; + } + + @Override + public Object[] toArray() { + return null; + } + }; + + assertEquals( + "malformed_selection", + assertDoesNotThrow(() -> FlagSelectionRequest.of(broken, null)).dropReason()); + } + + @TableTest({ + "scenario | tags | tagLength | keyPatterns | keyPatternLength | reason ", + "31 values in each list | 31 | 1 | 31 | 1 | ", + "32 tags | 32 | 1 | 0 | 1 | too_many_values ", + "32 key patterns | 0 | 1 | 32 | 1 | too_many_values ", + "200-byte values | 1 | 200 | 1 | 200 | ", + "201-byte key pattern | 1 | 1 | 1 | 201 | invalid_key_pattern", + "201-byte tag | 1 | 201 | 1 | 1 | invalid_tag ", + "too_many_values before invalid_key_pattern | 1 | 1 | 32 | 201 | too_many_values ", + "invalid_key_pattern before invalid_tag | 1 | 201 | 1 | 201 | invalid_key_pattern" + }) + void validatesTheLimitsInPrecedenceOrder( + final int tags, + final int tagLength, + final int keyPatterns, + final int keyPatternLength, + final String reason) { + final FlagSelectionRequest selection = + FlagSelectionRequest.of(values(tags, tagLength), values(keyPatterns, keyPatternLength)); + + assertEquals(reason, selection.dropReason()); + } + + @Test + void malformedSelectionTakesPrecedenceOverTheLimits() { + final List tooMany = values(32, 201); + + assertEquals( + "malformed_selection", FlagSelectionRequest.of(tooMany, asList("a", null)).dropReason()); + assertEquals( + "malformed_selection", + FlagSelectionRequest.of(singletonList("\uD83D"), tooMany).dropReason()); + } + + @Test + void comparesSelectionsInCanonicalForm() { + final FlagSelectionRequest selection = FlagSelectionRequest.of(asList("b", "a"), null); + + assertEquals(selection, FlagSelectionRequest.of(asList(" a", "b", "a"), emptyList())); + assertEquals(selection.hashCode(), FlagSelectionRequest.of(asList("a", "b"), null).hashCode()); + assertNotEquals(selection, FlagSelectionRequest.of(null, asList("a", "b"))); + assertNotEquals(selection, FlagSelectionRequest.NONE); + assertEquals( + FlagSelectionRequest.of(values(32, 1), null), FlagSelectionRequest.of(values(33, 1), null)); + } + + @TableTest({ + "scenario | tag | keyPattern | query ", + "space and colon | 'release:summer sale' | 'ops-*' | 'selected_flag_tags=release%3Asummer%20sale&selected_flag_key_patterns=ops-%2A' ", + "unreserved | 'aZ09-._~' | 'x' | 'selected_flag_tags=aZ09-._~&selected_flag_key_patterns=x' ", + "reserved | '+,;=&#%/?@' | ':' | 'selected_flag_tags=%2B%2C%3B%3D%26%23%25%2F%3F%40&selected_flag_key_patterns=%3A'", + "non-ASCII | 'caf\u00e9' | 'x' | 'selected_flag_tags=caf%C3%A9&selected_flag_key_patterns=x' " + }) + void percentEncodesValuesWithUppercaseHex( + final String tag, final String keyPattern, final String query) { + final URI sent = + FlagSelectionRequest.of(singletonList(tag), singletonList(keyPattern)).appendTo(ENDPOINT); + + assertEquals("https://cdn.example/ufc?" + query, sent.toString()); + } + + @Test + void encodesTheCharactersThatEncodeUriComponentKeeps() { + final URI sent = FlagSelectionRequest.of(singletonList("!'()*"), null).appendTo(ENDPOINT); + + assertEquals("https://cdn.example/ufc?selected_flag_tags=%21%27%28%29%2A", sent.toString()); + } + + @Test + void encodesCharactersOutsideTheBasicMultilingualPlane() { + final URI sent = + FlagSelectionRequest.of(singletonList("\uD83D\uDE00"), null).appendTo(ENDPOINT); + + assertEquals("https://cdn.example/ufc?selected_flag_tags=%F0%9F%98%80", sent.toString()); + } + + @TableTest({ + "scenario | endpoint | sent ", + "no query | 'https://cdn.example/ufc' | 'https://cdn.example/ufc?selected_flag_tags=a&selected_flag_tags=b&selected_flag_key_patterns=c' ", + "empty query | 'https://cdn.example/ufc?' | 'https://cdn.example/ufc?selected_flag_tags=a&selected_flag_tags=b&selected_flag_key_patterns=c' ", + "existing query | 'https://cdn.example/ufc?dd_env=a+b&t=%2B' | 'https://cdn.example/ufc?dd_env=a+b&t=%2B&selected_flag_tags=a&selected_flag_tags=b&selected_flag_key_patterns=c'", + "fragment | 'https://cdn.example/ufc?dd_env=test#top' | 'https://cdn.example/ufc?dd_env=test&selected_flag_tags=a&selected_flag_tags=b&selected_flag_key_patterns=c#top' ", + "fragment, no query | 'http://user@localhost:8080/ufc.json#top' | 'http://user@localhost:8080/ufc.json?selected_flag_tags=a&selected_flag_tags=b&selected_flag_key_patterns=c#top' ", + "encoded path | 'https://cdn.example/a%20b/ufc' | 'https://cdn.example/a%20b/ufc?selected_flag_tags=a&selected_flag_tags=b&selected_flag_key_patterns=c' " + }) + void mergesTheParametersIntoTheEndpointQuery(final String endpoint, final String sent) { + final FlagSelectionRequest selection = + FlagSelectionRequest.of(asList("b", "a"), singletonList("c")); + + assertEquals(sent, selection.appendTo(URI.create(endpoint)).toString()); + } + + @Test + void leavesTheEndpointUnchangedWithoutValues() { + assertSame(ENDPOINT, FlagSelectionRequest.NONE.appendTo(ENDPOINT)); + assertSame(ENDPOINT, FlagSelectionRequest.of(values(32, 1), null).appendTo(ENDPOINT)); + } + + @Test + void limitsTheWholeRequestUrlTo8192Bytes() { + final FlagSelectionRequest selection = FlagSelectionRequest.of(singletonList("a"), null); + final String base = "https://cdn.example/ufc?pad="; + final int parameterLength = "&selected_flag_tags=a".length(); + + final URI atLimit = URI.create(base + repeat('x', 8192 - base.length() - parameterLength)); + final URI overLimit = URI.create(base + repeat('x', 8193 - base.length() - parameterLength)); + + assertEquals(8192, selection.appendTo(atLimit).toASCIIString().length()); + assertNull(selection.appendTo(overLimit)); + } + + @Test + void measuresTheUrlAfterPercentEncoding() { + final FlagSelectionRequest selection = FlagSelectionRequest.of(singletonList("a"), null); + // U+00E9 is sent as six characters, %C3%A9: five more than its string form. + final String base = "https://cdn.example/caf\u00e9?pad="; + final int parameterLength = "&selected_flag_tags=a".length(); + + final URI atLimit = URI.create(base + repeat('x', 8192 - 5 - base.length() - parameterLength)); + final URI overLimit = URI.create(base + repeat('x', 8192 - base.length() - parameterLength)); + + assertEquals(8192, selection.appendTo(atLimit).toASCIIString().length()); + assertNull(selection.appendTo(overLimit)); + } + + @TableTest({ + "scenario | header | reason ", + "malformed_selection | 'malformed_selection' | malformed_selection", + "too_many_values | 'too_many_values' | too_many_values ", + "invalid_key_pattern | 'invalid_key_pattern' | invalid_key_pattern", + "invalid_tag | 'invalid_tag' | invalid_tag ", + "tag_load_failed | 'tag_load_failed' | tag_load_failed ", + "wrong case | 'TAG_LOAD_FAILED' | unknown ", + "trailing white space | 'tag_load_failed ' | unknown ", + "leading white space | ' invalid_tag' | unknown ", + "unknown reason | 'server_busy' | unknown ", + "33 characters | 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | unknown ", + "known reason with tail | 'invalid_tag,invalid_tag' | unknown ", + "empty | '' | " + }) + void sanitizesTheIgnoredHeader(final String header, final String reason) { + assertEquals(reason, FlagSelectionRequest.ignoredReason(header)); + } + + @Test + void absentIgnoredHeaderHasNoReason() { + assertNull(FlagSelectionRequest.ignoredReason(null)); + } + + @TableTest({ + "scenario | reason | limit ", + "malformed_selection | malformed_selection | 'flagSelection must be an object with optional tags and keyPatterns string lists'", + "too_many_values | too_many_values | 'each list accepts at most 31 values' ", + "invalid_key_pattern | invalid_key_pattern | 'each key pattern is at most 200 UTF-8 bytes' ", + "invalid_tag | invalid_tag | 'each tag is at most 200 UTF-8 bytes' " + }) + void reportsADroppedSelectionAtEachCallAndCountsItOnce(final String reason, final String limit) { + final FlagSelectionRequest selection = dropped(reason); + final RecordingHealthMetrics metrics = new RecordingHealthMetrics(); + + selection.reportNotSent(metrics.services()); + selection.reportNotSent(metrics.services()); + + final String message = + "flagSelection was not sent (" + + reason + + ": " + + limit + + "). Loading the full flag configuration."; + assertEquals(asList("ERROR " + message, "ERROR " + message), messages()); + assertEquals(singletonList("flag_selection.dropped|1|" + reason), metrics.counts); + } + + @Test + void reportsAnOverlongUrlAsTooManyValues() { + final RecordingHealthMetrics metrics = new RecordingHealthMetrics(); + + FlagSelectionRequest.urlTooLong().reportNotSent(metrics.services()); + + assertEquals( + singletonList( + "ERROR flagSelection was not sent (too_many_values: the request URL is at most 8192" + + " bytes). Loading the full flag configuration."), + messages()); + assertEquals(singletonList("flag_selection.dropped|1|too_many_values"), metrics.counts); + assertEquals("too_many_values", FlagSelectionRequest.urlTooLong().dropReason()); + } + + @Test + void reportsNothingForASelectionThatIsSent() { + final RecordingHealthMetrics metrics = new RecordingHealthMetrics(); + + FlagSelectionRequest.NONE.reportNotSent(metrics.services()); + FlagSelectionRequest.of(singletonList("a"), null).reportNotSent(metrics.services()); + + assertTrue(messages().isEmpty()); + assertTrue(metrics.counts.isEmpty()); + } + + @Test + void reportsEachIgnoredReasonOnce() { + final RecordingHealthMetrics metrics = new RecordingHealthMetrics(); + final RuntimeServices services = metrics.services(); + + FlagSelectionRequest.reportIgnored(services, "tag_load_failed"); + FlagSelectionRequest.reportIgnored(services, "tag_load_failed"); + FlagSelectionRequest.reportIgnored(services, "unknown"); + + assertEquals( + asList( + "ERROR flagSelection was ignored by the server (tag_load_failed). Loaded the full flag" + + " configuration.", + "ERROR flagSelection was ignored by the server (unknown). Loaded the full flag" + + " configuration."), + messages()); + assertEquals( + asList("flag_selection.ignored|1|tag_load_failed", "flag_selection.ignored|1|unknown"), + metrics.counts); + } + + @Test + void warnsOnceAboutASecondSelectionAndRemoteConfiguration() { + FlagSelectionRequest.warnSecondSelection(); + FlagSelectionRequest.warnSecondSelection(); + FlagSelectionRequest.warnRemoteConfiguration(); + FlagSelectionRequest.warnRemoteConfiguration(); + + assertEquals( + asList( + "WARN flagSelection differs from the flag selection already in use in this process." + + " Using the first selection.", + "WARN flagSelection is not supported when flags are delivered through Remote Config." + + " Loading the full flag configuration."), + messages()); + } + + @Test + void failingDiagnosticsDoNotThrow() { + CapturingServiceProvider.failOn("flagSelection"); + final RuntimeServices failingMetrics = + new RuntimeServices((metric, value, reason) -> { + throw new IllegalStateException("metrics backend failed"); + }); + + assertDoesNotThrow(() -> dropped("invalid_tag").reportNotSent(failingMetrics)); + assertDoesNotThrow(() -> FlagSelectionRequest.reportIgnored(failingMetrics, "invalid_tag")); + assertDoesNotThrow(FlagSelectionRequest::warnSecondSelection); + assertDoesNotThrow(FlagSelectionRequest::warnRemoteConfiguration); + assertFalse(messages().isEmpty()); + } + + @Test + void countsEachReasonOncePerKind() { + final RecordingHealthMetrics metrics = new RecordingHealthMetrics(); + + dropped("too_many_values").reportNotSent(metrics.services()); + dropped("invalid_tag").reportNotSent(metrics.services()); + FlagSelectionRequest.urlTooLong().reportNotSent(metrics.services()); + FlagSelectionRequest.reportIgnored(metrics.services(), "too_many_values"); + + assertEquals( + asList( + "flag_selection.dropped|1|too_many_values", + "flag_selection.dropped|1|invalid_tag", + "flag_selection.ignored|1|too_many_values"), + metrics.counts); + } + + private static FlagSelectionRequest dropped(final String reason) { + switch (reason) { + case "malformed_selection": + return FlagSelectionRequest.of(singletonList(null), null); + case "too_many_values": + return FlagSelectionRequest.of(values(32, 1), null); + case "invalid_key_pattern": + return FlagSelectionRequest.of(null, values(1, 201)); + case "invalid_tag": + return FlagSelectionRequest.of(values(1, 201), null); + default: + throw new IllegalArgumentException(reason); + } + } + + private static List values(final int count, final int length) { + return IntStream.range(0, count) + .mapToObj(i -> String.format("%0" + length + "d", i)) + .collect(Collectors.toList()); + } + + private static String repeat(final char character, final int count) { + final char[] characters = new char[count]; + Arrays.fill(characters, character); + return new String(characters); + } + + private static List messages() { + return CapturingServiceProvider.events().stream() + .filter(e -> e.message.startsWith("flagSelection")) + .map(e -> e.level + " " + e.message) + .collect(Collectors.toList()); + } +} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionTestSupport.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionTestSupport.java new file mode 100644 index 00000000000..03d7fc6f449 --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionTestSupport.java @@ -0,0 +1,30 @@ +package com.datadog.openfeature.internal.delivery; + +import com.datadog.openfeature.internal.RuntimeServices; +import com.datadog.openfeature.internal.bridge.v1.HealthMetrics; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** Test helpers for the flag selection diagnostics, which are reported once per class loader. */ +public final class FlagSelectionTestSupport { + private FlagSelectionTestSupport() {} + + /** Forgets the diagnostics already reported, so a test sees its own first ones. */ + public static void resetReported() { + FlagSelectionRequest.resetReportedForTest(); + } + + /** Records the health counters it receives, as {@code name|value|reason}. */ + public static final class RecordingHealthMetrics implements HealthMetrics { + public final List counts = new CopyOnWriteArrayList<>(); + + @Override + public void count(final String metric, final long value, final String reason) { + this.counts.add(metric + "|" + value + "|" + reason); + } + + public RuntimeServices services() { + return new RuntimeServices(this); + } + } +} diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionVectorsTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionVectorsTest.java new file mode 100644 index 00000000000..12f5460395e --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/delivery/FlagSelectionVectorsTest.java @@ -0,0 +1,121 @@ +package com.datadog.openfeature.internal.delivery; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Collections.emptyList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import com.datadog.openfeature.internal.JsonReading; +import java.io.IOException; +import java.net.URI; +import java.net.URLDecoder; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * Runs the cross-SDK flag selection vectors from the {@code ffe-system-test-data} submodule. The + * {@code rawQuery} cases exercise the server's percent-decoding, so they have no SDK expectation. + */ +class FlagSelectionVectorsTest { + private static final String VECTORS_PATH = + "dd-smoke-tests/openfeature/src/test/resources/ffe-system-test-data/flag-selection/selection-vectors.json"; + private static final URI ENDPOINT = + URI.create( + "https://ufc-server.ff-cdn.datadoghq.com/api/v2/feature-flagging/config/rules-based/server?dd_env=prod"); + + // No exact counts: the submodule can add vector cases at any time. + @Test + void runsEverySdkCaseAndSkipsOnlyTheRawQueryCases() throws IOException { + final List> cases = vectorCases(); + + assertTrue(cases.stream().anyMatch(c -> c.containsKey("sdk")), "the vectors have no SDK case"); + for (final Map testCase : cases) { + assertTrue( + testCase.containsKey("sdk") != testCase.containsKey("rawQuery"), + "exactly one of sdk or rawQuery: " + testCase.get("name")); + } + } + + static Stream cases() throws IOException { + return vectorCases().stream().map(c -> Arguments.of(c.get("name"), c)); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("cases") + @SuppressWarnings("unchecked") + void sendsTheCanonicalSelectionOrDropsIt(final String name, final Map testCase) { + assumeTrue(testCase.containsKey("sdk"), "the server's percent-decoding has no SDK expectation"); + final Map query = (Map) testCase.get("query"); + final Map sdk = (Map) testCase.get("sdk"); + + final FlagSelectionRequest selection = + FlagSelectionRequest.of( + (List) query.get("selected_flag_tags"), + (List) query.get("selected_flag_key_patterns")); + + if (sdk.containsKey("dropped")) { + assertEquals(sdk.get("dropped"), selection.dropReason()); + assertEquals(ENDPOINT, selection.appendTo(ENDPOINT), "a dropped selection is not sent"); + return; + } + final Map expected = (Map) sdk.get("selection"); + final List tags = (List) expected.get("tags"); + final List keyPatterns = (List) expected.get("keyPatterns"); + assertNull(selection.dropReason()); + assertEquals(tags, selection.tags()); + assertEquals(keyPatterns, selection.keyPatterns()); + assertEquals(tags.isEmpty() && keyPatterns.isEmpty(), selection.isNone()); + + final URI sent = selection.appendTo(ENDPOINT); + assertEquals(ENDPOINT.getRawPath(), sent.getRawPath()); + final List parameters = new ArrayList<>(); + parameters.add("dd_env=prod"); + tags.forEach(tag -> parameters.add("selected_flag_tags=" + tag)); + keyPatterns.forEach(pattern -> parameters.add("selected_flag_key_patterns=" + pattern)); + assertEquals(parameters, decodedParameters(sent)); + assertFalse(sent.getRawQuery().contains("+"), "values are percent-encoded, not form-encoded"); + } + + private static List decodedParameters(final URI uri) { + final List parameters = new ArrayList<>(); + for (final String parameter : uri.getRawQuery().split("&")) { + final int separator = parameter.indexOf('='); + parameters.add( + parameter.substring(0, separator) + + "=" + + URLDecoder.decode(parameter.substring(separator + 1), UTF_8)); + } + return parameters; + } + + @SuppressWarnings("unchecked") + private static List> vectorCases() throws IOException { + final Map vectors = JsonReading.readObject(Files.readAllBytes(vectorsFile())); + final Object cases = vectors.get("cases"); + return cases == null ? emptyList() : (List>) cases; + } + + private static Path vectorsFile() { + Path directory = Paths.get("").toAbsolutePath(); + while (directory != null) { + final Path candidate = directory.resolve(VECTORS_PATH); + if (Files.isRegularFile(candidate)) { + return candidate; + } + directory = directory.getParent(); + } + throw new IllegalStateException("Unable to find the flag selection vectors: " + VECTORS_PATH); + } +}