use of io.opentelemetry.api.trace.TracerProvider 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");
}
use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method droppingAttributes.
@Test
void droppingAttributes() {
int maxNumberOfAttrs = 8;
SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfAttributes(maxNumberOfAttrs).build();
TracerProvider tracerProvider = SdkTracerProvider.builder().setSpanLimits(spanLimits).build();
// Verify methods do not crash.
SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
for (int i = 0; i < 2 * maxNumberOfAttrs; i++) {
spanBuilder.setAttribute("key" + i, i);
}
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
try {
Attributes attrs = span.toSpanData().getAttributes();
assertThat(attrs.size()).isEqualTo(maxNumberOfAttrs);
for (int i = 0; i < maxNumberOfAttrs; i++) {
assertThat(attrs.get(longKey("key" + i))).isEqualTo(i);
}
} finally {
span.end();
}
}
use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method linkAttributeLength.
@Test
void linkAttributeLength() {
int maxLength = 25;
TracerProvider tracerProvider = SdkTracerProvider.builder().setSpanLimits(SpanLimits.builder().setMaxAttributeValueLength(maxLength).build()).build();
SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
String strVal = IntStream.range(0, maxLength).mapToObj(i -> "a").collect(joining());
String tooLongStrVal = strVal + strVal;
Attributes attributes = Attributes.builder().put("string", tooLongStrVal).put("boolean", true).put("long", 1L).put("double", 1.0).put(stringArrayKey("stringArray"), Arrays.asList(strVal, tooLongStrVal)).put(booleanArrayKey("booleanArray"), Arrays.asList(true, false)).put(longArrayKey("longArray"), Arrays.asList(1L, 2L)).put(doubleArrayKey("doubleArray"), Arrays.asList(1.0, 2.0)).build();
spanBuilder.addLink(sampledSpanContext, attributes);
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
try {
attributes = span.toSpanData().getLinks().get(0).getAttributes();
assertThat(attributes.get(stringKey("string"))).isEqualTo(strVal);
assertThat(attributes.get(booleanKey("boolean"))).isEqualTo(true);
assertThat(attributes.get(longKey("long"))).isEqualTo(1L);
assertThat(attributes.get(doubleKey("double"))).isEqualTo(1.0);
assertThat(attributes.get(stringArrayKey("stringArray"))).isEqualTo(Arrays.asList(strVal, strVal));
assertThat(attributes.get(booleanArrayKey("booleanArray"))).isEqualTo(Arrays.asList(true, false));
assertThat(attributes.get(longArrayKey("longArray"))).isEqualTo(Arrays.asList(1L, 2L));
assertThat(attributes.get(doubleArrayKey("doubleArray"))).isEqualTo(Arrays.asList(1.0, 2.0));
} finally {
span.end();
}
}
use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method truncateLinkAttributes.
@Test
void truncateLinkAttributes() {
SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfAttributesPerLink(1).build();
TracerProvider tracerProvider = SdkTracerProvider.builder().setSpanLimits(spanLimits).build();
// Verify methods do not crash.
SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
Attributes attributes = Attributes.of(stringKey("key0"), "str", stringKey("key1"), "str", stringKey("key2"), "str");
spanBuilder.addLink(sampledSpanContext, attributes);
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
try {
assertThat(span.toSpanData().getLinks()).containsExactly(LinkData.create(sampledSpanContext, Attributes.of(stringKey("key0"), "str"), 3));
} finally {
span.end();
}
}
use of io.opentelemetry.api.trace.TracerProvider in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method truncateLink.
@Test
void truncateLink() {
int maxNumberOfLinks = 8;
SpanLimits spanLimits = SpanLimits.builder().setMaxNumberOfLinks(maxNumberOfLinks).build();
TracerProvider tracerProvider = SdkTracerProvider.builder().setSpanLimits(spanLimits).build();
// Verify methods do not crash.
SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
for (int i = 0; i < 2 * maxNumberOfLinks; i++) {
spanBuilder.addLink(sampledSpanContext);
}
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
try {
SpanData spanData = span.toSpanData();
List<LinkData> links = spanData.getLinks();
assertThat(links).hasSize(maxNumberOfLinks);
for (int i = 0; i < maxNumberOfLinks; i++) {
assertThat(links.get(i)).isEqualTo(LinkData.create(sampledSpanContext));
assertThat(spanData.getTotalRecordedLinks()).isEqualTo(2 * maxNumberOfLinks);
}
} finally {
span.end();
}
}
Aggregations