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

Commit

Permalink
Add Process IP to jaeger.thrift (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
black-adder authored Jun 19, 2017
1 parent 4752c93 commit 74faa98
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 130 deletions.
15 changes: 15 additions & 0 deletions jaeger-core/src/main/java/com/uber/jaeger/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ public class Constants {
* root span, so that the trace can be found in the UI using this value as a correlation ID.
*/
public static final String DEBUG_ID_HEADER_KEY = "jaeger-debug-id";

/**
* The name of the tag used to report client version.
*/
public static final String JAEGER_CLIENT_VERSION_TAG_KEY = "jaeger.version";

/**
* The name used to report host name of the process.
*/
public static final String TRACER_HOSTNAME_TAG_KEY = "hostname";

/**
* The name used to report ip of the process.
*/
public static final String TRACER_IP_TAG_KEY = "ip";
}
36 changes: 16 additions & 20 deletions jaeger-core/src/main/java/com/uber/jaeger/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package com.uber.jaeger;

import com.uber.jaeger.Constants;
import com.uber.jaeger.exceptions.UnsupportedFormatException;
import com.uber.jaeger.metrics.Metrics;
import com.uber.jaeger.metrics.NullStatsReporter;
Expand Down Expand Up @@ -69,7 +70,7 @@ public class Tracer implements io.opentracing.Tracer {
private final PropagationRegistry registry;
private final Clock clock;
private final Metrics metrics;
private final int ip;
private final int ipv4;
private final Map<String, ?> tags;
private final boolean zipkinSharedRpcSpan;
private final ActiveSpanSource activeSpanSource;
Expand All @@ -93,21 +94,21 @@ private Tracer(
this.zipkinSharedRpcSpan = zipkinSharedRpcSpan;
this.activeSpanSource = activeSpanSource;

int ip;
try {
ip = Utils.ipToInt(Inet4Address.getLocalHost().getHostAddress());
} catch (UnknownHostException e) {
ip = 0;
}
this.ip = ip;

this.version = loadVersion();

tags.put("jaeger.version", this.version);
tags.put(Constants.JAEGER_CLIENT_VERSION_TAG_KEY, this.version);
String hostname = getHostName();
if (hostname != null) {
tags.put("jaeger.hostname", hostname);
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;
}
this.ipv4 = ipv4;
this.tags = Collections.unmodifiableMap(tags);
}

Expand All @@ -127,8 +128,8 @@ public String getServiceName() {
return tags;
}

public int getIp() {
return ip;
public int getIpv4() {
return ipv4;
}

Clock clock() {
Expand Down Expand Up @@ -397,11 +398,6 @@ public io.opentracing.Span startManual() {
}
}

// TODO move this to jaeger-zipkin, this adds tracer tags to zipkin first span in a process
if (zipkinSharedRpcSpan && (references.isEmpty() || isRpcServer())) {
tags.putAll(Tracer.this.tags);
}

Span span =
new Span(
Tracer.this,
Expand Down Expand Up @@ -557,15 +553,15 @@ private static String loadVersion() {
try {
Properties prop = new Properties();
prop.load(is);
version = prop.getProperty("jaeger.version");
version = prop.getProperty(Constants.JAEGER_CLIENT_VERSION_TAG_KEY);
} finally {
is.close();
}
} catch (Exception e) {
throw new RuntimeException("Cannot read jaeger.properties", e);
}
if (version == null) {
throw new RuntimeException("Cannot read jaeger.version from jaeger.properties");
throw new RuntimeException("Cannot read " + Constants.JAEGER_CLIENT_VERSION_TAG_KEY + " from jaeger.properties");
}
return "Java-" + version;
}
Expand Down
108 changes: 10 additions & 98 deletions jaeger-core/src/test/java/com/uber/jaeger/TracerTagsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,115 +23,27 @@
package com.uber.jaeger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import com.uber.jaeger.reporters.InMemoryReporter;
import com.uber.jaeger.samplers.ConstSampler;
import io.opentracing.tag.Tags;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class TracerTagsTest {

private enum SpanType {
ROOT,
CHILD,
RPC_SERVER
}

// sentinel value is used to mark tags that should *not* be present
private static final Object SENTINEL = new Object();

private final SpanType spanType;
private final Map<String, Object> expectedTags;

public TracerTagsTest(SpanType spanType, Map<String, Object> expectedTags) {
this.spanType = spanType;
this.expectedTags = expectedTags;
}

@Parameterized.Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
Tracer tracer = new Tracer.Builder("x", null, null).build();
String hostname = tracer.getHostName();

Map<String, Object> rootTags = new HashMap<>();
rootTags.put("jaeger.version", tracer.getVersion());
rootTags.put("jaeger.hostname", hostname);
rootTags.put("tracer.tag.str", "y");
rootTags.put("tracer.tag.bool", true);
rootTags.put("tracer.tag.num", 1);
rootTags.put("sampler.type", "const");
rootTags.put("sampler.param", true);

Map<String, Object> childTags = new HashMap<>();
childTags.put("jaeger.version", SENTINEL);
childTags.put("jaeger.hostname", SENTINEL);
childTags.put("tracer.tag.str", SENTINEL);
childTags.put("tracer.tag.bool", SENTINEL);
childTags.put("tracer.tag.num", SENTINEL);
childTags.put("sampler.type", SENTINEL);
childTags.put("sampler.param", SENTINEL);

Map<String, Object> rpcTags = new HashMap<>();
rpcTags.put("jaeger.version", tracer.getVersion());
rpcTags.put("jaeger.hostname", hostname);
rpcTags.put("tracer.tag.str", "y");
rpcTags.put("tracer.tag.bool", true);
rpcTags.put("tracer.tag.num", 1);
rpcTags.put("sampler.type", SENTINEL);
rpcTags.put("sampler.param", SENTINEL);

List<Object[]> data = new ArrayList<>();
data.add(new Object[] {SpanType.ROOT, rootTags});
data.add(new Object[] {SpanType.CHILD, childTags});
data.add(new Object[] {SpanType.RPC_SERVER, rpcTags});
return data;
}

@Before
public void setUp() throws Exception {}

@Test
public void testTracerTagsZipkin() throws Exception {
public void testTracerTags() throws Exception {
InMemoryReporter spanReporter = new InMemoryReporter();
Tracer tracer = new Tracer.Builder("x", spanReporter, new ConstSampler(true))
.withZipkinSharedRpcSpan()
.withTag("tracer.tag.str", "y")
.withTag("tracer.tag.bool", true)
.withTag("tracer.tag.num", 1)
.build();
.withZipkinSharedRpcSpan()
.withTag("tracer.tag.str", "y")
.build();

Span span = (Span) tracer.buildSpan("root").startManual();
if (spanType == SpanType.CHILD) {
span = (Span) tracer.buildSpan("child").asChildOf(span).startManual();
}
if (spanType == SpanType.RPC_SERVER) {
span =
(Span)
tracer
.buildSpan("rpc-server")
.asChildOf(span)
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
.startManual();
}
Map<String, Object> tags = span.getTags();
for (String key : expectedTags.keySet()) {
Object expectedValue = expectedTags.get(key);
Object actualValue = tags.get(key);
if (expectedValue == SENTINEL) {
assertNull("Not expecting " + key + " for " + spanType, actualValue);
} else {
assertEquals("Expecting " + key + " for " + spanType, expectedValue, actualValue);
}
}

// span should only contain sampler tags and no tracer tags
assertEquals(2, span.getTags().size());
assertEquals(true, span.getTags().containsKey("sampler.type"));
assertEquals(true, span.getTags().containsKey("sampler.param"));
assertEquals(false, span.getTags().containsKey("tracer.tag.str"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ public void testFlushSendsSpan() throws Exception {
assertEquals(0, actualSpan.getParentSpanId());
assertTrue(actualSpan.references.isEmpty());
assertEquals(expectedSpan.getOperationName(), actualSpan.getOperationName());
assertEquals(3, batch.getProcess().getTags().size());
assertEquals("jaeger.hostname", batch.getProcess().getTags().get(0).getKey());
assertEquals(4, batch.getProcess().getTags().size());
assertEquals("hostname", batch.getProcess().getTags().get(0).getKey());
assertEquals("jaeger.version", batch.getProcess().getTags().get(1).getKey());
assertEquals("bar", batch.getProcess().getTags().get(2).getVStr());
assertEquals("ip", batch.getProcess().getTags().get(3).getKey());
}

@Test
Expand Down
1 change: 1 addition & 0 deletions jaeger-zipkin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies {
testCompile group: 'junit', name: 'junit', version: junitVersion
testCompile group: 'io.zipkin.java', name: 'zipkin-junit', version: '1.16.2'
testCompile group: 'io.zipkin.brave', name: 'brave-http', version: '4.1.1'
testCompile group: 'com.tngtech.java', name: 'junit-dataprovider', version: junitDataProviderVersion

signature 'org.codehaus.mojo.signature:java16:1.1@signature'
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
import com.twitter.zipkin.thriftjava.BinaryAnnotation;
import com.twitter.zipkin.thriftjava.Endpoint;
import com.twitter.zipkin.thriftjava.zipkincoreConstants;
import com.uber.jaeger.Constants;
import com.uber.jaeger.LogData;
import com.uber.jaeger.Span;
import com.uber.jaeger.SpanContext;
import com.uber.jaeger.Tracer;
import io.opentracing.tag.Tags;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -43,7 +45,7 @@ public class ThriftSpanConverter {

public static com.twitter.zipkin.thriftjava.Span convertSpan(Span span) {
Tracer tracer = span.getTracer();
Endpoint host = new Endpoint(tracer.getIp(), (short) 0, tracer.getServiceName());
Endpoint host = new Endpoint(tracer.getIpv4(), (short) 0, tracer.getServiceName());

SpanContext context = span.context();
return new com.twitter.zipkin.thriftjava.Span(
Expand Down Expand Up @@ -88,6 +90,20 @@ private static List<BinaryAnnotation> buildBinaryAnnotations(Span span, Endpoint
Map<String, Object> tags = span.getTags();
boolean isRpc = isRpc(span);
boolean isClient = isRpcClient(span);
boolean firstSpanInProcess = span.getReferences().isEmpty() || isRpcServer(span);

if (firstSpanInProcess) {
Map<String, ?> processTags = span.getTracer().tags();
// add tracer tags to first zipkin span in a process but remove "ip" tag as it is
// taken care of separately.
for (String tagKey : processTags.keySet()) {
if (!tagKey.equals(Constants.TRACER_IP_TAG_KEY)) {
Object tagValue = processTags.get(tagKey);
// add a tracer. prefix to process tags for zipkin
binaryAnnotations.add(buildBinaryAnnotation("tracer." + tagKey, tagValue));
}
}
}

Endpoint peerEndpoint = extractPeerEndpoint(tags);
if (peerEndpoint != null && isClient) {
Expand Down Expand Up @@ -137,13 +153,17 @@ private static BinaryAnnotation buildBinaryAnnotation(String tagKey, Object tagV
return banno;
}

public static boolean isRpc(Span span) {
static boolean isRpcServer(Span span) {
return Tags.SPAN_KIND_SERVER.equals(span.getTags().get(Tags.SPAN_KIND.getKey()));
}

static boolean isRpc(Span span) {
Object spanKindValue = span.getTags().get(Tags.SPAN_KIND.getKey());
return Tags.SPAN_KIND_CLIENT.equals(spanKindValue) || Tags.SPAN_KIND_SERVER.equals(spanKindValue);

}

public static boolean isRpcClient(Span span) {
static boolean isRpcClient(Span span) {
return Tags.SPAN_KIND_CLIENT.equals(span.getTags().get(Tags.SPAN_KIND.getKey()));
}

Expand Down
Loading

0 comments on commit 74faa98

Please sign in to comment.