Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Sockpeer service. #7888

Merged
merged 17 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,14 @@ public void onEnd(
String peerService = mapToPeerService(peerName);
if (peerService != null) {
attributes.put(SemanticAttributes.PEER_SERVICE, peerService);
} else {
// try map sockPeerName only if there was no match for peerName
String sockPeerName = attributesGetter.sockPeerName(request, response);
String sockPeerService = mapToPeerService(sockPeerName);
if (sockPeerService != null) {
attributes.put(SemanticAttributes.PEER_SERVICE, sockPeerService);
}
}
rahuldimri marked this conversation as resolved.
Show resolved Hide resolved
}
rahuldimri marked this conversation as resolved.
Show resolved Hide resolved

@Nullable
private String mapToPeerService(String endpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
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;
Expand Down Expand Up @@ -94,4 +96,57 @@ void shouldSetPeerNameIfItMatches() {
assertThat(endAttributes.build())
.containsOnly(entry(SemanticAttributes.PEER_SERVICE, "myService"));
}

@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.sockPeerName(any(), any())).thenReturn("example.com");
rahuldimri marked this conversation as resolved.
Show resolved Hide resolved

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"));
}

@Test
void shouldSetNoSockPeerNameIfPeerNameMatches() {
rahuldimri marked this conversation as resolved.
Show resolved Hide resolved
// given
Map<String, String> peerServiceMapping = new HashMap<>();
peerServiceMapping.put("example.com", "myService");
peerServiceMapping.put("unmatched.com", "someOtherService");

PeerServiceAttributesExtractor<String, String> underTest =
new PeerServiceAttributesExtractor<>(netAttributesExtractor, peerServiceMapping);

when(netAttributesExtractor.peerName(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())
.containsOnly(entry(SemanticAttributes.PEER_SERVICE, "myService"));
verify(netAttributesExtractor, never()).sockPeerName(any(), any());
rahuldimri marked this conversation as resolved.
Show resolved Hide resolved
}
}