Updating Sockpeer service. (#7888)

Co-authored-by: Mateusz Rzeszutek <mrzeszutek@splunk.com>
This commit is contained in:
rahuldimri 2023-04-28 12:40:35 +05:30 committed by GitHub
parent 84a8c11ae4
commit 26a00e0b24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -59,6 +59,10 @@ public final class PeerServiceAttributesExtractor<REQUEST, RESPONSE>
String peerName = attributesGetter.getPeerName(request);
String peerService = mapToPeerService(peerName);
if (peerService == null) {
String sockPeerName = attributesGetter.getSockPeerName(request, response);
peerService = mapToPeerService(sockPeerName);
}
if (peerService != null) {
attributes.put(SemanticAttributes.PEER_SERVICE, peerService);
}

View File

@ -10,6 +10,8 @@ import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.opentelemetry.api.common.Attributes;
@ -89,6 +91,33 @@ class PeerServiceAttributesExtractorTest {
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"));
verify(netAttributesExtractor, never()).getSockPeerName(any(), any());
}
@Test
void shouldSetSockPeerNameIfItMatchesAndNoPeerNameProvided() {
// 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<>(netAttributesExtractor, peerServiceMapping);
when(netAttributesExtractor.getSockPeerName(any(), any())).thenReturn("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())