This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new f5755bf870 [improve] Improve logging sample rate validation (#7834)
f5755bf870 is described below
commit f5755bf8700431560ea18aa340218e8d9443c49b
Author: Duansg <[email protected]>
AuthorDate: Sun Oct 18 18:26:27 2026 -0700
[improve] Improve logging sample rate validation (#7834)
---
.../logging/common/sampler/CountSampler.java | 10 ++---
.../logging/common/sampler/CountSamplerTest.java | 44 ++++++++++++++++++++++
2 files changed, 49 insertions(+), 5 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/sampler/CountSampler.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/sampler/CountSampler.java
index 61072c799b..a68e8e0700 100644
---
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/sampler/CountSampler.java
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/sampler/CountSampler.java
@@ -101,16 +101,16 @@ public class CountSampler implements Sampler {
if (StringUtils.isBlank(probability)) {
return ALWAYS_SAMPLE;
}
- if ("0".equals(probability)) {
- return NEVER_SAMPLE;
- }
if ("1".equals(probability) || "1.0".equals(probability) ||
"1.0.0".equals(probability)) {
return ALWAYS_SAMPLE;
}
float parseProbability = NumberUtils.toFloat(probability, 1);
- if (parseProbability < 0.01f || parseProbability > 1) {
+ if (parseProbability == 0) {
+ return NEVER_SAMPLE;
+ }
+ if (!Float.isFinite(parseProbability) || parseProbability < 0.01f ||
parseProbability > 1) {
throw new IllegalArgumentException(
- "probability should be between 0.01 and 1: was " +
probability);
+ "probability should be 0 or between 0.01 and 1: was " +
probability);
}
return new CountSampler(parseProbability);
}
diff --git
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/test/java/org/apache/shenyu/plugin/logging/common/sampler/CountSamplerTest.java
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/test/java/org/apache/shenyu/plugin/logging/common/sampler/CountSamplerTest.java
index 993c2ba91e..2fb407db80 100644
---
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/test/java/org/apache/shenyu/plugin/logging/common/sampler/CountSamplerTest.java
+++
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/test/java/org/apache/shenyu/plugin/logging/common/sampler/CountSamplerTest.java
@@ -22,6 +22,10 @@ import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.NullAndEmptySource;
+import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
@@ -82,4 +86,44 @@ public class CountSamplerTest {
Assertions.assertEquals(CountSampler.create("1"),
Sampler.ALWAYS_SAMPLE);
Assertions.assertEquals(CountSampler.create("0.5").getClass(),
CountSampler.class);
}
+
+ @ParameterizedTest
+ @ValueSource(strings = {"0", "0.0", "0.00", "-0.0", "0e0", " 0.0 "})
+ public void testCreateWithZeroProbability(final String probability) {
+ Sampler sampler = CountSampler.create(probability);
+ for (int i = 0; i < 100; i++) {
+ Assertions.assertFalse(sampler.isSampled(null, null));
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"NaN", "Infinity", "-Infinity", "-0.01", "0.001",
"1.01"})
+ public void testCreateWithInvalidProbability(final String probability) {
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
CountSampler.create(probability));
+ }
+
+ @ParameterizedTest
+ @CsvSource({"0.01, 1", "0.5, 50", "1, 100"})
+ public void testSamplingCountPerCycle(final String probability, final int
expectedCount) {
+ Sampler sampler = CountSampler.create(probability);
+ for (int cycle = 0; cycle < 2; cycle++) {
+ int sampledCount = 0;
+ for (int i = 0; i < 100; i++) {
+ if (sampler.isSampled(null, null)) {
+ sampledCount++;
+ }
+ }
+ Assertions.assertEquals(expectedCount, sampledCount);
+ }
+ }
+
+ @ParameterizedTest
+ @NullAndEmptySource
+ @ValueSource(strings = {" ", "1", "1.0", "1.0.0", "invalid"})
+ public void testCreateWithDefaultOrFullProbability(final String
probability) {
+ Sampler sampler = CountSampler.create(probability);
+ for (int i = 0; i < 100; i++) {
+ Assertions.assertTrue(sampler.isSampled(null, null));
+ }
+ }
}