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 3 commits
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 @@ -76,6 +76,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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

@UseAgent("applicationinsights-no-sampling.json")
abstract class NoSamplingTest {
@UseAgent("applicationinsights-ingestion-sampling.json")
abstract class IngestionSamplingTest {

@RegisterExtension static final SmokeTestExtension testing = SmokeTestExtension.create();

Expand All @@ -48,40 +48,40 @@ void testNoSampling() throws Exception {
assertThat(messageEnvelopes.size()).isEqualTo(1000);

for (Envelope requestEnvelope : requestEnvelopes) {
assertThat(requestEnvelope.getSampleRate()).isEqualTo(100.0f);
assertThat(requestEnvelope.getSampleRate()).isNull();
}
for (Envelope eventEnvelope : eventEnvelopes) {
assertThat(eventEnvelope.getSampleRate()).isEqualTo(100.0f);
assertThat(eventEnvelope.getSampleRate()).isNull();
}
for (Envelope messageEnvelope : messageEnvelopes) {
assertThat(messageEnvelope.getSampleRate()).isEqualTo(100.0f);
assertThat(messageEnvelope.getSampleRate()).isNull();
}
}

@Environment(TOMCAT_8_JAVA_8)
static class Tomcat8Java8Test extends NoSamplingTest {}
static class Tomcat8Java8Test extends IngestionSamplingTest {}

@Environment(TOMCAT_8_JAVA_8_OPENJ9)
static class Tomcat8Java8OpenJ9Test extends NoSamplingTest {}
static class Tomcat8Java8OpenJ9Test extends IngestionSamplingTest {}

@Environment(TOMCAT_8_JAVA_11)
static class Tomcat8Java11Test extends NoSamplingTest {}
static class Tomcat8Java11Test extends IngestionSamplingTest {}

@Environment(TOMCAT_8_JAVA_11_OPENJ9)
static class Tomcat8Java11OpenJ9Test extends NoSamplingTest {}
static class Tomcat8Java11OpenJ9Test extends IngestionSamplingTest {}

@Environment(TOMCAT_8_JAVA_17)
static class Tomcat8Java17Test extends NoSamplingTest {}
static class Tomcat8Java17Test extends IngestionSamplingTest {}

@Environment(TOMCAT_8_JAVA_19)
static class Tomcat8Java19Test extends NoSamplingTest {}
static class Tomcat8Java19Test extends IngestionSamplingTest {}

@Environment(TOMCAT_8_JAVA_20)
static class Tomcat8Java20Test extends NoSamplingTest {}
static class Tomcat8Java20Test extends IngestionSamplingTest {}

@Environment(WILDFLY_13_JAVA_8)
static class Wildfly13Java8Test extends NoSamplingTest {}
static class Wildfly13Java8Test extends IngestionSamplingTest {}

@Environment(WILDFLY_13_JAVA_8_OPENJ9)
static class Wildfly13Java8OpenJ9Test extends NoSamplingTest {}
static class Wildfly13Java8OpenJ9Test extends IngestionSamplingTest {}
}