-
Notifications
You must be signed in to change notification settings - Fork 231
Add BaggageRestrictionManager #217
Changes from 1 commit
ea69588
6379066
e29d837
11c6d86
f113e55
df9b653
4af5607
b644bb6
72c1b84
b24d7de
4d5d98b
495a656
72cbdc5
45d2b58
877120b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,5 @@ | |
public abstract class BaggageRestrictionManager { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add |
||
static final int DEFAULT_MAX_VALUE_LENGTH = 2048; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only used in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it'll be used in another manager in the follow up PR |
||
|
||
public abstract BaggageValidity isBaggageValid(String key, String value); | ||
public abstract BaggageSetter getBaggageSetter(String key); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,18 +25,19 @@ | |
import com.uber.jaeger.Span; | ||
import com.uber.jaeger.SpanContext; | ||
import com.uber.jaeger.metrics.Metrics; | ||
import lombok.Value; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import lombok.Value; | ||
|
||
@Value(staticConstructor = "of") | ||
public class BaggageValidity { | ||
public class BaggageSetter { | ||
final boolean valid; | ||
final int maxValueLength; | ||
final Metrics metrics; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @value(staticConstructor = "of") generates a private constructor and a public factory for this class. |
||
|
||
public SpanContext sanitizeBaggage(Span span, String key, String value) { | ||
Metrics metrics = span.getTracer().getMetrics(); | ||
public SpanContext setBaggage(Span span, String key, String value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add javadoc, simply based on the method name, I'd conclude that it simply sets baggage. |
||
if (!this.isValid()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's simpler to check the boolean directly instead of using the getter |
||
metrics.baggageUpdateFailure.inc(1); | ||
this.logFields(span, key, value, null, false, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to use |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,20 +22,21 @@ | |
|
||
package com.uber.jaeger.baggage; | ||
|
||
public class DefaultBaggageRestrictionManager extends BaggageRestrictionManager { | ||
import com.uber.jaeger.metrics.Metrics; | ||
|
||
private final BaggageValidity baggageValidity; | ||
public class DefaultBaggageRestrictionManager extends BaggageRestrictionManager { | ||
private final BaggageSetter baggageSetter; | ||
|
||
public DefaultBaggageRestrictionManager() { | ||
this(DEFAULT_MAX_VALUE_LENGTH); | ||
public DefaultBaggageRestrictionManager(Metrics metrics) { | ||
this(metrics, DEFAULT_MAX_VALUE_LENGTH); | ||
} | ||
|
||
public DefaultBaggageRestrictionManager(int maxValueLength) { | ||
baggageValidity = BaggageValidity.of(true, maxValueLength); | ||
public DefaultBaggageRestrictionManager(Metrics metrics, int maxValueLength) { | ||
baggageSetter = BaggageSetter.of(true, maxValueLength, metrics); | ||
} | ||
|
||
@Override | ||
public BaggageValidity isBaggageValid(String key, String value) { | ||
return baggageValidity; | ||
public BaggageSetter getBaggageSetter(String key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getBaggageSetter is an interface function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discussed this offline: my suggestion doesn't work because the getter requires a |
||
return baggageSetter; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) 2017, Uber Technologies, Inc | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package com.uber.jaeger.baggage; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import com.uber.jaeger.LogData; | ||
import com.uber.jaeger.Span; | ||
import com.uber.jaeger.SpanContext; | ||
import com.uber.jaeger.Tracer; | ||
import com.uber.jaeger.metrics.InMemoryStatsReporter; | ||
import com.uber.jaeger.metrics.Metrics; | ||
import com.uber.jaeger.metrics.StatsFactoryImpl; | ||
import com.uber.jaeger.reporters.InMemoryReporter; | ||
import com.uber.jaeger.samplers.ConstSampler; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class BaggageSetterTest { | ||
|
||
private InMemoryReporter reporter; | ||
private Tracer tracer; | ||
private Span span; | ||
private InMemoryStatsReporter metricsReporter; | ||
private Metrics metrics; | ||
|
||
private static final String KEY = "key"; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
metricsReporter = new InMemoryStatsReporter(); | ||
reporter = new InMemoryReporter(); | ||
metrics = new Metrics(new StatsFactoryImpl(metricsReporter)); | ||
tracer = | ||
new Tracer.Builder("SamplerTest", reporter, new ConstSampler(true)) | ||
.withStatsReporter(metricsReporter) | ||
.withBaggageRestrictionManager(new DefaultBaggageRestrictionManager(metrics, 15)) | ||
.build(); | ||
span = (Span) tracer.buildSpan("some-operation").startManual(); | ||
} | ||
|
||
@Test | ||
public void testInvalidBaggage() { | ||
BaggageSetter setter = BaggageSetter.of(false, 0, metrics); | ||
final String value = "value"; | ||
SpanContext ctx = setter.setBaggage(span, KEY, value); | ||
|
||
assertBaggageLogs(span, KEY, value, false, true); | ||
assertNull(ctx.getBaggageItem(KEY)); | ||
|
||
assertEquals( | ||
1L, metricsReporter.counters.get("jaeger.baggage-update.result=err").longValue()); | ||
} | ||
|
||
@Test | ||
public void testTruncatedBaggage() { | ||
BaggageSetter setter = BaggageSetter.of(true, 5, metrics); | ||
final String value = "0123456789"; | ||
final String expected = "01234"; | ||
SpanContext ctx = setter.setBaggage(span, KEY, value); | ||
|
||
assertBaggageLogs(span, KEY, expected, true, false); | ||
assertEquals(expected, ctx.getBaggageItem(KEY)); | ||
|
||
assertEquals( | ||
1L, metricsReporter.counters.get("jaeger.baggage-truncate").longValue()); | ||
assertEquals( | ||
1L, metricsReporter.counters.get("jaeger.baggage-update.result=ok").longValue()); | ||
} | ||
|
||
private void assertBaggageLogs( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like testing concerns aren't separated between SpanTest and BaggageSetter. Ideally, we would only do the following in SpanTest All of the detailed test cases that only involve setting baggage should be under the purview of |
||
Span span, | ||
String key, | ||
String value, | ||
boolean truncate, | ||
boolean invalid | ||
) { | ||
List<LogData> logs = span.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 (truncate) { | ||
assertEquals("true", fields.get("truncated")); | ||
} | ||
if (invalid) { | ||
assertEquals("true", fields.get("invalid")); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This introduces a bug; the instance of metrics used by
DefaultBaggageRestrictionManager
would always be the one set in L453.