Skip to content

Commit 26a00e0

Browse files
rahuldimriMateusz Rzeszutek
andauthored
Updating Sockpeer service. (#7888)
Co-authored-by: Mateusz Rzeszutek <[email protected]>
1 parent 84a8c11 commit 26a00e0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/net/PeerServiceAttributesExtractor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public void onEnd(
5959

6060
String peerName = attributesGetter.getPeerName(request);
6161
String peerService = mapToPeerService(peerName);
62+
if (peerService == null) {
63+
String sockPeerName = attributesGetter.getSockPeerName(request, response);
64+
peerService = mapToPeerService(sockPeerName);
65+
}
6266
if (peerService != null) {
6367
attributes.put(SemanticAttributes.PEER_SERVICE, peerService);
6468
}

instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/net/PeerServiceAttributesExtractorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import static org.assertj.core.api.Assertions.entry;
1111
import static org.junit.jupiter.api.Assertions.assertTrue;
1212
import static org.mockito.ArgumentMatchers.any;
13+
import static org.mockito.Mockito.never;
14+
import static org.mockito.Mockito.verify;
1315
import static org.mockito.Mockito.when;
1416

1517
import io.opentelemetry.api.common.Attributes;
@@ -89,6 +91,33 @@ void shouldSetPeerNameIfItMatches() {
8991
AttributesBuilder endAttributes = Attributes.builder();
9092
underTest.onEnd(endAttributes, context, "request", "response", null);
9193

94+
// then
95+
assertThat(startAttributes.build()).isEmpty();
96+
assertThat(endAttributes.build())
97+
.containsOnly(entry(SemanticAttributes.PEER_SERVICE, "myService"));
98+
verify(netAttributesExtractor, never()).getSockPeerName(any(), any());
99+
}
100+
101+
@Test
102+
void shouldSetSockPeerNameIfItMatchesAndNoPeerNameProvided() {
103+
// given
104+
Map<String, String> peerServiceMapping = new HashMap<>();
105+
peerServiceMapping.put("example.com", "myService");
106+
peerServiceMapping.put("1.2.3.4", "someOtherService");
107+
108+
PeerServiceAttributesExtractor<String, String> underTest =
109+
new PeerServiceAttributesExtractor<>(netAttributesExtractor, peerServiceMapping);
110+
111+
when(netAttributesExtractor.getSockPeerName(any(), any())).thenReturn("example.com");
112+
113+
Context context = Context.root();
114+
115+
// when
116+
AttributesBuilder startAttributes = Attributes.builder();
117+
underTest.onStart(startAttributes, context, "request");
118+
AttributesBuilder endAttributes = Attributes.builder();
119+
underTest.onEnd(endAttributes, context, "request", "response", null);
120+
92121
// then
93122
assertThat(startAttributes.build()).isEmpty();
94123
assertThat(endAttributes.build())

0 commit comments

Comments
 (0)