Search in sources :

Example 31 with Attributes

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

the class SdkLogEmitterTest method logBuilder_maxAttributeLength.

@Test
void logBuilder_maxAttributeLength() {
    int maxLength = 25;
    AtomicReference<LogData> seenLog = new AtomicReference<>();
    SdkLogEmitterProvider logEmitterProvider = SdkLogEmitterProvider.builder().addLogProcessor(seenLog::set).setLogLimits(() -> LogLimits.builder().setMaxAttributeValueLength(maxLength).build()).build();
    LogBuilder logBuilder = logEmitterProvider.get("test").logBuilder();
    String strVal = StringUtils.padLeft("", maxLength);
    String tooLongStrVal = strVal + strVal;
    logBuilder.setAttributes(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()).emit();
    Attributes attributes = seenLog.get().getAttributes();
    assertThat(attributes).containsEntry("string", strVal).containsEntry("boolean", true).containsEntry("long", 1L).containsEntry("double", 1.0).containsEntry("stringArray", strVal, strVal).containsEntry("booleanArray", true, false).containsEntry("longArray", 1L, 2L).containsEntry("doubleArray", 1.0, 2.0);
}
Also used : LogData(io.opentelemetry.sdk.logs.data.LogData) Attributes(io.opentelemetry.api.common.Attributes) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 32 with Attributes

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

the class ResourceTest method create.

@Test
void create() {
    Attributes attributes = Attributes.of(stringKey("a"), "1", stringKey("b"), "2");
    Resource resource = Resource.create(attributes);
    assertThat(resource.getAttributes()).isNotNull();
    assertThat(resource.getAttributes().size()).isEqualTo(2);
    assertThat(resource.getAttributes()).isEqualTo(attributes);
    Resource resource1 = Resource.create(Attributes.empty());
    assertThat(resource1.getAttributes()).isNotNull();
    assertThat(resource1.getAttributes().isEmpty()).isTrue();
}
Also used : Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) Test(org.junit.jupiter.api.Test)

Example 33 with Attributes

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

the class ResourceTest method setUp.

@BeforeEach
void setUp() {
    Attributes attributes1 = Attributes.of(stringKey("a"), "1", stringKey("b"), "2");
    Attributes attribute2 = Attributes.of(stringKey("a"), "1", stringKey("b"), "3", stringKey("c"), "4");
    resource1 = Resource.create(attributes1);
    resource2 = Resource.create(attribute2);
}
Also used : Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 34 with Attributes

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

the class ResourceTest method testMergeResources_Resource1_Null.

@Test
void testMergeResources_Resource1_Null() {
    Attributes expectedAttributes = Attributes.of(stringKey("a"), "1", stringKey("b"), "3", stringKey("c"), "4");
    Resource resource = Resource.empty().merge(null).merge(resource2);
    assertThat(resource.getAttributes()).isEqualTo(expectedAttributes);
}
Also used : Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) Test(org.junit.jupiter.api.Test)

Example 35 with Attributes

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

the class ResourceTest method shouldBuilderHelperMethodsBuildResource.

@Test
void shouldBuilderHelperMethodsBuildResource() {
    // given
    ResourceBuilder builder = Resource.getDefault().toBuilder();
    Attributes sourceAttributes = Attributes.of(stringKey("hello"), "world");
    Resource source = Resource.create(sourceAttributes);
    Attributes sourceAttributes2 = Attributes.of(stringKey("OpenTelemetry"), "Java");
    // when
    Resource resource = builder.put("long", 42L).put("double", Math.E).put("boolean", true).put("string", "abc").put("long array", 1L, 2L, 3L).put("double array", Math.E, Math.PI).put("boolean array", true, false).put("string array", "first", "second").put(longKey("long key"), 4242L).put(longKey("int in disguise"), 21).putAll(source).putAll(sourceAttributes2).build();
    // then
    Attributes attributes = resource.getAttributes();
    assertThat(attributes.get(longKey("long"))).isEqualTo(42L);
    assertThat(attributes.get(doubleKey("double"))).isEqualTo(Math.E);
    assertThat(attributes.get(booleanKey("boolean"))).isEqualTo(true);
    assertThat(attributes.get(stringKey("string"))).isEqualTo("abc");
    assertThat(attributes.get(longArrayKey("long array"))).isEqualTo(Arrays.asList(1L, 2L, 3L));
    assertThat(attributes.get(doubleArrayKey("double array"))).isEqualTo(Arrays.asList(Math.E, Math.PI));
    assertThat(attributes.get(booleanArrayKey("boolean array"))).isEqualTo(Arrays.asList(true, false));
    assertThat(attributes.get(stringArrayKey("string array"))).isEqualTo(Arrays.asList("first", "second"));
    assertThat(attributes.get(longKey("long key"))).isEqualTo(4242L);
    assertThat(attributes.get(longKey("int in disguise"))).isEqualTo(21);
    assertThat(attributes.get(stringKey("hello"))).isEqualTo("world");
    assertThat(attributes.get(stringKey("OpenTelemetry"))).isEqualTo("Java");
}
Also used : Attributes(io.opentelemetry.api.common.Attributes) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) 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