Search in sources :

Example 16 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class ZipkinSpanExporterTest method generateSpan_AlreadyHasHttpStatusInfo.

@Test
void generateSpan_AlreadyHasHttpStatusInfo() {
    Attributes attributeMap = Attributes.of(SemanticAttributes.HTTP_STATUS_CODE, 404L, stringKey("error"), "A user provided error");
    SpanData data = buildStandardSpan().setAttributes(attributeMap).setKind(SpanKind.CLIENT).setStatus(StatusData.error()).setTotalAttributeCount(2).build();
    assertThat(exporter.generateSpan(data)).isEqualTo(buildZipkinSpan(Span.Kind.CLIENT).toBuilder().clearTags().putTag(SemanticAttributes.HTTP_STATUS_CODE.getKey(), "404").putTag(ZipkinSpanExporter.OTEL_STATUS_CODE, "ERROR").putTag("error", "A user provided error").build());
}
Also used : SpanData(io.opentelemetry.sdk.trace.data.SpanData) TestSpanData(io.opentelemetry.sdk.testing.trace.TestSpanData) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) Test(org.junit.jupiter.api.Test)

Example 17 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class ProcessResourceTest method windows.

@Test
@SetSystemProperty(key = "os.name", value = "Windows 10")
void windows() {
    Resource resource = ProcessResource.buildResource();
    assertThat(resource.getSchemaUrl()).isEqualTo(ResourceAttributes.SCHEMA_URL);
    Attributes attributes = resource.getAttributes();
    assertThat(attributes.get(ResourceAttributes.PROCESS_PID)).isGreaterThan(1);
    assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH)).contains("java").endsWith(".exe");
    assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE)).contains(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH));
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) SetSystemProperty(org.junitpioneer.jupiter.SetSystemProperty) Test(org.junit.jupiter.api.Test)

Example 18 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class ProcessRuntimeResourceTest method shouldCreateRuntimeAttributes.

@Test
void shouldCreateRuntimeAttributes() {
    // when
    Resource resource = ProcessRuntimeResource.buildResource();
    Attributes attributes = resource.getAttributes();
    // then
    assertThat(resource.getSchemaUrl()).isEqualTo(ResourceAttributes.SCHEMA_URL);
    assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_NAME)).isNotBlank();
    assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_VERSION)).isNotBlank();
    assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION)).isNotBlank();
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) Test(org.junit.jupiter.api.Test)

Example 19 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class SdkSpanTest method attributeLength.

@Test
void attributeLength() {
    int maxLength = 25;
    SdkSpan span = createTestSpan(SpanLimits.builder().setMaxAttributeValueLength(maxLength).build());
    try {
        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();
        span.setAllAttributes(attributes);
        attributes = span.toSpanData().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();
    }
}
Also used : TraceFlags(io.opentelemetry.api.trace.TraceFlags) BeforeEach(org.junit.jupiter.api.BeforeEach) EventData(io.opentelemetry.sdk.trace.data.EventData) Arrays(java.util.Arrays) StatusCode(io.opentelemetry.api.trace.StatusCode) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) AttributeKey.booleanKey(io.opentelemetry.api.common.AttributeKey.booleanKey) Future(java.util.concurrent.Future) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Duration(java.time.Duration) Map(java.util.Map) AttributeKey.stringArrayKey(io.opentelemetry.api.common.AttributeKey.stringArrayKey) PrintWriter(java.io.PrintWriter) Context(io.opentelemetry.context.Context) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SpanContext(io.opentelemetry.api.trace.SpanContext) AttributeKey.doubleArrayKey(io.opentelemetry.api.common.AttributeKey.doubleArrayKey) Instant(java.time.Instant) SpanKind(io.opentelemetry.api.trace.SpanKind) Collectors.joining(java.util.stream.Collectors.joining) Executors(java.util.concurrent.Executors) Test(org.junit.jupiter.api.Test) List(java.util.List) AttributeKey(io.opentelemetry.api.common.AttributeKey) SpanData(io.opentelemetry.sdk.trace.data.SpanData) AttributeKey.longArrayKey(io.opentelemetry.api.common.AttributeKey.longArrayKey) AttributeKey.longKey(io.opentelemetry.api.common.AttributeKey.longKey) IntStream(java.util.stream.IntStream) SpanId(io.opentelemetry.api.trace.SpanId) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) StatusData(io.opentelemetry.sdk.trace.data.StatusData) HashMap(java.util.HashMap) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) TraceState(io.opentelemetry.api.trace.TraceState) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TestClock(io.opentelemetry.sdk.testing.time.TestClock) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) StringWriter(java.io.StringWriter) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) AttributeKey.doubleKey(io.opentelemetry.api.common.AttributeKey.doubleKey) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) AttributeKey.booleanArrayKey(io.opentelemetry.api.common.AttributeKey.booleanArrayKey) LinkData(io.opentelemetry.sdk.trace.data.LinkData) Collections(java.util.Collections) SemanticAttributes(io.opentelemetry.semconv.trace.attributes.SemanticAttributes) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Example 20 with Attributes

use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.

the class SdkSpanTest method setAllAttributes_mergesAttributes.

@Test
void setAllAttributes_mergesAttributes() {
    SdkSpan span = createTestRootSpan();
    Attributes attributes = Attributes.builder().put("StringKey", "StringVal").put("LongKey", 1000L).put("DoubleKey", 10.0).put("BooleanKey", false).build();
    try {
        span.setAttribute("StringKey", "OtherStringVal").setAttribute("ExistingStringKey", "ExistingStringVal").setAttribute("LongKey", 2000L).setAllAttributes(attributes);
    } finally {
        span.end();
    }
    SpanData spanData = span.toSpanData();
    assertThat(spanData.getAttributes().size()).isEqualTo(5);
    assertThat(spanData.getAttributes().get(stringKey("StringKey"))).isNotNull().isEqualTo("StringVal");
    assertThat(spanData.getAttributes().get(stringKey("ExistingStringKey"))).isNotNull().isEqualTo("ExistingStringVal");
    assertThat(spanData.getAttributes().get(longKey("LongKey"))).isNotNull().isEqualTo(1000L);
    assertThat(spanData.getAttributes().get(doubleKey("DoubleKey"))).isNotNull().isEqualTo(10.0);
    assertThat(spanData.getAttributes().get(booleanKey("BooleanKey"))).isNotNull().isEqualTo(false);
}
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)

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