diff --git a/sdk-extensions/jaeger-remote-sampler/src/main/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSampler.java b/sdk-extensions/jaeger-remote-sampler/src/main/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSampler.java index f7ae501957e..6a6caa171e6 100644 --- a/sdk-extensions/jaeger-remote-sampler/src/main/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSampler.java +++ b/sdk-extensions/jaeger-remote-sampler/src/main/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSampler.java @@ -18,6 +18,8 @@ import io.opentelemetry.sdk.trace.samplers.Sampler; import io.opentelemetry.sdk.trace.samplers.SamplingDecision; import io.opentelemetry.sdk.trace.samplers.SamplingResult; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; import java.util.List; /** @@ -29,10 +31,10 @@ class RateLimitingSampler implements Sampler { static final AttributeKey SAMPLER_TYPE = stringKey("sampler.type"); static final AttributeKey SAMPLER_PARAM = doubleKey("sampler.param"); - private final double maxTracesPerSecond; private final RateLimiter rateLimiter; private final SamplingResult onSamplingResult; private final SamplingResult offSamplingResult; + private final String description; /** * Creates rate limiting sampler. @@ -40,13 +42,13 @@ class RateLimitingSampler implements Sampler { * @param maxTracesPerSecond the maximum number of sampled traces per second. */ RateLimitingSampler(int maxTracesPerSecond) { - this.maxTracesPerSecond = maxTracesPerSecond; double maxBalance = maxTracesPerSecond < 1.0 ? 1.0 : maxTracesPerSecond; this.rateLimiter = new RateLimiter(maxTracesPerSecond, maxBalance, Clock.getDefault()); Attributes attributes = Attributes.of(SAMPLER_TYPE, TYPE, SAMPLER_PARAM, (double) maxTracesPerSecond); this.onSamplingResult = SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE, attributes); this.offSamplingResult = SamplingResult.create(SamplingDecision.DROP, attributes); + description = "RateLimitingSampler{" + decimalFormat(maxTracesPerSecond) + "}"; } @Override @@ -62,11 +64,19 @@ public SamplingResult shouldSample( @Override public String getDescription() { - return String.format("RateLimitingSampler{%.2f}", maxTracesPerSecond); + return description; } @Override public String toString() { return getDescription(); } + + private static String decimalFormat(double value) { + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + decimalFormatSymbols.setDecimalSeparator('.'); + + DecimalFormat decimalFormat = new DecimalFormat("0.00", decimalFormatSymbols); + return decimalFormat.format(value); + } } diff --git a/sdk-extensions/jaeger-remote-sampler/src/test/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSamplerTest.java b/sdk-extensions/jaeger-remote-sampler/src/test/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSamplerTest.java index 873e4c5c0b4..a7300f9ccaa 100644 --- a/sdk-extensions/jaeger-remote-sampler/src/test/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSamplerTest.java +++ b/sdk-extensions/jaeger-remote-sampler/src/test/java/io/opentelemetry/sdk/extension/trace/jaeger/sampler/RateLimitingSamplerTest.java @@ -17,6 +17,7 @@ import io.opentelemetry.sdk.trace.samplers.SamplingDecision; import io.opentelemetry.sdk.trace.samplers.SamplingResult; import java.util.Collections; +import java.util.Locale; import org.junit.jupiter.api.Test; class RateLimitingSamplerTest { @@ -62,8 +63,14 @@ void sampleOneTrace() { } @Test - void description() { + void descriptionShouldBeLocaleIndependent() { RateLimitingSampler sampler = new RateLimitingSampler(15); + + // PL locale uses ',' as decimal separator + Locale.setDefault(Locale.forLanguageTag("PL")); + assertThat(sampler.getDescription()).isEqualTo("RateLimitingSampler{15.00}"); + + Locale.setDefault(Locale.ENGLISH); assertThat(sampler.getDescription()).isEqualTo("RateLimitingSampler{15.00}"); } } diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/samplers/TraceIdRatioBasedSampler.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/samplers/TraceIdRatioBasedSampler.java index b84dd374f39..59a63856318 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/samplers/TraceIdRatioBasedSampler.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/samplers/TraceIdRatioBasedSampler.java @@ -10,6 +10,8 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.trace.data.LinkData; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; import java.util.List; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -52,7 +54,7 @@ static TraceIdRatioBasedSampler create(double ratio) { TraceIdRatioBasedSampler(double ratio, long idUpperBound) { this.idUpperBound = idUpperBound; - description = String.format("TraceIdRatioBased{%.6f}", ratio); + description = "TraceIdRatioBased{" + decimalFormat(ratio) + "}"; } @Override @@ -108,4 +110,12 @@ long getIdUpperBound() { private static long getTraceIdRandomPart(String traceId) { return OtelEncodingUtils.longFromBase16String(traceId, 16); } + + private static String decimalFormat(double value) { + DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); + decimalFormatSymbols.setDecimalSeparator('.'); + + DecimalFormat decimalFormat = new DecimalFormat("0.000000", decimalFormatSymbols); + return decimalFormat.format(value); + } } diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/samplers/TraceIdRatioBasedSamplerTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/samplers/TraceIdRatioBasedSamplerTest.java index 0c3a160c75a..a0279fa8613 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/samplers/TraceIdRatioBasedSamplerTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/samplers/TraceIdRatioBasedSamplerTest.java @@ -19,6 +19,7 @@ import io.opentelemetry.sdk.trace.data.LinkData; import java.util.Collections; import java.util.List; +import java.util.Locale; import org.junit.jupiter.api.Test; class TraceIdRatioBasedSamplerTest { @@ -67,9 +68,15 @@ void outOfRangeLowProbability() { } @Test - void getDescription() { - assertThat(Sampler.traceIdRatioBased(0.5).getDescription()) - .isEqualTo(String.format("TraceIdRatioBased{%.6f}", 0.5)); + void descriptionShouldBeLocaleIndependent() { + Sampler sampler = Sampler.traceIdRatioBased(0.5); + + // PL locale uses ',' as decimal separator + Locale.setDefault(Locale.forLanguageTag("PL")); + assertThat(sampler.getDescription()).isEqualTo("TraceIdRatioBased{0.500000}"); + + Locale.setDefault(Locale.ENGLISH); + assertThat(sampler.getDescription()).isEqualTo("TraceIdRatioBased{0.500000}"); } @Test