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

Fix NPE in brave-encoder-stackdriver when local ip is not set #225

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -72,14 +72,16 @@ Attributes extract(MutableSpan braveSpan) {
// will be rewritten into multiple single-host Stackdriver spans. A client send
// trace might not show the final destination.
if (braveSpan.localServiceName() != null && braveSpan.kind() == Span.Kind.SERVER) {
// Create an IP without querying DNS
InetAddress ip = InetAddresses.forString(braveSpan.localIp());
if (ip instanceof Inet4Address) {
attributes.putAttributeMap(
getLabelName("endpoint.ipv4"), toAttributeValue(ip.getHostAddress()));
} else if (ip instanceof Inet6Address) {
attributes.putAttributeMap(
getLabelName("endpoint.ipv6"), toAttributeValue(ip.getHostAddress()));
if (braveSpan.localIp() != null) {
// Create an IP without querying DNS
InetAddress ip = InetAddresses.forString(braveSpan.localIp());
if (ip instanceof Inet4Address) {
attributes.putAttributeMap(
getLabelName("endpoint.ipv4"), toAttributeValue(ip.getHostAddress()));
} else if (ip instanceof Inet6Address) {
attributes.putAttributeMap(
getLabelName("endpoint.ipv6"), toAttributeValue(ip.getHostAddress()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,32 @@ class AttributesExtractorTest {
assertThat(clientLabels).doesNotContainKeys("endpoint.ipv4", "endpoint.ipv6");
}

@Test void testEndpointIsNotSetForNullLocalIp() {
AttributesExtractor extractor = new AttributesExtractor(Tags.ERROR, Collections.emptyMap());

MutableSpan serverSpan =
new MutableSpan(TraceContext.newBuilder().traceId(4).spanId(5).build(), null);
serverSpan.name("test-span");
serverSpan.kind(Span.Kind.SERVER);
serverSpan.localServiceName("service1");
serverSpan.localIp(null);
serverSpan.localPort(80);

MutableSpan clientSpan =
new MutableSpan(TraceContext.newBuilder().traceId(4).parentId(5).spanId(6).build(), null);
clientSpan.name("test-span");
clientSpan.kind(Span.Kind.CLIENT);
clientSpan.localServiceName("service1");
clientSpan.localIp("::1");
clientSpan.localPort(80);

Map<String, AttributeValue> serverLabels = extractor.extract(serverSpan).getAttributeMapMap();
assertThat(serverLabels).doesNotContainKey("endpoint.ipv4");
assertThat(serverLabels).doesNotContainKey("endpoint.ipv6");
Map<String, AttributeValue> clientLabels = extractor.extract(clientSpan).getAttributeMapMap();
assertThat(clientLabels).doesNotContainKeys("endpoint.ipv4", "endpoint.ipv6");
}

@Test void testErrorTag() {
AttributesExtractor extractor = new AttributesExtractor(Tags.ERROR, Collections.emptyMap());

Expand Down
Loading