-
Notifications
You must be signed in to change notification settings - Fork 355
Add head-based sampling for LLM Observability traces [MLOB-7815] #12277
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
Draft
ncybul
wants to merge
2
commits into
master
Choose a base branch
from
nicole.cybul/llmobs-java-head-based-sampling
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
93 changes: 93 additions & 0 deletions
93
dd-java-agent/agent-llmobs/src/main/java/datadog/trace/llmobs/domain/LLMObsSampler.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,93 @@ | ||
| package datadog.trace.llmobs.domain; | ||
|
|
||
| import datadog.trace.api.Config; | ||
|
|
||
| /** | ||
| * Head-based retention sampler for LLM Observability traces. | ||
| * | ||
| * <p>Duplicates the arithmetic in {@code DeterministicSampler} because {@code agent-llmobs} does | ||
| * not depend on {@code dd-trace-core}; {@code ApiSecurityDownstreamSamplerImpl} does the same. | ||
| */ | ||
|
ncybul marked this conversation as resolved.
|
||
| public final class LLMObsSampler { | ||
|
|
||
| private static final long KNUTH_FACTOR = 1111111111111111111L; | ||
| private static final double MAX = Math.pow(2, 64) - 1; | ||
|
|
||
| private final double rate; | ||
| private final long threshold; | ||
| private final String formattedRate; | ||
|
|
||
| public static LLMObsSampler fromConfig() { | ||
| return new LLMObsSampler(Config.get().getLlmObsSampleRate()); | ||
| } | ||
|
|
||
| public LLMObsSampler(final double rate) { | ||
| // NaN is clamped to 1.0 rather than left alone: every comparison against it is false, which | ||
| // would otherwise leave the cutoff and the reported rate disagreeing about what happened. | ||
| final double bounded = Double.isNaN(rate) ? 1.0 : rate; | ||
| this.rate = bounded < 0.0 ? 0.0 : (bounded > 1.0 ? 1.0 : bounded); | ||
| this.threshold = cutoff(this.rate); | ||
| this.formattedRate = formatRate(this.rate); | ||
| } | ||
|
|
||
| /** | ||
| * The configured rate, formatted for the wire. Derived from the configured {@code double} rather | ||
| * than from a narrowed {@code float}, so the reported rate is the rate actually applied. | ||
| */ | ||
| public String formattedRate() { | ||
| return formattedRate; | ||
| } | ||
|
|
||
| /** | ||
| * @param samplingId the low-order 64 bits of the APM trace ID. | ||
| * @return whether the trace is retained. | ||
| */ | ||
| public boolean sample(final long samplingId) { | ||
| // unsigned 64 bit comparison with cutoff/threshold | ||
| return samplingId * KNUTH_FACTOR + Long.MIN_VALUE <= threshold; | ||
| } | ||
|
|
||
| private static long cutoff(final double rate) { | ||
| if (rate < 0.5) { | ||
| return (long) (rate * MAX) + Long.MIN_VALUE; | ||
| } | ||
| if (rate < 1.0) { | ||
| return (long) ((rate * MAX) + Long.MIN_VALUE); | ||
| } | ||
| return Long.MAX_VALUE; | ||
| } | ||
|
|
||
| /** | ||
| * Formats a sampling rate with up to 6 decimal digits of precision and no trailing zeros. Mirrors | ||
| * {@code format_rate} in dd-trace-py, which stamps {@code sample_rate} through the same helper it | ||
| * uses for {@code _dd.p.ksr}, so the two languages report the same rate as the same string. | ||
| */ | ||
| static String formatRate(final double rate) { | ||
|
ncybul marked this conversation as resolved.
|
||
| if (rate <= 0.0) { | ||
| return "0"; | ||
| } | ||
| if (rate >= 1.0) { | ||
| return "1"; | ||
| } | ||
| long rounded = Math.round(rate * 1_000_000L); | ||
| if (rounded <= 0) { | ||
| return "0"; | ||
| } | ||
| if (rounded >= 1_000_000L) { | ||
| return "1"; | ||
| } | ||
| // Build "0.DDDDDD", then trim trailing zeros. | ||
| char[] chars = new char[8]; | ||
| chars[0] = '0'; | ||
| chars[1] = '.'; | ||
| for (int i = 7; i >= 2; i--) { | ||
| chars[i] = (char) ('0' + (rounded % 10)); | ||
| rounded /= 10; | ||
| } | ||
| int end = 8; | ||
| while (chars[end - 1] == '0') { | ||
| end--; | ||
| } | ||
| return new String(chars, 0, end); | ||
| } | ||
| } | ||
133 changes: 133 additions & 0 deletions
133
...gent/agent-llmobs/src/test/java/datadog/trace/llmobs/domain/DDLLMObsSpanSamplingTest.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,133 @@ | ||
| package datadog.trace.llmobs.domain; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import datadog.trace.agent.tooling.TracerInstaller; | ||
| import datadog.trace.api.WellKnownTags; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentScope; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
| import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
| import datadog.trace.bootstrap.instrumentation.api.Tags; | ||
| import datadog.trace.core.CoreTracer; | ||
| import java.lang.reflect.Field; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Covers where the head-based sampling decision is made and where it is inherited: the decision is | ||
| * computed once, at the root of an LLMObs trace, and every descendant reports it verbatim. | ||
| */ | ||
| class DDLLMObsSpanSamplingTest { | ||
| private static final String SAMPLE_RATE_TAG = "_ml_obs_tag.sample_rate"; | ||
| private static final String SAMPLING_DECISION_TAG = "_ml_obs_tag.sampling_decision"; | ||
|
|
||
| private static final Field SPAN_FIELD; | ||
|
|
||
| private static CoreTracer tracer; | ||
|
|
||
| static { | ||
| try { | ||
| SPAN_FIELD = DDLLMObsSpan.class.getDeclaredField("span"); | ||
| SPAN_FIELD.setAccessible(true); | ||
| } catch (ReflectiveOperationException error) { | ||
| throw new ExceptionInInitializerError(error); | ||
| } | ||
| } | ||
|
|
||
| @BeforeAll | ||
| static void installTracer() { | ||
| tracer = CoreTracer.builder().build(); | ||
| TracerInstaller.forceInstallGlobalTracer(tracer); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void closeTracer() { | ||
| TracerInstaller.forceInstallGlobalTracer(null); | ||
| tracer.close(); | ||
| } | ||
|
|
||
| @Test | ||
| void stampsRetainedDecisionAtTheDefaultRate() throws IllegalAccessException { | ||
| // The fields are stamped at every rate, including 1.0, matching dd-trace-py. | ||
| DDLLMObsSpan llmObsSpan = newSpan(new LLMObsSampler(1.0)); | ||
| try { | ||
| AgentSpan span = spanOf(llmObsSpan); | ||
| assertEquals("1", span.getTag(SAMPLING_DECISION_TAG)); | ||
| assertEquals("1", span.getTag(SAMPLE_RATE_TAG)); | ||
| } finally { | ||
| llmObsSpan.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void stampsDroppedDecisionOnRoot() throws IllegalAccessException { | ||
| DDLLMObsSpan llmObsSpan = newSpan(new LLMObsSampler(0.0)); | ||
| try { | ||
| AgentSpan span = spanOf(llmObsSpan); | ||
| assertEquals("0", span.getTag(SAMPLING_DECISION_TAG)); | ||
| assertEquals("0", span.getTag(SAMPLE_RATE_TAG)); | ||
| } finally { | ||
| llmObsSpan.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void childInheritsTheRootDecisionInsteadOfRecomputingIt() throws IllegalAccessException { | ||
| // The child's sampler drops everything. If the decision were recomputed per span, the child | ||
| // would report "0" and the trace would be torn in half at the intake. | ||
| DDLLMObsSpan root = newSpan(new LLMObsSampler(1.0)); | ||
| try { | ||
| AgentSpan rootSpan = spanOf(root); | ||
| // Inheritance is gated on the two spans sharing an APM trace, so the root's APM span has to | ||
| // be active for the child to be started under it. | ||
| try (AgentScope ignored = AgentTracer.activateSpan(rootSpan)) { | ||
| DDLLMObsSpan child = newSpan(new LLMObsSampler(0.0)); | ||
| try { | ||
| AgentSpan childSpan = spanOf(child); | ||
| assertEquals("1", childSpan.getTag(SAMPLING_DECISION_TAG)); | ||
| assertEquals( | ||
| rootSpan.getTag(SAMPLE_RATE_TAG), | ||
| childSpan.getTag(SAMPLE_RATE_TAG), | ||
| "every span in a trace must report the rate the decision was made at"); | ||
| } finally { | ||
| child.finish(); | ||
| } | ||
| } | ||
| } finally { | ||
| root.finish(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void childOfADroppedRootStaysDropped() throws IllegalAccessException { | ||
| // Symmetric case. Because a decision is stamped at every rate, "the context carries a decision" | ||
| // is an unambiguous signal, so a child never mistakes an inherited drop for being a root. | ||
| DDLLMObsSpan root = newSpan(new LLMObsSampler(0.0)); | ||
| try { | ||
| try (AgentScope ignored = AgentTracer.activateSpan(spanOf(root))) { | ||
| DDLLMObsSpan child = newSpan(new LLMObsSampler(1.0)); | ||
| try { | ||
| AgentSpan childSpan = spanOf(child); | ||
| assertEquals("0", childSpan.getTag(SAMPLING_DECISION_TAG)); | ||
| assertEquals("0", childSpan.getTag(SAMPLE_RATE_TAG)); | ||
| } finally { | ||
| child.finish(); | ||
| } | ||
| } | ||
| } finally { | ||
| root.finish(); | ||
| } | ||
| } | ||
|
|
||
| private static DDLLMObsSpan newSpan(LLMObsSampler sampler) { | ||
| WellKnownTags tags = | ||
| new WellKnownTags("runtime-id", "hostname", "test", "service", "version", "java"); | ||
| return new DDLLMObsSpan( | ||
| Tags.LLMOBS_LLM_SPAN_KIND, "span", "ml-app", null, "service", tags, sampler); | ||
| } | ||
|
|
||
| private static AgentSpan spanOf(DDLLMObsSpan llmObsSpan) throws IllegalAccessException { | ||
| return (AgentSpan) SPAN_FIELD.get(llmObsSpan); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.