Search in sources :

Example 1 with DeterministicTraceSampler

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());
}
Also used : BaggageSpanProcessor(io.honeycomb.opentelemetry.sdk.trace.spanprocessors.BaggageSpanProcessor) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler)

Example 2 with DeterministicTraceSampler

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));
    }
}
Also used : SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) Test(org.junit.jupiter.api.Test)

Example 3 with DeterministicTraceSampler

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);
}
Also used : SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) Test(org.junit.jupiter.api.Test)

Example 4 with DeterministicTraceSampler

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);
    }
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) OpenTelemetry(io.opentelemetry.api.OpenTelemetry) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) Method(java.lang.reflect.Method) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Field(java.lang.reflect.Field) BatchSpanProcessor(io.opentelemetry.sdk.trace.export.BatchSpanProcessor) BaggageSpanProcessor(io.honeycomb.opentelemetry.sdk.trace.spanprocessors.BaggageSpanProcessor) SpanProcessor(io.opentelemetry.sdk.trace.SpanProcessor) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) List(java.util.List) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) Test(org.junit.jupiter.api.Test)

Example 5 with DeterministicTraceSampler

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());
    }
}
Also used : SamplingResult(io.opentelemetry.sdk.trace.samplers.SamplingResult) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) Test(org.junit.jupiter.api.Test)

Aggregations

DeterministicTraceSampler (io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler)6 Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)5 Test (org.junit.jupiter.api.Test)5 SamplingResult (io.opentelemetry.sdk.trace.samplers.SamplingResult)4 BaggageSpanProcessor (io.honeycomb.opentelemetry.sdk.trace.spanprocessors.BaggageSpanProcessor)2 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)1 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)1 Attributes (io.opentelemetry.api.common.Attributes)1 TracerProvider (io.opentelemetry.api.trace.TracerProvider)1 Resource (io.opentelemetry.sdk.resources.Resource)1 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)1 SpanProcessor (io.opentelemetry.sdk.trace.SpanProcessor)1 BatchSpanProcessor (io.opentelemetry.sdk.trace.export.BatchSpanProcessor)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1