From 46385329b46304b827a1579b4d24d6fe99deebc3 Mon Sep 17 00:00:00 2001 From: Oswyn Brent Date: Thu, 15 Aug 2024 12:03:47 +1000 Subject: [PATCH] Fix NPE in brave-encoder-stackdriver when local ip is not set (#225) --- .../brave/AttributesExtractor.java | 18 +++++++------ .../brave/AttributesExtractorTest.java | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/encoder-stackdriver-brave/src/main/java/zipkin2/reporter/stackdriver/brave/AttributesExtractor.java b/encoder-stackdriver-brave/src/main/java/zipkin2/reporter/stackdriver/brave/AttributesExtractor.java index 5b49477..77d8dfa 100644 --- a/encoder-stackdriver-brave/src/main/java/zipkin2/reporter/stackdriver/brave/AttributesExtractor.java +++ b/encoder-stackdriver-brave/src/main/java/zipkin2/reporter/stackdriver/brave/AttributesExtractor.java @@ -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())); + } } } diff --git a/encoder-stackdriver-brave/src/test/java/zipkin2/reporter/stackdriver/brave/AttributesExtractorTest.java b/encoder-stackdriver-brave/src/test/java/zipkin2/reporter/stackdriver/brave/AttributesExtractorTest.java index e1b403e..b023f87 100644 --- a/encoder-stackdriver-brave/src/test/java/zipkin2/reporter/stackdriver/brave/AttributesExtractorTest.java +++ b/encoder-stackdriver-brave/src/test/java/zipkin2/reporter/stackdriver/brave/AttributesExtractorTest.java @@ -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 serverLabels = extractor.extract(serverSpan).getAttributeMapMap(); + assertThat(serverLabels).doesNotContainKey("endpoint.ipv4"); + assertThat(serverLabels).doesNotContainKey("endpoint.ipv6"); + Map clientLabels = extractor.extract(clientSpan).getAttributeMapMap(); + assertThat(clientLabels).doesNotContainKeys("endpoint.ipv4", "endpoint.ipv6"); + } + @Test void testErrorTag() { AttributesExtractor extractor = new AttributesExtractor(Tags.ERROR, Collections.emptyMap());