Search in sources :

Example 1 with Sampler

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));
    }
}
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 2 with Sampler

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);
}
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 Sampler

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

Example 4 with Sampler

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");
        }
    });
}
Also used : ConfigProperties(io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties) HashMap(java.util.HashMap) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) ConfigurableSamplerProvider(io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider) Test(org.junit.jupiter.api.Test)

Example 5 with Sampler

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");
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) Attributes(io.opentelemetry.api.common.Attributes) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) SpanKind(io.opentelemetry.api.trace.SpanKind) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Aggregations

Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)10 Test (org.junit.jupiter.api.Test)10 DeterministicTraceSampler (io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler)5 SamplingResult (io.opentelemetry.sdk.trace.samplers.SamplingResult)5 Attributes (io.opentelemetry.api.common.Attributes)3 Context (io.opentelemetry.context.Context)3 List (java.util.List)3 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)2 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)2 SpanContext (io.opentelemetry.api.trace.SpanContext)2 SpanKind (io.opentelemetry.api.trace.SpanKind)2 TracerProvider (io.opentelemetry.api.trace.TracerProvider)2 ConfigProperties (io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties)2 Resource (io.opentelemetry.sdk.resources.Resource)2 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)2 HashMap (java.util.HashMap)2 BaggageSpanProcessor (io.honeycomb.opentelemetry.sdk.trace.spanprocessors.BaggageSpanProcessor)1 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)1 Span (io.opentelemetry.api.trace.Span)1 SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)1