Repository navigation
Add dd-openfeature SDK settings and configuration delivery #12841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+2,789
−36
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
38f8c6c
feat(ffe): Add dd-openfeature SDK settings and configuration delivery
PerfectSlayer 6759ace
refactor(ffe): Parse the configuration source like the agent
PerfectSlayer e03bde2
fix(ffe): Validate Feature Flags endpoint URLs
PerfectSlayer 33f9cc6
fix(ffe): Parse boolean and integer settings like the agent
PerfectSlayer dc2e567
fix(ffe): Report only parsing failures as malformed Remote Configuration
PerfectSlayer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
dd-openfeature/src/main/java/com/datadog/openfeature/internal/ConfigurationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.datadog.openfeature.internal; | ||
|
|
||
| import com.datadog.openfeature.internal.ufc.ServerConfiguration; | ||
| import java.util.function.Consumer; | ||
|
|
||
| /** Delivers flag configurations to the runtime. */ | ||
| public interface ConfigurationService extends AutoCloseable { | ||
| /** | ||
| * Starts delivering configurations. Implementations may deliver the first configuration before | ||
| * returning. | ||
| * | ||
| * @param listener the listener receiving configurations, or {@code null} when removed. | ||
| */ | ||
| void start(Consumer<ServerConfiguration> listener); | ||
|
|
||
| @Override | ||
| void close(); | ||
| } |
107 changes: 107 additions & 0 deletions
107
dd-openfeature/src/main/java/com/datadog/openfeature/internal/EventQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package com.datadog.openfeature.internal; | ||
|
|
||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.locks.LockSupport; | ||
| import java.util.function.Consumer; | ||
|
|
||
| /** | ||
| * A bounded, lock-free, multiple-producer single-consumer queue. Producers never block; the | ||
| * consumer polls with a timeout by parking, as producers do not signal it. | ||
| * | ||
| * @param <E> the element type. | ||
| */ | ||
| public final class EventQueue<E> { | ||
| private static final long PARK_NANOS = TimeUnit.MILLISECONDS.toNanos(10); | ||
|
|
||
| private final ConcurrentLinkedQueue<E> queue = new ConcurrentLinkedQueue<>(); | ||
| private final AtomicInteger size = new AtomicInteger(); | ||
| private final int capacity; | ||
|
|
||
| public EventQueue(final int capacity) { | ||
| this.capacity = capacity; | ||
| } | ||
|
|
||
| /** | ||
| * Offers an element without blocking. | ||
| * | ||
| * @param element the element to offer. | ||
| * @return {@code true} if the element was queued, {@code false} if the queue is full. | ||
| */ | ||
| public boolean offer(final E element) { | ||
| if (this.size.incrementAndGet() > this.capacity) { | ||
| this.size.decrementAndGet(); | ||
| return false; | ||
| } | ||
| this.queue.offer(element); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @return the head element, or {@code null} if the queue is empty. | ||
| */ | ||
| public E poll() { | ||
| final E element = this.queue.poll(); | ||
| if (element != null) { | ||
| this.size.decrementAndGet(); | ||
| } | ||
| return element; | ||
| } | ||
|
|
||
| /** | ||
| * Polls the head element, waiting up to the given timeout for one to be available. | ||
| * | ||
| * @param timeout the maximum time to wait. | ||
| * @param unit the timeout unit. | ||
| * @return the head element, or {@code null} if none was available in time. | ||
| * @throws InterruptedException if the consumer thread is interrupted while waiting. | ||
| */ | ||
| public E poll(final long timeout, final TimeUnit unit) throws InterruptedException { | ||
| final long deadline = System.nanoTime() + unit.toNanos(timeout); | ||
| while (true) { | ||
| final E element = poll(); | ||
| if (element != null) { | ||
| return element; | ||
| } | ||
| final long remaining = deadline - System.nanoTime(); | ||
| if (remaining <= 0) { | ||
| return null; | ||
| } | ||
| LockSupport.parkNanos(this, Math.min(remaining, PARK_NANOS)); | ||
| if (Thread.interrupted()) { | ||
| throw new InterruptedException(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Drains up to {@code limit} elements to the consumer. | ||
| * | ||
| * @param consumer the consumer of the drained elements. | ||
| * @param limit the maximum number of elements to drain. | ||
| */ | ||
| public void drain(final Consumer<E> consumer, final int limit) { | ||
| for (int i = 0; i < limit; i++) { | ||
| final E element = poll(); | ||
| if (element == null) { | ||
| return; | ||
| } | ||
| consumer.accept(element); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return the current number of queued elements. | ||
| */ | ||
| public int size() { | ||
| return this.size.get(); | ||
| } | ||
|
|
||
| /** | ||
| * @return the queue capacity. | ||
| */ | ||
| public int capacity() { | ||
| return this.capacity; | ||
| } | ||
| } | ||
96 changes: 96 additions & 0 deletions
96
dd-openfeature/src/main/java/com/datadog/openfeature/internal/JsonWriting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package com.datadog.openfeature.internal; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonEncoding; | ||
| import com.fasterxml.jackson.core.JsonFactory; | ||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** JSON serialization helpers for event payloads. */ | ||
| public final class JsonWriting { | ||
| private static final JsonFactory JSON_FACTORY = new JsonFactory(); | ||
|
|
||
| private JsonWriting() {} | ||
|
|
||
| /** | ||
| * Serializes a JSON document. | ||
| * | ||
| * @param writer the document writer. | ||
| * @return the UTF-8 encoded document. | ||
| */ | ||
| public static byte[] write(final Writer writer) { | ||
| final ByteArrayOutputStream output = new ByteArrayOutputStream(256); | ||
| try (JsonGenerator generator = JSON_FACTORY.createGenerator(output, JsonEncoding.UTF8)) { | ||
| writer.write(generator); | ||
| } catch (final IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| return output.toByteArray(); | ||
| } | ||
|
|
||
| /** | ||
| * Writes an arbitrary value: maps, collections, strings, numbers, booleans and {@code null}. | ||
| * Other values are written with their string representation. | ||
| * | ||
| * @param generator the generator to write to. | ||
| * @param value the value to write. | ||
| * @throws IOException if the value could not be written. | ||
| */ | ||
| public static void writeValue(final JsonGenerator generator, @Nullable final Object value) | ||
| throws IOException { | ||
| if (value == null) { | ||
| generator.writeNull(); | ||
| } else if (value instanceof String) { | ||
| generator.writeString((String) value); | ||
| } else if (value instanceof Boolean) { | ||
| generator.writeBoolean((Boolean) value); | ||
| } else if (value instanceof Integer || value instanceof Long || value instanceof Short) { | ||
| generator.writeNumber(((Number) value).longValue()); | ||
| } else if (value instanceof Number) { | ||
| generator.writeNumber(((Number) value).doubleValue()); | ||
| } else if (value instanceof Map) { | ||
| generator.writeStartObject(); | ||
| for (final Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) { | ||
| generator.writeFieldName(String.valueOf(entry.getKey())); | ||
| writeValue(generator, entry.getValue()); | ||
| } | ||
| generator.writeEndObject(); | ||
| } else if (value instanceof Collection) { | ||
| generator.writeStartArray(); | ||
| for (final Object element : (Collection<?>) value) { | ||
| writeValue(generator, element); | ||
| } | ||
| generator.writeEndArray(); | ||
| } else { | ||
| generator.writeString(value.toString()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Writes a {@code {"key": ...}} object field, skipped when the key is {@code null}. | ||
| * | ||
| * @param generator the generator to write to. | ||
| * @param fieldName the field name. | ||
| * @param key the key value. | ||
| * @throws IOException if the field could not be written. | ||
| */ | ||
| public static void writeKeyObject( | ||
| final JsonGenerator generator, final String fieldName, @Nullable final String key) | ||
| throws IOException { | ||
| if (key != null) { | ||
| generator.writeObjectFieldStart(fieldName); | ||
| generator.writeStringField("key", key); | ||
| generator.writeEndObject(); | ||
| } | ||
| } | ||
|
|
||
| /** Writes a JSON document. */ | ||
| @FunctionalInterface | ||
| public interface Writer { | ||
| void write(JsonGenerator generator) throws IOException; | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
dd-openfeature/src/main/java/com/datadog/openfeature/internal/RuntimeServices.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.datadog.openfeature.internal; | ||
|
|
||
| import com.datadog.openfeature.internal.connector.HealthMetrics; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** Thread creation and health metrics shared by the runtime components. */ | ||
| public final class RuntimeServices { | ||
| private final HealthMetrics healthMetrics; | ||
|
|
||
| public RuntimeServices(final HealthMetrics healthMetrics) { | ||
| this.healthMetrics = healthMetrics; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a daemon thread. | ||
| * | ||
| * @param role the thread role, used to name the thread. | ||
| * @param task the thread task. | ||
| * @return the created thread, not started yet. | ||
| */ | ||
| public Thread newThread(final String role, final Runnable task) { | ||
| final Thread thread = new Thread(task, "dd-openfeature-" + role); | ||
| thread.setDaemon(true); | ||
| thread.setContextClassLoader(null); | ||
| return thread; | ||
| } | ||
|
|
||
| /** | ||
| * Increments a health counter, skipping non-positive values. | ||
| * | ||
| * @param name the metric name. | ||
| * @param value the increment. | ||
| * @param reason the optional {@code reason} tag value. | ||
| */ | ||
| public void countMetric(final String name, final long value, @Nullable final String reason) { | ||
| if (value > 0) { | ||
| this.healthMetrics.count(name, value, reason); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling offer(null) while capacity is available increments the size counter before ConcurrentLinkedQueue rejects the element. The exception permanently consumes a queue slot instead of leaving the queue unchanged. With capacity one, the empty queue subsequently rejects every valid event. Reject null before reserving capacity. The suggested replacement was verified to preserve capacity and normal insertion behavior.
React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it
What each reaction means
🤖 Bits Code Review · Open Bits AI session