-
Notifications
You must be signed in to change notification settings - Fork 312
WIP: chore(sampling): track knuth sampling in distributed trace #9518
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
base: master
Are you sure you want to change the base?
Changes from all commits
9f7317e
b6e1df9
c6ef173
ad8190f
2ac621f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ | |
import datadog.trace.api.sampling.SamplingMechanism; | ||
import datadog.trace.common.writer.RemoteResponseListener; | ||
import datadog.trace.core.CoreSpan; | ||
import java.text.DecimalFormat; | ||
import java.text.DecimalFormatSymbols; | ||
import java.util.Collections; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.function.Function; | ||
|
@@ -22,8 +25,15 @@ public class RateByServiceTraceSampler implements Sampler, PrioritySampler, Remo | |
|
||
private static final Logger log = LoggerFactory.getLogger(RateByServiceTraceSampler.class); | ||
public static final String SAMPLING_AGENT_RATE = "_dd.agent_psr"; | ||
public static final String KNUTH_SAMPLING_RATE = "_dd.p.ksr"; | ||
|
||
private static final double DEFAULT_RATE = 1.0; | ||
private static final DecimalFormat DECIMAL_FORMAT; | ||
|
||
static { | ||
DECIMAL_FORMAT = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); | ||
DECIMAL_FORMAT.setMaximumFractionDigits(6); | ||
} | ||
|
||
private volatile RateSamplersByEnvAndService serviceRates = new RateSamplersByEnvAndService(); | ||
|
||
|
@@ -56,12 +66,21 @@ public <T extends CoreSpan<T>> void setSamplingPriority(final T span) { | |
sampler.getSampleRate(), | ||
SamplingMechanism.AGENT_RATE); | ||
} | ||
|
||
// Set Knuth sampling rate tag | ||
String ksrRate = formatKnuthSamplingRate(sampler.getSampleRate()); | ||
span.setTag(KNUTH_SAMPLING_RATE, ksrRate); | ||
} | ||
|
||
private <T extends CoreSpan<T>> String getSpanEnv(final T span) { | ||
return span.getTag("env", ""); | ||
} | ||
|
||
private String formatKnuthSamplingRate(double rate) { | ||
// Format to up to 6 decimal places, removing trailing zeros | ||
return DECIMAL_FORMAT.format(rate); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quick heads-up that But adding synchronization here would kill performance.... One option could be to write our own simple formatter, since we just want to truncate the value - for example, if we assume the value is between 0 and 1, a naive solution might be:
( you'd need to write tests to verify this behaves as expected - but you'd need to do that anyway :) Also note you don't need to do the formatting every time the sampling rate is applied because each In other words, the sample rate for each individual |
||
} | ||
|
||
@Override | ||
public void onResponse( | ||
final String endpoint, final Map<String, Map<String, Number>> responseJson) { | ||
|
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.
Should centralize this logic. Currently we define this function in two places