Skip to content

Commit

Permalink
Fix NPE in brave-encoder-stackdriver when local ip is not set (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
oswynb authored Aug 15, 2024
1 parent 829844a commit 4638532
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
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

0 comments on commit 4638532

Please sign in to comment.