use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.
the class SdkSpanTest method setAllAttributes.
@Test
void setAllAttributes() {
SdkSpan span = createTestRootSpan();
Attributes attributes = Attributes.builder().put("StringKey", "StringVal").put("NullStringKey", (String) null).put("EmptyStringKey", "").put(stringKey("NullStringAttributeValue"), null).put(stringKey("EmptyStringAttributeValue"), "").put("LongKey", 1000L).put(longKey("LongKey2"), 5).put(longKey("LongKey3"), 6L).put("DoubleKey", 10.0).put("BooleanKey", false).put(stringArrayKey("ArrayStringKey"), Arrays.asList("StringVal", null, "", "StringVal2")).put(longArrayKey("ArrayLongKey"), Arrays.asList(1L, 2L, 3L, 4L, 5L)).put(doubleArrayKey("ArrayDoubleKey"), Arrays.asList(0.1, 2.3, 4.5, 6.7, 8.9)).put(booleanArrayKey("ArrayBooleanKey"), Arrays.asList(true, false, false, true)).put(stringArrayKey("NullArrayStringKey"), (String[]) null).put(longArrayKey("NullArrayLongKey"), (Long[]) null).put(doubleArrayKey("NullArrayDoubleKey"), (Double[]) null).put(booleanArrayKey("NullArrayBooleanKey"), (Boolean[]) null).put(longArrayKey("ArrayWithNullLongKey"), Arrays.asList(new Long[] { null })).put(stringArrayKey("ArrayWithNullStringKey"), Arrays.asList(new String[] { null })).put(doubleArrayKey("ArrayWithNullDoubleKey"), Arrays.asList(new Double[] { null })).put(booleanArrayKey("ArrayWithNullBooleanKey"), Arrays.asList(new Boolean[] { null })).build();
try {
span.setAllAttributes(attributes);
} finally {
span.end();
}
SpanData spanData = span.toSpanData();
assertThat(spanData.getAttributes().size()).isEqualTo(16);
assertThat(spanData.getAttributes().get(stringKey("StringKey"))).isNotNull();
assertThat(spanData.getAttributes().get(stringKey("EmptyStringKey"))).isNotNull();
assertThat(spanData.getAttributes().get(stringKey("EmptyStringAttributeValue"))).isNotNull();
assertThat(spanData.getAttributes().get(longKey("LongKey"))).isNotNull();
assertThat(spanData.getAttributes().get(longKey("LongKey2"))).isEqualTo(5L);
assertThat(spanData.getAttributes().get(longKey("LongKey3"))).isEqualTo(6L);
assertThat(spanData.getAttributes().get(doubleKey("DoubleKey"))).isNotNull();
assertThat(spanData.getAttributes().get(booleanKey("BooleanKey"))).isNotNull();
assertThat(spanData.getAttributes().get(stringArrayKey("ArrayStringKey"))).isNotNull();
assertThat(spanData.getAttributes().get(longArrayKey("ArrayLongKey"))).isNotNull();
assertThat(spanData.getAttributes().get(doubleArrayKey("ArrayDoubleKey"))).isNotNull();
assertThat(spanData.getAttributes().get(booleanArrayKey("ArrayBooleanKey"))).isNotNull();
assertThat(spanData.getAttributes().get(longArrayKey("ArrayWithNullLongKey"))).isNotNull();
assertThat(spanData.getAttributes().get(stringArrayKey("ArrayWithNullStringKey"))).isNotNull();
assertThat(spanData.getAttributes().get(doubleArrayKey("ArrayWithNullDoubleKey"))).isNotNull();
assertThat(spanData.getAttributes().get(booleanArrayKey("ArrayWithNullBooleanKey"))).isNotNull();
assertThat(spanData.getAttributes().get(stringArrayKey("ArrayStringKey")).size()).isEqualTo(4);
assertThat(spanData.getAttributes().get(longArrayKey("ArrayLongKey")).size()).isEqualTo(5);
assertThat(spanData.getAttributes().get(doubleArrayKey("ArrayDoubleKey")).size()).isEqualTo(5);
assertThat(spanData.getAttributes().get(booleanArrayKey("ArrayBooleanKey")).size()).isEqualTo(4);
}
use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method setAllAttributes_nullAttributes.
@Test
void setAllAttributes_nullAttributes() {
SpanBuilder spanBuilder = sdkTracer.spanBuilder(SPAN_NAME).setAllAttributes(null);
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
try {
SpanData spanData = span.toSpanData();
Attributes attrs = spanData.getAttributes();
assertThat(attrs.size()).isEqualTo(0);
assertThat(spanData.getTotalAttributeCount()).isEqualTo(0);
} finally {
span.end();
}
}
use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method setAttribute.
@Test
void setAttribute() {
SpanBuilder spanBuilder = sdkTracer.spanBuilder(SPAN_NAME).setAttribute("string", "value").setAttribute("long", 12345L).setAttribute("double", .12345).setAttribute("boolean", true).setAttribute(stringKey("stringAttribute"), "attrvalue");
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
try {
SpanData spanData = span.toSpanData();
Attributes attrs = spanData.getAttributes();
assertThat(attrs.size()).isEqualTo(5);
assertThat(attrs.get(stringKey("string"))).isEqualTo("value");
assertThat(attrs.get(longKey("long"))).isEqualTo(12345L);
assertThat(attrs.get(doubleKey("double"))).isEqualTo(0.12345);
assertThat(attrs.get(booleanKey("boolean"))).isEqualTo(true);
assertThat(attrs.get(stringKey("stringAttribute"))).isEqualTo("attrvalue");
assertThat(spanData.getTotalAttributeCount()).isEqualTo(5);
} finally {
span.end();
}
}
use of io.opentelemetry.api.common.Attributes 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.common.Attributes in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method setAllAttributes_emptyAttributes.
@Test
void setAllAttributes_emptyAttributes() {
SpanBuilder spanBuilder = sdkTracer.spanBuilder(SPAN_NAME).setAllAttributes(Attributes.empty());
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
try {
SpanData spanData = span.toSpanData();
Attributes attrs = spanData.getAttributes();
assertThat(attrs.size()).isEqualTo(0);
assertThat(spanData.getTotalAttributeCount()).isEqualTo(0);
} finally {
span.end();
}
}
Aggregations