Search in sources :

Example 26 with AttributesBuilder

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

the class InetSocketAddressNetServerAttributesGetterTest method unresolved.

@Test
void unresolved() {
    // given
    InetSocketAddress request = InetSocketAddress.createUnresolved("github.com", 123);
    assertThat(request.getAddress()).isNull();
    InetSocketAddress response = InetSocketAddress.createUnresolved("api.github.com", 456);
    assertThat(request.getAddress()).isNull();
    Context context = Context.root();
    // when
    AttributesBuilder startAttributes = Attributes.builder();
    extractor.onStart(startAttributes, context, request);
    AttributesBuilder endAttributes = Attributes.builder();
    extractor.onEnd(endAttributes, context, request, response, null);
    // then
    assertThat(startAttributes.build()).containsOnly(entry(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP), entry(SemanticAttributes.NET_PEER_PORT, 123L));
    assertThat(endAttributes.build()).isEmpty();
}
Also used : Context(io.opentelemetry.context.Context) InetSocketAddress(java.net.InetSocketAddress) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 27 with AttributesBuilder

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

the class InetSocketAddressNetServerAttributesGetterTest method noInetSocketAddress.

@Test
void noInetSocketAddress() {
    AttributesBuilder attributes = Attributes.builder();
    extractor.onStart(attributes, Context.root(), null);
    assertThat(attributes.build()).containsOnly(entry(SemanticAttributes.NET_TRANSPORT, SemanticAttributes.NetTransportValues.IP_TCP));
}
Also used : AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 28 with AttributesBuilder

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

the class NetServerAttributesExtractorTest method doesNotSetDuplicateAttributes.

@Test
public void doesNotSetDuplicateAttributes() {
    // given
    Map<String, String> request = new HashMap<>();
    request.put("transport", "TCP");
    request.put("peerName", "1.2.3.4");
    request.put("peerIp", "1.2.3.4");
    request.put("peerPort", "123");
    Map<String, String> response = new HashMap<>();
    response.put("peerName", "4.3.2.1");
    response.put("peerPort", "42");
    response.put("peerIp", "4.3.2.1");
    NetServerAttributesExtractor<Map<String, String>, Map<String, String>> extractor = createTestExtractor();
    Context context = Context.root();
    // when
    AttributesBuilder startAttributes = Attributes.builder();
    extractor.onStart(startAttributes, context, request);
    AttributesBuilder endAttributes = Attributes.builder();
    extractor.onEnd(endAttributes, context, request, response, null);
    // then
    assertThat(startAttributes.build()).containsOnly(entry(SemanticAttributes.NET_TRANSPORT, "TCP"), entry(SemanticAttributes.NET_PEER_PORT, 123L), entry(SemanticAttributes.NET_PEER_IP, "1.2.3.4"));
    assertThat(endAttributes.build()).isEmpty();
}
Also used : Context(io.opentelemetry.context.Context) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 29 with AttributesBuilder

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

the class NetServerAttributesExtractorTest method doesNotSetNegativePort.

@Test
public void doesNotSetNegativePort() {
    // given
    Map<String, String> request = new HashMap<>();
    request.put("peerPort", "-42");
    Map<String, String> response = new HashMap<>();
    response.put("peerPort", "-1");
    NetServerAttributesExtractor<Map<String, String>, Map<String, String>> extractor = createTestExtractor();
    Context context = Context.root();
    // when
    AttributesBuilder startAttributes = Attributes.builder();
    extractor.onStart(startAttributes, context, request);
    AttributesBuilder endAttributes = Attributes.builder();
    extractor.onEnd(endAttributes, context, request, response, null);
    // then
    assertThat(startAttributes.build()).isEmpty();
    assertThat(endAttributes.build()).isEmpty();
}
Also used : Context(io.opentelemetry.context.Context) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 30 with AttributesBuilder

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

the class DbClientAttributesExtractorTest method shouldExtractAllAvailableAttributes.

@Test
void shouldExtractAllAvailableAttributes() {
    // given
    Map<String, String> request = new HashMap<>();
    request.put("db.system", "myDb");
    request.put("db.user", "username");
    request.put("db.name", "potatoes");
    request.put("db.connection_string", "mydb:///potatoes");
    request.put("db.statement", "SELECT * FROM potato");
    request.put("db.operation", "SELECT");
    Context context = Context.root();
    DbClientAttributesExtractor<Map<String, String>, Void> underTest = DbClientAttributesExtractor.create(new TestAttributesGetter());
    // when
    AttributesBuilder startAttributes = Attributes.builder();
    underTest.onStart(startAttributes, context, request);
    AttributesBuilder endAttributes = Attributes.builder();
    underTest.onEnd(endAttributes, context, request, null, null);
    // then
    assertThat(startAttributes.build()).containsOnly(entry(SemanticAttributes.DB_SYSTEM, "myDb"), entry(SemanticAttributes.DB_USER, "username"), entry(SemanticAttributes.DB_NAME, "potatoes"), entry(SemanticAttributes.DB_CONNECTION_STRING, "mydb:///potatoes"), entry(SemanticAttributes.DB_STATEMENT, "SELECT * FROM potato"), entry(SemanticAttributes.DB_OPERATION, "SELECT"));
    assertThat(endAttributes.build().isEmpty()).isTrue();
}
Also used : Context(io.opentelemetry.context.Context) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)106 Test (org.junit.jupiter.api.Test)39 HashMap (java.util.HashMap)35 Map (java.util.Map)33 Context (io.opentelemetry.context.Context)25 Attributes (io.opentelemetry.api.common.Attributes)15 Test (org.junit.Test)14 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)9 LogBuilder (io.opentelemetry.instrumentation.api.appender.internal.LogBuilder)5 AttributeKey (io.opentelemetry.api.common.AttributeKey)4 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)4 Request (okhttp3.Request)4 Request (com.android.volley.Request)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 Response (okhttp3.Response)3 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 HttpResponse (com.android.volley.toolbox.HttpResponse)2