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

Commit

Permalink
Save baggage in span
Browse files Browse the repository at this point in the history
  • Loading branch information
black-adder committed May 24, 2017
1 parent 11026c7 commit 655aaec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions jaeger-core/src/main/java/com/uber/jaeger/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ public List<LogData> getLogs() {
public Span setBaggageItem(String key, String value) {
synchronized (this) {
this.context = this.context.withBaggageItem(key, value);
if (context.isSampled()) {
Map<String, String> fields = new HashMap<String, String>();
fields.put("event", "baggage");
fields.put("key", key);
fields.put("value", value);
return this.log(fields);
}
}
return this;
}
Expand Down
20 changes: 20 additions & 0 deletions jaeger-core/src/test/java/com/uber/jaeger/SpanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.opentracing.tag.Tags;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Before;
Expand Down Expand Up @@ -77,6 +78,9 @@ public void testSetAndGetBaggageItem() {
String key = "some.BAGGAGE";
span.setBaggageItem(key, expected);
assertEquals(expected, span.getBaggageItem(key));

// Ensure the baggage was logged
this.assertBaggageLogs(span, key, expected);
}

@Test
Expand Down Expand Up @@ -291,12 +295,14 @@ public void testSpanDetectsSamplingPriorityLessThanZero() {
public void testBaggageOneReference() {
io.opentracing.Span parent = tracer.buildSpan("foo").start();
parent.setBaggageItem("foo", "bar");
this.assertBaggageLogs(parent, "foo", "bar");

io.opentracing.Span child = tracer.buildSpan("foo")
.asChildOf(parent)
.start();

child.setBaggageItem("a", "a");
this.assertBaggageLogs(child, "a", "a");

assertNull(parent.getBaggageItem("a"));
assertEquals("a", child.getBaggageItem("a"));
Expand All @@ -307,15 +313,18 @@ public void testBaggageOneReference() {
public void testBaggageMultipleReferences() {
io.opentracing.Span parent1 = tracer.buildSpan("foo").start();
parent1.setBaggageItem("foo", "bar");
this.assertBaggageLogs(parent1, "foo", "bar");
io.opentracing.Span parent2 = tracer.buildSpan("foo").start();
parent2.setBaggageItem("foo2", "bar");
this.assertBaggageLogs(parent2, "foo2", "bar");

io.opentracing.Span child = tracer.buildSpan("foo")
.asChildOf(parent1)
.addReference(References.FOLLOWS_FROM, parent2.context())
.start();

child.setBaggageItem("a", "a");
this.assertBaggageLogs(child, "a", "a");

assertNull(parent1.getBaggageItem("a"));
assertNull(parent2.getBaggageItem("a"));
Expand All @@ -338,4 +347,15 @@ public void testImmutableBaggage() {
baggageIter.next();
assertFalse(baggageIter.hasNext());
}

private void assertBaggageLogs(io.opentracing.Span span, String key, String value) {
Span sp = (Span)span;
List<LogData> logs = sp.getLogs();
assertEquals(1, logs.size());
Map<String, ?> fields = logs.get(0).getFields();
assertEquals(3, fields.size());
assertEquals("baggage", fields.get("event"));
assertEquals(key, fields.get("key"));
assertEquals(value, fields.get("value"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ public void testConvertSpan() {
Span span = tracer.buildSpan("operation-name").start();
span = span.log(1, "key", "value");
span = span.log(1, fields);
span = span.setBaggageItem("foo", "bar");

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

assertEquals("operation-name", thriftSpan.getOperationName());
assertEquals(2, thriftSpan.getLogs().size());
assertEquals(3, thriftSpan.getLogs().size());
Log thriftLog = thriftSpan.getLogs().get(0);
assertEquals(1, thriftLog.getTimestamp());
assertEquals(2, thriftLog.getFields().size());
Expand All @@ -145,6 +146,18 @@ public void testConvertSpan() {
thriftTag = thriftLog.getFields().get(0);
assertEquals("k", thriftTag.getKey());
assertEquals("v", thriftTag.getVStr());

thriftLog = thriftSpan.getLogs().get(2);
assertEquals(3, thriftLog.getFields().size());
thriftTag = thriftLog.getFields().get(0);
assertEquals("event", thriftTag.getKey());
assertEquals("baggage", thriftTag.getVStr());
thriftTag = thriftLog.getFields().get(1);
assertEquals("value", thriftTag.getKey());
assertEquals("bar", thriftTag.getVStr());
thriftTag = thriftLog.getFields().get(2);
assertEquals("key", thriftTag.getKey());
assertEquals("foo", thriftTag.getVStr());
}

@Test
Expand Down

0 comments on commit 655aaec

Please sign in to comment.