Search in sources :

Example 16 with AttributesBuilder

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

the class MessagingAttributesExtractorTest method shouldExtractNoAttributesIfNoneAreAvailable.

@Test
void shouldExtractNoAttributesIfNoneAreAvailable() {
    // given
    TestMessagingAttributesExtractor underTest = new TestMessagingAttributesExtractor(MessageOperation.SEND);
    Context context = Context.root();
    // when
    AttributesBuilder startAttributes = Attributes.builder();
    underTest.onStart(startAttributes, context, Collections.emptyMap());
    AttributesBuilder endAttributes = Attributes.builder();
    underTest.onEnd(endAttributes, context, Collections.emptyMap(), null, null);
    // then
    assertThat(startAttributes.build().isEmpty()).isTrue();
    assertThat(endAttributes.build().isEmpty()).isTrue();
}
Also used : Context(io.opentelemetry.context.Context) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 17 with AttributesBuilder

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

the class PeerServiceAttributesExtractorTest method shouldSetPeerIpIfItMatchesAndNameDoesNot.

@Test
void shouldSetPeerIpIfItMatchesAndNameDoesNot() {
    // given
    Map<String, String> peerServiceMapping = new HashMap<>();
    peerServiceMapping.put("example.com", "myService");
    peerServiceMapping.put("1.2.3.4", "someOtherService");
    PeerServiceAttributesExtractor<String, String> underTest = new PeerServiceAttributesExtractor<>(peerServiceMapping, netAttributesExtractor);
    given(netAttributesExtractor.peerName(any(), any())).willReturn("test.com");
    given(netAttributesExtractor.peerIp(any(), any())).willReturn("1.2.3.4");
    Context context = Context.root();
    // when
    AttributesBuilder startAttributes = Attributes.builder();
    underTest.onStart(startAttributes, context, "request");
    AttributesBuilder endAttributes = Attributes.builder();
    underTest.onEnd(endAttributes, context, "request", "response", null);
    // then
    assertThat(startAttributes.build()).isEmpty();
    assertThat(endAttributes.build()).containsOnly(entry(SemanticAttributes.PEER_SERVICE, "someOtherService"));
}
Also used : Context(io.opentelemetry.context.Context) HashMap(java.util.HashMap) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 18 with AttributesBuilder

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

the class PeerServiceAttributesExtractorTest method shouldSetPeerNameIfItMatches.

@Test
void shouldSetPeerNameIfItMatches() {
    // given
    Map<String, String> peerServiceMapping = new HashMap<>();
    peerServiceMapping.put("example.com", "myService");
    peerServiceMapping.put("1.2.3.4", "someOtherService");
    PeerServiceAttributesExtractor<String, String> underTest = new PeerServiceAttributesExtractor<>(peerServiceMapping, netAttributesExtractor);
    given(netAttributesExtractor.peerName(any(), any())).willReturn("example.com");
    Context context = Context.root();
    // when
    AttributesBuilder startAttributes = Attributes.builder();
    underTest.onStart(startAttributes, context, "request");
    AttributesBuilder endAttributes = Attributes.builder();
    underTest.onEnd(endAttributes, context, "request", "response", null);
    // then
    assertThat(startAttributes.build()).isEmpty();
    assertThat(endAttributes.build()).containsOnly(entry(SemanticAttributes.PEER_SERVICE, "myService"));
}
Also used : Context(io.opentelemetry.context.Context) HashMap(java.util.HashMap) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 19 with AttributesBuilder

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

the class PeerServiceAttributesExtractorTest method shouldNotSetAnyValueIfNetExtractorReturnsNulls.

@Test
void shouldNotSetAnyValueIfNetExtractorReturnsNulls() {
    // given
    Map<String, String> peerServiceMapping = singletonMap("1.2.3.4", "myService");
    PeerServiceAttributesExtractor<String, String> underTest = new PeerServiceAttributesExtractor<>(peerServiceMapping, netAttributesExtractor);
    Context context = Context.root();
    // when
    AttributesBuilder attributes = Attributes.builder();
    underTest.onStart(attributes, context, "request");
    underTest.onEnd(attributes, context, "request", "response", null);
    // then
    assertTrue(attributes.build().isEmpty());
}
Also used : Context(io.opentelemetry.context.Context) AttributesBuilder(io.opentelemetry.api.common.AttributesBuilder) Test(org.junit.jupiter.api.Test)

Example 20 with AttributesBuilder

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

the class HttpServerAttributesExtractorTest method extractClientIpFromX_Forwarded_Proto.

@Test
void extractClientIpFromX_Forwarded_Proto() {
    Map<String, String> request = new HashMap<>();
    request.put("header.x-forwarded-proto", "https");
    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_SCHEME, "https"));
    extractor.onEnd(attributes, Context.root(), request, null, null);
    assertThat(attributes.build()).containsOnly(entry(SemanticAttributes.HTTP_SCHEME, "https"));
}
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)

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