-
Notifications
You must be signed in to change notification settings - Fork 232
Add BaggageRestrictionManager #217
Changes from 9 commits
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* 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; | ||
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 BaggageSetter getBaggageSetter(String key); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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 com.uber.jaeger.Span; | ||
import com.uber.jaeger.SpanContext; | ||
import com.uber.jaeger.metrics.Metrics; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
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; | ||
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. |
||
|
||
/** | ||
* 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. | ||
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. s/is valid/is allowed to be set by this service/ |
||
* <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) { | ||
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 (!valid) { | ||
metrics.baggageUpdateFailure.inc(1); | ||
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. if you move prevItem and truncated declarations to the top, this could be made a lot more readable: |
||
return span.context(); | ||
} | ||
boolean truncated = false; | ||
if (value.length() > maxValueLength) { | ||
truncated = true; | ||
value = value.substring(0, maxValueLength); | ||
metrics.baggageTruncate.inc(1); | ||
} | ||
|
||
String prevItem = span.getBaggageItem(key); | ||
logFields(span, key, value, prevItem, truncated, false); | ||
SpanContext context = span.context().withBaggageItem(key, value); | ||
metrics.baggageUpdateSuccess.inc(1); | ||
return context; | ||
} | ||
|
||
private void logFields(Span span, String key, String value, String prevItem, boolean truncated, boolean invalid) { | ||
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. avoid negatives - s/invalid/valid/ |
||
if (span.context().isSampled()) { | ||
Map<String, String> fields = new HashMap<String, String>(); | ||
fields.put("event", "baggage"); | ||
fields.put("key", key); | ||
fields.put("value", value); | ||
if (prevItem != null) { | ||
fields.put("override", "true"); | ||
} | ||
if (truncated) { | ||
fields.put("truncated", "true"); | ||
} | ||
if (invalid) { | ||
fields.put("invalid", "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. why are you logging these as strings instead of booleans? 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. ah, nvm |
||
} | ||
span.log(fields); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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 com.uber.jaeger.metrics.Metrics; | ||
|
||
public class DefaultBaggageRestrictionManager extends BaggageRestrictionManager { | ||
private final BaggageSetter baggageSetter; | ||
|
||
public DefaultBaggageRestrictionManager(Metrics metrics) { | ||
this(metrics, DEFAULT_MAX_VALUE_LENGTH); | ||
} | ||
|
||
public DefaultBaggageRestrictionManager(Metrics metrics, int maxValueLength) { | ||
baggageSetter = BaggageSetter.of(true, maxValueLength, metrics); | ||
} | ||
|
||
@Override | ||
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; | ||
} | ||
} |
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.
Could you add
javadocs
on what this is?