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

Commit

Permalink
wtf
Browse files Browse the repository at this point in the history
  • Loading branch information
black-adder committed Jul 26, 2017
1 parent b644bb6 commit af37bfb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@

package com.uber.jaeger.baggage;

/**
* BaggageRestrictionManager is an abstract class that manages baggage
* restrictions for baggage keys. The manager will return a {@link BaggageSetter}
* for a specific baggage key which will set the baggage on the {@link com.uber.jaeger.Span}
* given the baggage restrictions for that key.
*/
public abstract class BaggageRestrictionManager {
static final int DEFAULT_MAX_VALUE_LENGTH = 2048;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,34 @@

import lombok.Value;

/**
* BaggageSetter is a class that sets baggage and the logs associated
* with the baggage on a given {@link Span}.
*/
@Value(staticConstructor = "of")
public class BaggageSetter {
final boolean valid;
final int maxValueLength;
final Metrics metrics;

/**
* Sets the baggage key:value on the {@link Span} and the corresponding
* logs. Whether the baggage is set on the span depends on if the key
* is valid.
* <p>
* A {@link SpanContext} is returned with the new baggage key:value set
* if key is valid, else returns the existing {@link SpanContext}
* on the {@link Span}.
*
* @param span the span to set the baggage on
* @param key the baggage key to set
* @param value the baggage value to set
* @return the SpanContext with the baggage set
*/
public SpanContext setBaggage(Span span, String key, String value) {
if (!this.isValid()) {
if (valid) {
metrics.baggageUpdateFailure.inc(1);
this.logFields(span, key, value, null, false, true);
logFields(span, key, value, null, false, true);
return span.context();
}
boolean truncated = false;
Expand All @@ -51,7 +69,7 @@ public SpanContext setBaggage(Span span, String key, String value) {
}

String prevItem = span.getBaggageItem(key);
this.logFields(span, key, value, prevItem, truncated, false);
logFields(span, key, value, prevItem, truncated, false);
SpanContext context = span.context().withBaggageItem(key, value);
metrics.baggageUpdateSuccess.inc(1);
return context;
Expand Down
68 changes: 11 additions & 57 deletions jaeger-core/src/test/java/com/uber/jaeger/SpanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.uber.jaeger.baggage.BaggageRestrictionManager;
Expand All @@ -46,6 +47,8 @@
import java.util.Map.Entry;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

public class SpanTest {
private Clock clock;
Expand All @@ -65,7 +68,7 @@ public void setUp() throws Exception {
new Tracer.Builder("SamplerTest", reporter, new ConstSampler(true))
.withStatsReporter(metricsReporter)
.withClock(clock)
.withBaggageRestrictionManager(new DefaultBaggageRestrictionManager(metrics, 15))
.withBaggageRestrictionManager(new DefaultBaggageRestrictionManager(metrics))
.build();
span = (Span) tracer.buildSpan("some-operation").startManual();
}
Expand All @@ -82,41 +85,20 @@ public void testSpanMetrics() {

@Test
public void testSetAndGetBaggageItem() {
String value = "01234567890123456789";
String expected = "012345678901234";
String key = "some.BAGGAGE";
span.setBaggageItem(key, value);
assertEquals(expected, span.getBaggageItem(key));

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

assertEquals(
1L, metricsReporter.counters.get("jaeger.baggage-truncate").longValue());
assertEquals(
1L, metricsReporter.counters.get("jaeger.baggage-update.result=ok").longValue());
}

@Test
public void testInvalidBaggageKey() {
String key = "some.BAGGAGE";
BaggageRestrictionManager mgr = mock(BaggageRestrictionManager.class);

final BaggageRestrictionManager mgr = Mockito.mock(DefaultBaggageRestrictionManager.class);
tracer =
new Tracer.Builder("SamplerTest", reporter, new ConstSampler(true))
.withStatsReporter(metricsReporter)
.withClock(clock)
.withBaggageRestrictionManager(mgr)
.build();
span = (Span) tracer.buildSpan("some-operation").startManual();

when(mgr.getBaggageSetter(key)).thenReturn(BaggageSetter.of(false, 0, metrics));

span.setBaggageItem(key, "luggage");
assertNull(span.getBaggageItem(key));

assertEquals(
1L, metricsReporter.counters.get("jaeger.baggage-update.result=err").longValue());
final String key = "key";
final String value = "value";
when(mgr.getBaggageSetter(key)).thenReturn(BaggageSetter.of(true, 10, metrics));
span.setBaggageItem(key, "value");
verify(mgr).getBaggageSetter(key);
assertEquals(value, span.getBaggageItem(key));
}

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

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

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

assertNull(parent.getBaggageItem("a"));
assertEquals("a", child.getBaggageItem("a"));
Expand All @@ -349,20 +329,16 @@ public void testBaggageOneReference() {
public void testBaggageMultipleReferences() {
io.opentracing.Span parent1 = tracer.buildSpan("foo").startManual();
parent1.setBaggageItem("foo", "bar");
this.assertBaggageLogs(parent1, "foo", "bar", false, false);
io.opentracing.Span parent2 = tracer.buildSpan("foo").startManual();
parent2.setBaggageItem("foo2", "bar");
this.assertBaggageLogs(parent2, "foo2", "bar", false, false);

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

child.setBaggageItem("a", "a");
this.assertBaggageLogs(child, "a", "a", false, false);
child.setBaggageItem("foo2", "b");
this.assertBaggageLogs(child, "foo2", "b", true, false);

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

private void assertBaggageLogs(
io.opentracing.Span span,
String key,
String value,
boolean override,
boolean truncate
) {
Span sp = (Span)span;
List<LogData> logs = sp.getLogs();
assertEquals(false, logs.isEmpty());
Map<String, ?> fields = logs.get(logs.size() - 1).getFields();
assertEquals("baggage", fields.get("event"));
assertEquals(key, fields.get("key"));
assertEquals(value, fields.get("value"));
if (override) {
assertEquals("true", fields.get("override"));
}
if (truncate) {
assertEquals("true", fields.get("truncated"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testInvalidBaggage() {
final String value = "value";
SpanContext ctx = setter.setBaggage(span, KEY, value);

assertBaggageLogs(span, KEY, value, false, true);
assertBaggageLogs(span, KEY, value, false, false, true);
assertNull(ctx.getBaggageItem(KEY));

assertEquals(
Expand All @@ -84,7 +84,7 @@ public void testTruncatedBaggage() {
final String expected = "01234";
SpanContext ctx = setter.setBaggage(span, KEY, value);

assertBaggageLogs(span, KEY, expected, true, false);
assertBaggageLogs(span, KEY, expected, true, false, false);
assertEquals(expected, ctx.getBaggageItem(KEY));

assertEquals(
Expand All @@ -93,11 +93,27 @@ public void testTruncatedBaggage() {
1L, metricsReporter.counters.get("jaeger.baggage-update.result=ok").longValue());
}

@Test
public void testOverrideBaggage() {
BaggageSetter setter = BaggageSetter.of(true, 5, metrics);
final String value = "value";
SpanContext ctx = setter.setBaggage(span, KEY, value);
Span child = (Span) tracer.buildSpan("some-operation").asChildOf(ctx).startManual();
ctx = setter.setBaggage(child, KEY, value);

assertBaggageLogs(child, KEY, value, false, true, false);
assertEquals(value, ctx.getBaggageItem(KEY));

assertEquals(
2L, metricsReporter.counters.get("jaeger.baggage-update.result=ok").longValue());
}

private void assertBaggageLogs(
Span span,
String key,
String value,
boolean truncate,
boolean override,
boolean invalid
) {
List<LogData> logs = span.getLogs();
Expand All @@ -109,6 +125,9 @@ private void assertBaggageLogs(
if (truncate) {
assertEquals("true", fields.get("truncated"));
}
if (override) {
assertEquals("true", fields.get("override"));
}
if (invalid) {
assertEquals("true", fields.get("invalid"));
}
Expand Down

0 comments on commit af37bfb

Please sign in to comment.