Search in sources :

Example 21 with AttributesBuilder

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

the class HttpServerAttributesExtractorTest method extractClientIpFromX_Forwarded_For.

@Test
void extractClientIpFromX_Forwarded_For() {
    Map<String, String> request = new HashMap<>();
    request.put("header.x-forwarded-for", "1.1.1.1");
    HttpServerAttributesExtractor<Map<String, String>, Map<String, String>> extractor = HttpServerAttributesExtractor.builder(new TestHttpServerAttributesExtractor()).setCapturedRequestHeaders(emptyList()).setCapturedResponseHeaders(emptyList()).build();
    AttributesBuilder attributes = Attributes.builder();
    extractor.onStart(attributes, Context.root(), request);
    assertThat(attributes.build()).containsOnly(entry(SemanticAttributes.HTTP_CLIENT_IP, "1.1.1.1"));
    extractor.onEnd(attributes, Context.root(), request, null, null);
    assertThat(attributes.build()).containsOnly(entry(SemanticAttributes.HTTP_CLIENT_IP, "1.1.1.1"));
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 22 with AttributesBuilder

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

the class InetSocketAddressNetClientAttributesGetterTest method noInetSocketAddress.

@Test
void noInetSocketAddress() {
    AttributesBuilder attributes = Attributes.builder();
    extractor.onEnd(attributes, Context.root(), null, null, 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 23 with AttributesBuilder

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

the class NetClientAttributesExtractorTest 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");
    TestNetClientAttributesGetter getter = new TestNetClientAttributesGetter();
    NetClientAttributesExtractor<Map<String, String>, Map<String, String>> extractor = NetClientAttributesExtractor.create(getter);
    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()).containsOnly(entry(SemanticAttributes.NET_PEER_PORT, 42L), entry(SemanticAttributes.NET_PEER_IP, "4.3.2.1"));
}
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 24 with AttributesBuilder

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

the class NetClientAttributesExtractorTest 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");
    TestNetClientAttributesGetter getter = new TestNetClientAttributesGetter();
    NetClientAttributesExtractor<Map<String, String>, Map<String, String>> extractor = NetClientAttributesExtractor.create(getter);
    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 25 with AttributesBuilder

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

the class RpcAttributesExtractorTest method normal.

@Test
void normal() {
    Map<String, String> request = new HashMap<>();
    request.put("service", "my.Service");
    request.put("method", "Method");
    Context context = Context.root();
    TestExtractor extractor = new TestExtractor();
    AttributesBuilder attributes = Attributes.builder();
    extractor.onStart(attributes, context, request);
    assertThat(attributes.build()).containsOnly(entry(SemanticAttributes.RPC_SYSTEM, "test"), entry(SemanticAttributes.RPC_SERVICE, "my.Service"), entry(SemanticAttributes.RPC_METHOD, "Method"));
    extractor.onEnd(attributes, context, request, null, null);
    assertThat(attributes.build()).containsOnly(entry(SemanticAttributes.RPC_SYSTEM, "test"), entry(SemanticAttributes.RPC_SERVICE, "my.Service"), entry(SemanticAttributes.RPC_METHOD, "Method"));
}
Also used : Context(io.opentelemetry.context.Context) HashMap(java.util.HashMap) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)105 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