use of io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler in project honeycomb-opentelemetry-java by honeycombio.
the class HoneycombSdkTracerProviderConfigurer method configure.
@Override
public void configure(SdkTracerProviderBuilder tracerProvider, ConfigProperties config) {
int sampleRate;
try {
sampleRate = EnvironmentConfiguration.getSampleRate();
} catch (NumberFormatException e) {
System.err.println("WARN: Sample rate provided is not an integer, using default sample rate of 1");
sampleRate = 1;
}
tracerProvider.setSampler(new DeterministicTraceSampler(sampleRate)).addSpanProcessor(new BaggageSpanProcessor());
}
use of io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler 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.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler 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.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler in project honeycomb-opentelemetry-java by honeycombio.
the class OpenTelemetryConfigurationTest method testConfiguration_tracerSettings.
@Test
@SuppressWarnings("unchecked")
public void testConfiguration_tracerSettings() {
Sampler sampler = new DeterministicTraceSampler(5);
OpenTelemetry openTelemetry = OpenTelemetryConfiguration.builder().setSampler(sampler).setApiKey("foobar").setDataset("dataset").addResourceAttribute("str", "str").addResourceAttribute("bool", true).addResourceAttribute("int", 123).addResourceAttribute("str-array", "str1", "str2", "str3").build();
TracerProvider provider = openTelemetry.getTracerProvider();
try {
Method m = provider.getClass().getDeclaredMethod("unobfuscate");
m.setAccessible(true);
SdkTracerProvider sdkProvider = (SdkTracerProvider) m.invoke(provider, (Object[]) null);
// verify sampler
Sampler s = sdkProvider.getSampler();
Assertions.assertEquals(sampler, s);
Field providerField = pluckField(sdkProvider.getClass(), "sharedState");
Object sharedState = providerField.get(sdkProvider);
Field resourceField = pluckField(sharedState.getClass(), "resource");
Resource r = (Resource) resourceField.get(sharedState);
Attributes attrs = r.getAttributes();
// verify resource attributes
Assertions.assertEquals(DistroMetadata.VERSION_VALUE, attrs.get(AttributeKey.stringKey(DistroMetadata.VERSION_FIELD)));
Assertions.assertEquals(DistroMetadata.RUNTIME_VERSION_VALUE, attrs.get(AttributeKey.stringKey(DistroMetadata.RUNTIME_VERSION_FIELD)));
Assertions.assertEquals("str", attrs.get(AttributeKey.stringKey("str")));
Assertions.assertEquals(true, attrs.get(AttributeKey.booleanKey("bool")));
Assertions.assertEquals(123, attrs.get(AttributeKey.longKey("int")));
Assertions.assertEquals(Arrays.asList("str1", "str2", "str3"), attrs.get(AttributeKey.stringArrayKey("str-array")));
Field procesorField = pluckField(sharedState.getClass(), "activeSpanProcessor");
Object sp = procesorField.get(sharedState);
Field allSpanProcessors = pluckField(sp.getClass(), "spanProcessorsAll");
List<SpanProcessor> processors = (List<SpanProcessor>) allSpanProcessors.get(sp);
// verify configured span processors
Assertions.assertInstanceOf(BaggageSpanProcessor.class, processors.get(0));
Assertions.assertInstanceOf(BatchSpanProcessor.class, processors.get(1));
} catch (Exception e) {
Assertions.fail(e);
}
}
use of io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler in project honeycomb-opentelemetry-java by honeycombio.
the class DeterministicTraceSamplerTest method test_neverSample.
@Test
public void test_neverSample() {
int sampleRate = 0;
Sampler sampler = new DeterministicTraceSampler(sampleRate);
for (int i = 0; i < 100; i++) {
String traceID = IdGenerator.random().generateTraceId();
SamplingResult result = sampler.shouldSample(Context.current(), traceID, "span", SpanKind.CLIENT, Attributes.empty(), Collections.emptyList());
Assertions.assertEquals(SamplingDecision.DROP, result.getDecision());
}
}
Aggregations