Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Avoid host lookups if trace tags have already been provided
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Hunt <huntchr@gmail.com>
  • Loading branch information
huntc committed Mar 18, 2018
1 parent ee137d6 commit 8f90229
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
33 changes: 24 additions & 9 deletions jaeger-core/src/main/java/com/uber/jaeger/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions jaeger-core/src/test/java/com/uber/jaeger/TracerTagsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}

0 comments on commit 8f90229

Please sign in to comment.