Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}


ext.opentracingVersion = '0.15.0'
ext.opentracingVersion = '0.20.10'
ext.guavaVersion = '18.0'
ext.apacheThriftVersion = '0.9.2'
ext.jerseyVersion = '2.22.2'
Expand Down
22 changes: 12 additions & 10 deletions jaeger-core/src/main/java/com/uber/jaeger/LogData.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,28 @@
*/
package com.uber.jaeger;

import lombok.Getter;

import java.util.Map;

@Getter
public final class LogData {
private final long time; // time in microseconds
private final String message;
private final Object payload;
private final Map<String, ?> fields;

LogData(long time, String message, Object payload) {
this.time = time;
this.message = message;
this.payload = payload;
this.fields = null;
}

public long getTime() {
return time;
}

public String getMessage() {
return message;
}

public Object getPayload() {
return payload;
LogData(long time, Map<String, ?> fields) {
this.time = time;
this.message = null;
this.payload = null;
this.fields = fields;
}
}
41 changes: 37 additions & 4 deletions jaeger-core/src/main/java/com/uber/jaeger/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,53 @@ private Span setTagAsObject(String key, Object value) {
return this;
}

@Override
public Span log(Map<String, ?> fields) {
return log(tracer.clock().currentTimeMicros(), fields);
}

@Override
public Span log(long timestampMicroseconds, Map<String, ?> fields) {
synchronized (this) {
if (fields == null) {
return this;
}
if (context.isSampled()) {
if (logs == null) {
this.logs = new ArrayList<LogData>();
}
logs.add(new LogData(timestampMicroseconds, fields));
}
return this;
}
}

@Override
public Span log(String event) {
return log(tracer.clock().currentTimeMicros(), event, null);
}

@Override
public Span log(long timestampMicroseconds, String event) {
return log(timestampMicroseconds, event, null);
}

@Override
public Span log(String message, /* @Nullable */ Object payload) {
return log(tracer.clock().currentTimeMicros(), message, payload);
}

@Override
public Span log(long instantMicroseconds, String message, /* @Nullable */ Object payload) {
public Span log(long timestampMicroseconds, String message, /* @Nullable */ Object payload) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that this function is deprecated, I'm not going to do a check here to see if the payload is a map.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't suggesting it. Payload as map should be stringified as any other object.

synchronized (this) {
if (message == null && payload == null) {
return this;
}
if (context.isSampled()) {
if (logs == null) {
this.logs = new ArrayList<>();
this.logs = new ArrayList<LogData>();
}

logs.add(new LogData(instantMicroseconds, message, payload));
logs.add(new LogData(timestampMicroseconds, message, payload));
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,33 @@ public static com.uber.jaeger.thriftjava.Span convertSpan(Span span) {
}

protected static List<Log> buildLogs(List<LogData> logs) {
List<Log> jLogs = new ArrayList<>();
List<Log> jLogs = new ArrayList<Log>();
if (logs != null) {
for (LogData logData : logs) {
Log jLog = new Log();
jLog.setTimestamp(logData.getTime());
final Tag tag = buildTag(logData.getMessage(), logData.getPayload());
jLog.setFields(new ArrayList<Tag>() {{add(tag);}});
if (logData.getFields() != null) {
jLog.setFields(buildTags(logData.getFields()));
} else {
List<Tag> tags = new ArrayList<Tag>();
if (logData.getMessage() != null) {
tags.add(buildTag("event", logData.getMessage()));
}
if (logData.getPayload() != null) {
tags.add(buildTag("payload", logData.getPayload()));
}
jLog.setFields(tags);
}
jLogs.add(jLog);
}
}
return jLogs;
}

protected static List<Tag> buildTags(Map<String, Object> tags) {
List<Tag> jTags = new ArrayList<>();
protected static List<Tag> buildTags(Map<String, ?> tags) {
List<Tag> jTags = new ArrayList<Tag>();
if (tags != null) {
for (Map.Entry<String, Object> entry : tags.entrySet()) {
for (Map.Entry<String, ?> entry : tags.entrySet()) {
String tagKey = entry.getKey();
Object tagValue = entry.getValue();
jTags.add(buildTag(tagKey, tagValue));
Expand Down
55 changes: 45 additions & 10 deletions jaeger-core/src/test/java/com/uber/jaeger/SpanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
package com.uber.jaeger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -35,15 +36,15 @@
import org.junit.Before;
import org.junit.Test;

import java.util.Random;
import java.util.HashMap;
import java.util.Map;

public class SpanTest {
private Clock clock;
private InMemoryReporter reporter;
private Tracer tracer;
private Span span;
private InMemoryStatsReporter metricsReporter;
private SpanContext context;

@Before
public void setUp() throws Exception {
Expand All @@ -56,9 +57,6 @@ public void setUp() throws Exception {
.withClock(clock)
.build();
span = (Span) tracer.buildSpan("some-operation").start();

Random rand = new Random();
context = new SpanContext(rand.nextLong(), rand.nextLong(), rand.nextLong(), (byte) 1);
}

@Test
Expand Down Expand Up @@ -191,36 +189,73 @@ public void testOperationName() {
}

@Test
public void testLog() {
public void testLogWithTimestamp() {
long expectedTimestamp = 2222;
String expectedLog = "some-log";
final String expectedLog = "some-log";
final String expectedEvent = "event";
Object expectedPayload = new Object();
Map<String, String> expectedFields = new HashMap<String, String>() {{put(expectedEvent, expectedLog);}};

span.log(expectedTimestamp, expectedLog, expectedPayload);
span.log(expectedTimestamp, expectedEvent);
span.log(expectedTimestamp, expectedFields);
span.log(expectedTimestamp, (String) null);
span.log(expectedTimestamp, (Map<String, ?>) null);

LogData actualLogData = span.getLogs().get(0);

assertEquals(expectedTimestamp, actualLogData.getTime());
assertEquals(expectedLog, actualLogData.getMessage());
assertEquals(expectedPayload, actualLogData.getPayload());

actualLogData = span.getLogs().get(1);

assertEquals(expectedTimestamp, actualLogData.getTime());
assertEquals(expectedEvent, actualLogData.getMessage());
assertNull(actualLogData.getPayload());

actualLogData = span.getLogs().get(2);

assertEquals(expectedTimestamp, actualLogData.getTime());
assertNull(actualLogData.getMessage());
assertNull(actualLogData.getPayload());
assertEquals(expectedFields, actualLogData.getFields());
}

@Test
public void testLogWithTimestamp() {
public void testLog() {
final long expectedTimestamp = 2222;
final String expectedLog = "some-log";
final String expectedEvent = "expectedEvent";
final Object expectedPayload = new Object();
Map<String, String> expectedFields = new HashMap<String, String>() {{put(expectedEvent, expectedLog);}};

when(clock.currentTimeMicros()).thenReturn(expectedTimestamp);

Span span = (Span) tracer.buildSpan("test-service-operation").start();
span.log(expectedLog, expectedPayload);
span.log(expectedEvent);
span.log(expectedFields);
span.log((String) null);
span.log((Map<String, ?>) null);

LogData actualLogData = span.getLogs().get(0);

assertEquals(expectedTimestamp, actualLogData.getTime());
assertEquals(expectedLog, actualLogData.getMessage());
assertEquals(expectedPayload, actualLogData.getPayload());

actualLogData = span.getLogs().get(1);

assertEquals(expectedTimestamp, actualLogData.getTime());
assertEquals(expectedEvent, actualLogData.getMessage());
assertNull(actualLogData.getPayload());

actualLogData = span.getLogs().get(2);

assertEquals(expectedTimestamp, actualLogData.getTime());
assertNull(actualLogData.getMessage());
assertNull(actualLogData.getPayload());
assertEquals(expectedFields, actualLogData.getFields());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testBuildTag(Object tagValue, TagType tagType, Object expected) {

@Test
public void testBuildTags() {
Map<String, Object> tags = new HashMap<>();
Map<String, Object> tags = new HashMap<String, Object>();
tags.put("key", "value");

List<Tag> jTags = JaegerThriftSpanConverter.buildTags(tags);
Expand All @@ -110,18 +110,53 @@ public void testBuildTags() {

@Test
public void testConvertSpan() {
Map<String, Object> fields = new HashMap<String, Object>();
fields.put("k", "v");

Span span = tracer.buildSpan("operation-name").start();
span = span.log(1, "key", "value");
span = span.log(1, fields);

com.uber.jaeger.thriftjava.Span jSpan = JaegerThriftSpanConverter.convertSpan((com.uber.jaeger.Span) span);

assertEquals("operation-name", jSpan.getOperationName());
assertEquals(1, jSpan.getLogs().size());
assertEquals(2, jSpan.getLogs().size());
Log jLog = jSpan.getLogs().get(0);
assertEquals(1, jLog.getTimestamp());
assertEquals(1, jLog.getFields().size());
assertEquals(2, jLog.getFields().size());
Tag jTag = jLog.getFields().get(0);
assertEquals("key", jTag.getKey());
assertEquals("event", jTag.getKey());
assertEquals("key", jTag.getVStr());
jTag = jLog.getFields().get(1);
assertEquals("payload", jTag.getKey());
assertEquals("value", jTag.getVStr());

jLog = jSpan.getLogs().get(1);
assertEquals(1, jLog.getTimestamp());
assertEquals(1, jLog.getFields().size());
jTag = jLog.getFields().get(0);
assertEquals("k", jTag.getKey());
assertEquals("v", jTag.getVStr());
}

@Test
public void testTruncateString() {
String testString =
"k9bHT50f9JNpPUggw3Qz\n" +
"Q1MUhMobIMPA5ItaB3KD\n" +
"vNUoBPRjOpJw2C46vgn3\n" +
"UisXI5KIIH8Wd8uqJ8Wn\n" +
"Z8NVmrcpIBwxc2Qje5d6\n" +
"1mJdQnPMc3VmX1v75As8\n" +
"pUyoicWVPeGEidRuhHpt\n" +
"R1sIR1YNjwtBIy9Swwdq\n" +
"LUIZXdLcPmCvQVPB3cYw\n" +
"VGAtFXG7D8ksLsKw94eY\n" +
"c7PNm74nEV3jIIvlJ217\n" +
"SLBfUBHW6SEjrHcz553i\n" +
"VSjpBvJYXR6CsoEMGce0\n" +
"LqSypCXJHDAzb0DL1w8B\n" +
"kS9g0wCgregSAlq63OIf";
assertEquals(256, JaegerThriftSpanConverter.truncateString(testString).length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.opentracing.NoopTracer;
import io.opentracing.NoopTracerFactory;
import io.opentracing.Tracer;

@JsonIgnoreProperties(ignoreUnknown = true)
Expand All @@ -48,7 +48,7 @@ public Configuration(
@Override
synchronized public Tracer getTracer() {
if (disable) {
return NoopTracer.INSTANCE;
return NoopTracerFactory.create();
}
return super.getTracer();
}
Expand Down