This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
Add BaggageRestrictionManager #217
Merged
black-adder
merged 15 commits into
jaegertracing:master
from
black-adder:Add_BaggageRestrictionManager
Aug 10, 2017
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ea69588
Add BaggageRestrictionManager
black-adder 6379066
feature envy
black-adder e29d837
comments
black-adder 11c6d86
too many spaces
black-adder f113e55
meh
black-adder df9b653
wow
black-adder 4af5607
fix variable name
black-adder b644bb6
private
black-adder 72c1b84
oops
black-adder b24d7de
make setter more reasonable
black-adder 4d5d98b
lil clean up
black-adder 495a656
revert some of the previous changes that added the key directly to th…
black-adder 72cbdc5
new API
black-adder 45d2b58
address comments
black-adder 877120b
Merge branch 'master' into Add_BaggageRestrictionManager
black-adder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageRestrictionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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 interface for a class that manages baggage | ||
* restrictions for baggage keys. The manager will return a {@link Restriction} | ||
* for a specific baggage key which will determine whether the baggage key is | ||
* allowed for the current service and any other applicable restrictions on the | ||
* baggage value. | ||
*/ | ||
public interface BaggageRestrictionManager { | ||
int DEFAULT_MAX_VALUE_LENGTH = 2048; | ||
|
||
Restriction getRestriction(String service, String key); | ||
} |
99 changes: 99 additions & 0 deletions
99
jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.Metric; | ||
import com.uber.jaeger.metrics.Metrics; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* BaggageSetter is a class that sets baggage and the logs associated | ||
* with the baggage on a given {@link Span}. | ||
*/ | ||
public class BaggageSetter { | ||
|
||
private final BaggageRestrictionManager restrictionManager; | ||
private final Metrics metrics; | ||
|
||
public BaggageSetter(BaggageRestrictionManager restrictionManager, Metrics metrics) { | ||
this.restrictionManager = restrictionManager; | ||
this.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 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 value the baggage value to set | ||
* @return the SpanContext with the baggage set | ||
*/ | ||
public SpanContext setBaggage(Span span, String key, String value) { | ||
Restriction restriction = restrictionManager.getRestriction(span.getTracer().getServiceName(), key); | ||
boolean truncated = false; | ||
String prevItem = null; | ||
if (!restriction.isKeyAllowed()) { | ||
metrics.baggageUpdateFailure.inc(1); | ||
logFields(span, key, value, prevItem, truncated, restriction.isKeyAllowed()); | ||
return span.context(); | ||
} | ||
if (value.length() > restriction.getMaxValueLength()) { | ||
truncated = true; | ||
value = value.substring(0, restriction.getMaxValueLength()); | ||
metrics.baggageTruncate.inc(1); | ||
} | ||
prevItem = span.getBaggageItem(key); | ||
logFields(span, key, value, prevItem, truncated, restriction.isKeyAllowed()); | ||
metrics.baggageUpdateSuccess.inc(1); | ||
return span.context().withBaggageItem(key, value); | ||
} | ||
|
||
private void logFields(Span span, String key, String value, String prevItem, boolean truncated, boolean valid) { | ||
if (!span.context().isSampled()) { | ||
return; | ||
} | ||
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 (!valid) { | ||
fields.put("invalid", "true"); | ||
} | ||
span.log(fields); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
jaeger-core/src/main/java/com/uber/jaeger/baggage/DefaultBaggageRestrictionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* DefaultBaggageRestrictionManager is a manager that returns a {@link Restriction} | ||
* that allows all baggage. | ||
*/ | ||
public class DefaultBaggageRestrictionManager implements BaggageRestrictionManager { | ||
private final Restriction restriction; | ||
|
||
public DefaultBaggageRestrictionManager() { | ||
this(DEFAULT_MAX_VALUE_LENGTH); | ||
} | ||
|
||
DefaultBaggageRestrictionManager(int maxValueLength) { | ||
this.restriction = Restriction.of(true, maxValueLength); | ||
} | ||
|
||
@Override | ||
public Restriction getRestriction(String service, String key) { | ||
return restriction; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
jaeger-core/src/main/java/com/uber/jaeger/baggage/Restriction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
import lombok.Value; | ||
|
||
/** | ||
* Restriction determines whether a baggage key is allowed and contains any | ||
* restrictions on the baggage value. | ||
*/ | ||
@Value(staticConstructor = "of") | ||
public class Restriction { | ||
boolean keyAllowed; | ||
int maxValueLength; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add javadoc, simply based on the method name, I'd conclude that it simply sets baggage.