From c6dadd693f139034a4ec35664b81f10014ad0c03 Mon Sep 17 00:00:00 2001 From: Zhan Su Date: Sun, 15 Sep 2019 19:26:53 -0700 Subject: [PATCH] Make serviceName check null-safe (#149) Span object stores `localEndpoint.serviceName` empty string as null so `.isEmpty()` throws NullPointerException. --- .../stackdriver/AttributesExtractor.java | 4 +++- .../stackdriver/AttributesExtractorTest.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/translation-stackdriver/src/main/java/zipkin2/translation/stackdriver/AttributesExtractor.java b/translation-stackdriver/src/main/java/zipkin2/translation/stackdriver/AttributesExtractor.java index e7a795b9..5edbbd0d 100644 --- a/translation-stackdriver/src/main/java/zipkin2/translation/stackdriver/AttributesExtractor.java +++ b/translation-stackdriver/src/main/java/zipkin2/translation/stackdriver/AttributesExtractor.java @@ -80,7 +80,9 @@ Attributes extract(Span zipkinSpan) { } } - if (zipkinSpan.localEndpoint() != null && !zipkinSpan.localEndpoint().serviceName().isEmpty()) { + if (zipkinSpan.localEndpoint() != null && + zipkinSpan.localEndpoint().serviceName() != null && + !zipkinSpan.localEndpoint().serviceName().isEmpty()) { attributes.putAttributeMap( kComponentLabelKey, toAttributeValue(zipkinSpan.localEndpoint().serviceName())); } diff --git a/translation-stackdriver/src/test/java/zipkin2/translation/stackdriver/AttributesExtractorTest.java b/translation-stackdriver/src/test/java/zipkin2/translation/stackdriver/AttributesExtractorTest.java index 66aa9fa7..49844c3b 100644 --- a/translation-stackdriver/src/test/java/zipkin2/translation/stackdriver/AttributesExtractorTest.java +++ b/translation-stackdriver/src/test/java/zipkin2/translation/stackdriver/AttributesExtractorTest.java @@ -148,6 +148,24 @@ public void testEndpointIsSetIpv6() { assertThat(clientLabels).doesNotContainKeys("endpoint.ipv4", "endpoint.ipv6"); } + @Test + public void testEndpointWithNullServiceName() { + Endpoint.Builder serverEndpointBuilder = Endpoint.newBuilder().port(80); + Endpoint serverEndpoint = serverEndpointBuilder.build(); + Span serverSpan = + Span.newBuilder() + .kind(Kind.SERVER) + .traceId("4") + .name("test-span") + .id("5") + .localEndpoint(serverEndpoint) + .build(); + + AttributesExtractor extractor = new AttributesExtractor(Collections.emptyMap()); + Map serverLabels = extractor.extract(serverSpan).getAttributeMapMap(); + assertThat(serverLabels).doesNotContainKey("endpoint.serviceName"); + } + @Test public void testComponentLabelIsSet() { AttributesExtractor extractor = new AttributesExtractor(Collections.emptyMap());