Skip to content
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

Fix ingestion sampling #3578

Merged
merged 8 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,10 @@ private static void track(
}

if (isPartOfTheCurrentTrace && applySampling && span instanceof ReadableSpan) {
long itemCount = getItemCount((ReadableSpan) span);
telemetryBuilder.setSampleRate(100.0f / itemCount);
Long itemCount = ((ReadableSpan) span).getAttribute(AiSemanticAttributes.ITEM_COUNT);
if (itemCount != null) {
telemetryBuilder.setSampleRate(100.0f / itemCount);
}
}

if (!isPartOfTheCurrentTrace && applySampling) {
Expand Down Expand Up @@ -574,11 +576,6 @@ private static boolean sample(String operationId, double samplingPercentage) {
return SamplingScoreGeneratorV2.getSamplingScore(operationId) < samplingPercentage;
}

private static long getItemCount(ReadableSpan span) {
Long itemCount = span.getAttribute(AiSemanticAttributes.ITEM_COUNT);
return itemCount == null ? 1L : itemCount;
}

private static void selectivelySetTags(
AbstractTelemetryBuilder telemetryBuilder, Map<String, String> sourceTags) {
for (Map.Entry<String, String> entry : sourceTags.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private static boolean isEmpty(@Nullable String str) {
public void validate() {
instrumentation.logging.getSeverityThreshold();
authentication.validate();
sampling.validate();
preview.validate();
}

Expand Down Expand Up @@ -168,6 +169,12 @@ public static class Sampling {
@Deprecated @Nullable public Double limitPerSecond;

public List<SamplingOverride> overrides = new ArrayList<>();

private void validate() {
for (SamplingOverride samplingOverride : overrides) {
samplingOverride.validate();
}
}
trask marked this conversation as resolved.
Show resolved Hide resolved
}

public static class SamplingPreview {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ private static void logConfigurationWarnings(Configuration config) {
configurationLogger.warn(
"\"clientsecret\" typed of AAD authentication has been deprecated since 3.5.0 GA. Please use \"user-assigned identity\" or \"system-assigned identity\" instead.");
}
if (config.sampling.percentage != null && config.sampling.requestsPerSecond != null) {
configurationLogger.warn(
"Sampling \"requestsPerSecond\" and \"percentage\" should not be used at the same time."
+ " Please remove one of them.");
config.sampling.percentage = null; // requestsPerSecond takes priority
}
trask marked this conversation as resolved.
Show resolved Hide resolved

logWarningIfUsingInternalAttributes(config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.List;
import javax.annotation.Nullable;

import static com.microsoft.applicationinsights.agent.internal.sampling.SamplingPercentage.USE_INGESTION_SAMPLING;

// this sampler does two things:
// * implements same trace id hashing algorithm so that traces are sampled the same across multiple
// nodes when some of those nodes are being monitored by other Application Insights SDKs (and 2.x
Expand Down Expand Up @@ -76,6 +78,10 @@ public SamplingResult shouldSample(
: parentlessDependencySamplingPercentage.get();
}

if (sp == SamplingPercentage.USE_INGESTION_SAMPLING) {
return SamplingResult.recordAndSample();
}

if (sp == 0) {
return SamplingResult.drop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public static Sampler getSampler(
SamplingPercentage parentlessDependencySamplingPercentage = SamplingPercentage.fixed(100);
sampler = new AiSampler(requestSamplingPercentage, parentlessDependencySamplingPercentage);
} else if (sampling.percentage != null) {
SamplingPercentage samplingPercentage = SamplingPercentage.fixed(sampling.percentage);
SamplingPercentage samplingPercentage;
if (sampling.percentage == 100) {
samplingPercentage = SamplingPercentage.ingestion();
} else {
samplingPercentage = SamplingPercentage.fixed(sampling.percentage);
}
sampler = new AiSampler(samplingPercentage, samplingPercentage);
} else {
throw new AssertionError("ConfigurationBuilder should have set the default sampling");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// the portal
public interface SamplingPercentage {

long USE_INGESTION_SAMPLING = -1;

double get();

static SamplingPercentage fixed(double percentage) {
Expand All @@ -22,4 +24,8 @@ static SamplingPercentage fixed(double percentage) {
static SamplingPercentage rateLimited(double targetPerSecondLimit) {
return new RateLimitedSamplingPercentage(targetPerSecondLimit, 0.1);
}

static SamplingPercentage ingestion() {
trask marked this conversation as resolved.
Show resolved Hide resolved
return () -> USE_INGESTION_SAMPLING;
trask marked this conversation as resolved.
Show resolved Hide resolved
}
}