Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dd-openfeature/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
`java-library`
idea
id("dd-trace-java.conventions.java")
id("dd-trace-java.version-file")
}

description = "Datadog OpenFeature provider SDK."
Expand Down
79 changes: 43 additions & 36 deletions dd-openfeature/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,84 @@
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
# To regenerate this file, run: ./gradlew :dd-openfeature:dependencies --write-locks
ch.qos.logback:logback-classic:1.2.13=testCompileClasspath,testRuntimeClasspath
ch.qos.logback:logback-core:1.2.13=testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-core:2.20.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson:jackson-bom:2.20.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
ch.qos.logback:logback-classic:1.2.13=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
ch.qos.logback:logback-core:1.2.13=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-core:2.20.0=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson:jackson-bom:2.20.0=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.github.javaparser:javaparser-core:3.25.6=codenarc
com.github.spotbugs:spotbugs-annotations:4.10.4=compileClasspath,spotbugs
com.github.spotbugs:spotbugs-annotations:4.10.4=compileClasspath,jmhCompileClasspath,spotbugs
com.github.spotbugs:spotbugs:4.10.4=spotbugs
com.github.stephenc.jcip:jcip-annotations:1.0-1=spotbugs
com.google.code.findbugs:jsr305:3.0.2=compileClasspath,spotbugs,testCompileClasspath,testRuntimeClasspath
com.google.code.findbugs:jsr305:3.0.2=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,spotbugs,testCompileClasspath,testRuntimeClasspath
com.google.code.gson:gson:2.14.0=spotbugs
com.google.errorprone:error_prone_annotations:2.48.0=spotbugs
com.thoughtworks.qdox:qdox:1.12.1=codenarc
commons-io:commons-io:2.21.0=spotbugs
de.thetaphi:forbiddenapis:3.11=compileClasspath
dev.openfeature:sdk:1.22.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.leangen.geantyref:geantyref:1.3.16=testRuntimeClasspath
de.thetaphi:forbiddenapis:3.11=compileClasspath,jmhCompileClasspath
dev.openfeature:sdk:1.22.0=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.leangen.geantyref:geantyref:1.3.16=jmhRuntimeClasspath,testRuntimeClasspath
jaxen:jaxen:2.0.6=spotbugs
net.bytebuddy:byte-buddy-agent:1.18.12=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.18.12=testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy-agent:1.18.12=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
net.bytebuddy:byte-buddy:1.18.12=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
net.sf.jopt-simple:jopt-simple:5.0.4=jmh,jmhCompileClasspath,jmhRuntimeClasspath
net.sf.saxon:Saxon-HE:12.10=spotbugs
org.apache.ant:ant-antlr:1.10.14=codenarc
org.apache.ant:ant-junit:1.10.14=codenarc
org.apache.bcel:bcel:6.12.0=spotbugs
org.apache.commons:commons-lang3:3.20.0=spotbugs
org.apache.commons:commons-math3:3.6.1=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.apache.commons:commons-text:1.15.0=spotbugs
org.apache.logging.log4j:log4j-api:2.26.1=spotbugs
org.apache.logging.log4j:log4j-core:2.26.1=spotbugs
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-ant:3.0.23=codenarc
org.codehaus.groovy:groovy-docgenerator:3.0.23=codenarc
org.codehaus.groovy:groovy-groovydoc:3.0.23=codenarc
org.codehaus.groovy:groovy-json:3.0.23=codenarc
org.codehaus.groovy:groovy-json:3.0.25=testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-json:3.0.25=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy-templates:3.0.23=codenarc
org.codehaus.groovy:groovy-xml:3.0.23=codenarc
org.codehaus.groovy:groovy:3.0.23=codenarc
org.codehaus.groovy:groovy:3.0.25=testCompileClasspath,testRuntimeClasspath
org.codehaus.groovy:groovy:3.0.25=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.codenarc:CodeNarc:3.7.0=codenarc
org.dom4j:dom4j:2.2.0=spotbugs
org.gmetrics:GMetrics:2.1.0=codenarc
org.hamcrest:hamcrest:3.0=testCompileClasspath,testRuntimeClasspath
org.hamcrest:hamcrest:3.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.jacoco:org.jacoco.agent:0.8.15=jacocoAgent,jacocoAnt
org.jacoco:org.jacoco.ant:0.8.15=jacocoAnt
org.jacoco:org.jacoco.core:0.8.15=jacocoAnt
org.jacoco:org.jacoco.report:0.8.15=jacocoAnt
org.junit.jupiter:junit-jupiter-api:5.14.1=testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-engine:5.14.1=testRuntimeClasspath
org.junit.jupiter:junit-jupiter-params:5.14.1=testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter:5.14.1=testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-commons:1.14.1=testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-engine:1.14.1=testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-launcher:1.14.1=testRuntimeClasspath
org.junit:junit-bom:5.14.1=testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-core:4.4.0=testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-junit-jupiter:4.4.0=testCompileClasspath,testRuntimeClasspath
org.objenesis:objenesis:3.3=testCompileClasspath,testRuntimeClasspath
org.opentest4j:opentest4j:1.3.0=testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-api:5.14.1=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-engine:5.14.1=jmhRuntimeClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter-params:5.14.1=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.jupiter:junit-jupiter:5.14.1=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-commons:1.14.1=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-engine:1.14.1=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.junit.platform:junit-platform-launcher:1.14.1=jmhRuntimeClasspath,testRuntimeClasspath
org.junit:junit-bom:5.14.1=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-core:4.4.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.mockito:mockito-junit-jupiter:4.4.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.objenesis:objenesis:3.3=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.openjdk.jmh:jmh-core:1.37=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.openjdk.jmh:jmh-generator-asm:1.37=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.openjdk.jmh:jmh-generator-bytecode:1.37=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.openjdk.jmh:jmh-generator-reflection:1.37=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.opentest4j:opentest4j:1.3.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.ow2.asm:asm-analysis:9.10.1=spotbugs
org.ow2.asm:asm-commons:9.10.1=jacocoAnt,spotbugs
org.ow2.asm:asm-tree:9.10.1=jacocoAnt,spotbugs
org.ow2.asm:asm-util:9.10.1=spotbugs
org.ow2.asm:asm:9.0=jmh,jmhCompileClasspath,jmhRuntimeClasspath
org.ow2.asm:asm:9.10.1=jacocoAnt,spotbugs
org.slf4j:jcl-over-slf4j:1.7.30=testCompileClasspath,testRuntimeClasspath
org.slf4j:jul-to-slf4j:1.7.30=testCompileClasspath,testRuntimeClasspath
org.slf4j:log4j-over-slf4j:1.7.30=testCompileClasspath,testRuntimeClasspath
org.slf4j:jcl-over-slf4j:1.7.30=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:jul-to-slf4j:1.7.30=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:log4j-over-slf4j:1.7.30=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:slf4j-api:2.0.17=spotbugsSlf4j
org.slf4j:slf4j-api:2.0.18=compileClasspath,runtimeClasspath,spotbugs,testCompileClasspath,testRuntimeClasspath
org.slf4j:slf4j-api:2.0.18=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,runtimeClasspath,spotbugs,testCompileClasspath,testRuntimeClasspath
org.slf4j:slf4j-simple:2.0.17=spotbugsSlf4j
org.spockframework:spock-bom:2.4-groovy-3.0=testCompileClasspath,testRuntimeClasspath
org.spockframework:spock-core:2.4-groovy-3.0=testCompileClasspath,testRuntimeClasspath
org.tabletest:tabletest-junit:1.2.2=testCompileClasspath,testRuntimeClasspath
org.tabletest:tabletest-parser:1.2.1=testCompileClasspath,testRuntimeClasspath
org.spockframework:spock-bom:2.4-groovy-3.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.spockframework:spock-core:2.4-groovy-3.0=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.tabletest:tabletest-junit:1.2.2=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.tabletest:tabletest-parser:1.2.1=jmhRuntimeClasspath,testCompileClasspath,testRuntimeClasspath
org.xmlresolver:xmlresolver:5.3.3=spotbugs
empty=annotationProcessor,spotbugsPlugins,testAnnotationProcessor
empty=annotationProcessor,jmhAnnotationProcessor,spotbugsPlugins,testAnnotationProcessor
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();
}
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Reject null elements before reserving queue capacity

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.

Suggested change
if (this.size.incrementAndGet() > this.capacity) {
if (element == null) {
throw new NullPointerException("element");
}
if (this.size.incrementAndGet() > this.capacity) {

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🤖 Bits Code Review · Open Bits AI session

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;
}
}
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;
}
}
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);
}
}
}
Loading
Loading