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

Make description of Samplers Locale independent #4887

Merged
merged 3 commits into from
Oct 31, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -62,11 +64,19 @@ public SamplingResult shouldSample(

@Override
public String getDescription() {
return String.format("RateLimitingSampler{%.2f}", maxTracesPerSecond);
return String.format("RateLimitingSampler{%s}", decimalFormat(maxTracesPerSecond));
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,7 +54,7 @@ static TraceIdRatioBasedSampler create(double ratio) {

TraceIdRatioBasedSampler(double ratio, long idUpperBound) {
this.idUpperBound = idUpperBound;
description = String.format("TraceIdRatioBased{%.6f}", ratio);
description = String.format("TraceIdRatioBased{%s}", decimalFormat(ratio));
}

@Override
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down