diff --git a/jaeger-core/src/main/java/com/uber/jaeger/Tracer.java b/jaeger-core/src/main/java/com/uber/jaeger/Tracer.java index f77cbdb3c..7a5f2144b 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/Tracer.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/Tracer.java @@ -17,6 +17,8 @@ import com.uber.jaeger.baggage.BaggageRestrictionManager; import com.uber.jaeger.baggage.BaggageSetter; import com.uber.jaeger.baggage.DefaultBaggageRestrictionManager; +import com.uber.jaeger.exceptions.EmptyIpException; +import com.uber.jaeger.exceptions.NotFourOctetsException; import com.uber.jaeger.exceptions.UnsupportedFormatException; import com.uber.jaeger.metrics.Metrics; import com.uber.jaeger.metrics.MetricsFactory; @@ -98,16 +100,29 @@ private Tracer( this.version = loadVersion(); tags.put(Constants.JAEGER_CLIENT_VERSION_TAG_KEY, this.version); - String hostname = getHostName(); - if (hostname != null) { - tags.put(Constants.TRACER_HOSTNAME_TAG_KEY, hostname); + if (tags.get(Constants.TRACER_HOSTNAME_TAG_KEY) == null) { + String hostname = getHostName(); + if (hostname != null) { + tags.put(Constants.TRACER_HOSTNAME_TAG_KEY, hostname); + } } - int ipv4 ; - try { - tags.put(Constants.TRACER_IP_TAG_KEY, InetAddress.getLocalHost().getHostAddress()); - ipv4 = Utils.ipToInt(Inet4Address.getLocalHost().getHostAddress()); - } catch (UnknownHostException e) { - ipv4 = 0; + int ipv4; + Object ipTag = tags.get(Constants.TRACER_IP_TAG_KEY); + if (ipTag == null) { + try { + tags.put(Constants.TRACER_IP_TAG_KEY, InetAddress.getLocalHost().getHostAddress()); + ipv4 = Utils.ipToInt(Inet4Address.getLocalHost().getHostAddress()); + } catch (UnknownHostException e) { + ipv4 = 0; + } + } else { + try { + ipv4 = Utils.ipToInt(ipTag.toString()); + } catch (EmptyIpException e) { + ipv4 = 0; + } catch (NotFourOctetsException e) { + ipv4 = 0; + } } this.ipv4 = ipv4; this.tags = Collections.unmodifiableMap(tags); diff --git a/jaeger-core/src/test/java/com/uber/jaeger/TracerTagsTest.java b/jaeger-core/src/test/java/com/uber/jaeger/TracerTagsTest.java index e2a03d425..249ca340e 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/TracerTagsTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/TracerTagsTest.java @@ -18,6 +18,9 @@ import com.uber.jaeger.reporters.InMemoryReporter; import com.uber.jaeger.samplers.ConstSampler; +import com.uber.jaeger.utils.Utils; +import java.net.Inet4Address; +import java.net.InetAddress; import org.junit.Test; public class TracerTagsTest { @@ -38,4 +41,52 @@ public void testTracerTags() throws Exception { assertEquals(true, span.getTags().containsKey("sampler.param")); assertEquals(false, span.getTags().containsKey("tracer.tag.str")); } + + @Test + public void testDefaultHostTags() throws Exception { + InMemoryReporter spanReporter = new InMemoryReporter(); + Tracer tracer = new Tracer.Builder("x") + .withReporter(spanReporter) + .build(); + assertEquals(tracer.getHostName(), tracer.tags().get(Constants.TRACER_HOSTNAME_TAG_KEY)); + assertEquals(InetAddress.getLocalHost().getHostAddress(), tracer.tags().get(Constants.TRACER_IP_TAG_KEY)); + assertEquals(Utils.ipToInt(Inet4Address.getLocalHost().getHostAddress()), tracer.getIpv4()); + } + + @Test + public void testDeclaredHostTags() throws Exception { + InMemoryReporter spanReporter = new InMemoryReporter(); + String hostname = "myhost"; + String ip = "1.1.1.1"; + Tracer tracer = new Tracer.Builder("x") + .withReporter(spanReporter) + .withTag(Constants.TRACER_HOSTNAME_TAG_KEY, hostname) + .withTag(Constants.TRACER_IP_TAG_KEY, ip) + .build(); + assertEquals(hostname, tracer.tags().get(Constants.TRACER_HOSTNAME_TAG_KEY)); + assertEquals(ip, tracer.tags().get(Constants.TRACER_IP_TAG_KEY)); + assertEquals(Utils.ipToInt(ip), tracer.getIpv4()); + } + + @Test + public void testEmptyDeclaredIpTag() throws Exception { + InMemoryReporter spanReporter = new InMemoryReporter(); + String ip = ""; + Tracer tracer = new Tracer.Builder("x") + .withReporter(spanReporter) + .withTag(Constants.TRACER_IP_TAG_KEY, ip) + .build(); + assertEquals(0, tracer.getIpv4()); + } + + @Test + public void testShortDeclaredIpTag() throws Exception { + InMemoryReporter spanReporter = new InMemoryReporter(); + String ip = ":19"; + Tracer tracer = new Tracer.Builder("x") + .withReporter(spanReporter) + .withTag(Constants.TRACER_IP_TAG_KEY, ip) + .build(); + assertEquals(0, tracer.getIpv4()); + } }