use of io.opentelemetry.sdk.trace.samplers.SamplingResult 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.SamplingResult 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.SamplingResult in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method sampler_updatedTraceState.
@Test
void sampler_updatedTraceState() {
String samplerAttributeName = "sampler-attribute";
AttributeKey<String> samplerAttributeKey = stringKey(samplerAttributeName);
SdkSpan span = (SdkSpan) SdkTracerProvider.builder().setSampler(new Sampler() {
@Override
public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks) {
return new SamplingResult() {
@Override
public SamplingDecision getDecision() {
return SamplingDecision.RECORD_AND_SAMPLE;
}
@Override
public Attributes getAttributes() {
return Attributes.empty();
}
@Override
public TraceState getUpdatedTraceState(TraceState parentTraceState) {
return parentTraceState.toBuilder().put("newkey", "newValue").build();
}
};
}
@Override
public String getDescription() {
return "test sampler";
}
}).build().get("test").spanBuilder(SPAN_NAME).setAttribute(samplerAttributeKey, "none").startSpan();
try {
assertThat(span.getSpanContext().isSampled()).isTrue();
assertThat(span.toSpanData().getAttributes().get(samplerAttributeKey)).isNotNull();
assertThat(span.toSpanData().getSpanContext().getTraceState()).isEqualTo(TraceState.builder().put("newkey", "newValue").build());
} finally {
span.end();
}
}
use of io.opentelemetry.sdk.trace.samplers.SamplingResult in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilder method startSpan.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Span startSpan() {
Context parentContext = parent == null ? Context.current() : parent;
Span parentSpan = Span.fromContext(parentContext);
SpanContext parentSpanContext = parentSpan.getSpanContext();
String traceId;
IdGenerator idGenerator = tracerSharedState.getIdGenerator();
String spanId = idGenerator.generateSpanId();
if (!parentSpanContext.isValid()) {
// New root span.
traceId = idGenerator.generateTraceId();
} else {
// New child span.
traceId = parentSpanContext.getTraceId();
}
List<LinkData> immutableLinks = links == null ? Collections.emptyList() : Collections.unmodifiableList(links);
// Avoid any possibility to modify the links list by adding links to the Builder after the
// startSpan is called. If that happens all the links will be added in a new list.
links = null;
Attributes immutableAttributes = attributes == null ? Attributes.empty() : attributes;
SamplingResult samplingResult = tracerSharedState.getSampler().shouldSample(parentContext, traceId, spanName, spanKind, immutableAttributes, immutableLinks);
SamplingDecision samplingDecision = samplingResult.getDecision();
TraceState samplingResultTraceState = samplingResult.getUpdatedTraceState(parentSpanContext.getTraceState());
SpanContext spanContext = ImmutableSpanContext.create(traceId, spanId, isSampled(samplingDecision) ? TraceFlags.getSampled() : TraceFlags.getDefault(), samplingResultTraceState, /* remote= */
false, tracerSharedState.isIdGeneratorSafeToSkipIdValidation());
if (!isRecording(samplingDecision)) {
return Span.wrap(spanContext);
}
Attributes samplingAttributes = samplingResult.getAttributes();
if (!samplingAttributes.isEmpty()) {
samplingAttributes.forEach((key, value) -> attributes().put((AttributeKey) key, value));
}
// Avoid any possibility to modify the attributes by adding attributes to the Builder after the
// startSpan is called. If that happens all the attributes will be added in a new map.
AttributesMap recordedAttributes = attributes;
attributes = null;
return SdkSpan.startSpan(spanContext, spanName, instrumentationLibraryInfo, spanKind, parentSpan, parentContext, spanLimits, tracerSharedState.getActiveSpanProcessor(), tracerSharedState.getClock(), tracerSharedState.getResource(), recordedAttributes, immutableLinks, totalNumberOfLinksAdded, startEpochNanos);
}
use of io.opentelemetry.sdk.trace.samplers.SamplingResult in project opentelemetry-java by open-telemetry.
the class RateLimitingSamplerTest method sampleOneTrace.
@Test
void sampleOneTrace() {
RateLimitingSampler sampler = new RateLimitingSampler(1);
SamplingResult samplingResult = sampler.shouldSample(spanContext, TRACE_ID, SPAN_NAME, SPAN_KIND, Attributes.empty(), Collections.emptyList());
assertThat(samplingResult.getDecision()).isEqualTo(SamplingDecision.RECORD_AND_SAMPLE);
assertThat(sampler.shouldSample(spanContext, TRACE_ID, SPAN_NAME, SPAN_KIND, Attributes.empty(), Collections.emptyList()).getDecision()).isEqualTo(SamplingDecision.DROP);
assertThat(samplingResult.getAttributes().size()).isEqualTo(2);
assertThat(samplingResult.getAttributes().get(RateLimitingSampler.SAMPLER_PARAM)).isEqualTo(1d);
assertThat(samplingResult.getAttributes().get(RateLimitingSampler.SAMPLER_TYPE)).isEqualTo(RateLimitingSampler.TYPE);
}
Aggregations