Search in sources :

Example 21 with Attributes

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);
}
Also used : SpanData(io.opentelemetry.sdk.trace.data.SpanData) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 22 with Attributes

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();
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 23 with Attributes

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();
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 24 with Attributes

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

Example 25 with Attributes

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();
    }
}
Also used : SpanBuilder(io.opentelemetry.api.trace.SpanBuilder) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Aggregations

Attributes (io.opentelemetry.api.common.Attributes)175 Test (org.junit.jupiter.api.Test)128 ResourceAttributes (io.opentelemetry.semconv.resource.attributes.ResourceAttributes)45 Resource (io.opentelemetry.sdk.resources.Resource)33 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)27 Context (io.opentelemetry.context.Context)25 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)24 SpanContext (io.opentelemetry.api.trace.SpanContext)22 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)22 SpanData (io.opentelemetry.sdk.trace.data.SpanData)22 HashMap (java.util.HashMap)21 Duration (java.time.Duration)20 ExemplarData (io.opentelemetry.sdk.metrics.data.ExemplarData)18 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)18 TestClock (io.opentelemetry.sdk.testing.time.TestClock)18 AttributeKey (io.opentelemetry.api.common.AttributeKey)17 Span (io.opentelemetry.api.trace.Span)17 DoubleExemplarData (io.opentelemetry.sdk.metrics.data.DoubleExemplarData)17 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)16 Map (java.util.Map)16