use of io.opentelemetry.sdk.trace.samplers.Sampler in project honeycomb-opentelemetry-java by honeycombio.
the class DeterministicTraceSamplerTest method test_samplingResult_has_sampleRate_attribute.
@Test
public void test_samplingResult_has_sampleRate_attribute() {
for (int i = 0; i < 10; i++) {
Sampler sampler = new DeterministicTraceSampler(i);
String traceID = IdGenerator.random().generateTraceId();
SamplingResult result = sampler.shouldSample(Context.current(), traceID, "span", SpanKind.CLIENT, Attributes.empty(), Collections.emptyList());
Assertions.assertEquals(result.getAttributes(), Attributes.of(AttributeKey.longKey("SampleRate"), (long) i));
}
}
use of io.opentelemetry.sdk.trace.samplers.Sampler in project honeycomb-opentelemetry-java by honeycombio.
the class DeterministicTraceSamplerTest method test_deterministicSampler_varies_sampling_decision_based_on_trace_id.
@Test
public void test_deterministicSampler_varies_sampling_decision_based_on_trace_id() {
int count = 0;
for (int i = 0; i < 100; i++) {
Sampler sampler = new DeterministicTraceSampler(2);
String traceID = IdGenerator.random().generateTraceId();
SamplingResult result = sampler.shouldSample(Context.current(), traceID, "span", SpanKind.CLIENT, Attributes.empty(), Collections.emptyList());
if (result.getDecision() == SamplingDecision.RECORD_AND_SAMPLE) {
count++;
}
}
Assertions.assertTrue(count > 25 && count < 75);
}
use of io.opentelemetry.sdk.trace.samplers.Sampler in project opentelemetry-java by open-telemetry.
the class ConfigurableSamplerTest method configuration.
@Test
void configuration() {
ConfigProperties config = DefaultConfigProperties.createForTest(ImmutableMap.of("test.option", "true"));
Sampler sampler = TracerProviderConfiguration.configureSampler("testSampler", config, TracerProviderConfiguration.class.getClassLoader());
assertThat(sampler).isInstanceOfSatisfying(TestConfigurableSamplerProvider.TestSampler.class, s -> assertThat(s.getConfig()).isSameAs(config));
}
use of io.opentelemetry.sdk.trace.samplers.Sampler in project opentelemetry-java by open-telemetry.
the class JaegerRemoteSamplerProviderTest method serviceProvider.
@Test
void serviceProvider() {
ServiceLoader<ConfigurableSamplerProvider> samplerProviders = ServiceLoader.load(ConfigurableSamplerProvider.class);
assertThat(samplerProviders).hasAtLeastOneElementOfType(JaegerRemoteSamplerProvider.class);
assertThat(samplerProviders).singleElement(type(JaegerRemoteSamplerProvider.class)).satisfies(provider -> assertThat(provider.getName()).isEqualTo("jaeger_remote"));
ConfigProperties mockConfig = mock(ConfigProperties.class);
when(mockConfig.getString(JaegerRemoteSamplerProvider.SERVICE_NAME_PROPERTY)).thenReturn("test_service");
HashMap<String, String> samplerArgs = new HashMap<>();
samplerArgs.put("endpoint", "http://localhost:9999");
samplerArgs.put("pollingInterval", "99");
double samplingRate = 0.33;
samplerArgs.put("initialSamplingRate", String.valueOf(samplingRate));
when(mockConfig.getMap(JaegerRemoteSamplerProvider.SAMPLER_ARG_PROPERTY)).thenReturn(samplerArgs);
Sampler sampler = Sampler.parentBased(Sampler.traceIdRatioBased(samplingRate));
assertThat(samplerProviders).singleElement(type(JaegerRemoteSamplerProvider.class)).satisfies(provider -> {
try (JaegerRemoteSampler s = (JaegerRemoteSampler) provider.createSampler(mockConfig)) {
assertThat(s).extracting("sampler", type(Sampler.class)).asString().isEqualTo(sampler.toString());
assertThat(s).extracting("serviceName").isEqualTo("test_service");
assertThat(s).extracting("delegate").extracting("endpoint").isEqualTo("http://localhost:9999/jaeger.api_v2.SamplingManager/GetSamplingStrategy");
}
});
}
use of io.opentelemetry.sdk.trace.samplers.Sampler in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method addAttributes_OnlyViaSampler.
@Test
void addAttributes_OnlyViaSampler() {
Sampler sampler = new Sampler() {
@Override
public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks) {
return SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE, Attributes.builder().put("cat", "meow").build());
}
@Override
public String getDescription() {
return "test";
}
};
TracerProvider tracerProvider = SdkTracerProvider.builder().setSampler(sampler).build();
// Verify methods do not crash.
SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
span.end();
assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1);
assertThat(span.toSpanData().getAttributes().get(stringKey("cat"))).isEqualTo("meow");
}
Aggregations