-
Notifications
You must be signed in to change notification settings - Fork 232
Add baggage restriction manager #210
Changes from all commits
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,70 @@ | ||
/* | ||
* 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 java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
|
||
public abstract class BaggageRestrictionManager { | ||
static final int DEFAULT_MAX_VALUE_LENGTH = 2048; | ||
static final SanitizedBaggage INVALID_BAGGAGE = SanitizedBaggage.of(false, null, null, null, false); | ||
|
||
public abstract SanitizedBaggage sanitizeBaggage(String key, String value, String prevValue); | ||
|
||
SanitizedBaggage sanitizeBaggage(String key, String value, String prevValue, int maxValueLength) { | ||
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. What is the semantic difference between truncated and sanitized? What other modification could be done aside from truncating? 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. we could be normalizing the key ala: https://github.com/uber/jaeger-client-go/blob/master/span.go#L259 or we might have to obfuscate the value, etc. 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. Doesn't make sense to normalize the key since the user will be expecting to get back the same key that they put in. We may encode the key when serialised, but not here. |
||
TruncatedBaggage baggage = truncateBaggage(value, maxValueLength); | ||
Map<String, String> fields = new HashMap<String, String>(); | ||
fields.put("event", "baggage"); | ||
fields.put("key", key); | ||
fields.put("value", baggage.getTruncatedValue()); | ||
if (baggage.isTruncated()) { | ||
fields.put("truncated", "true"); | ||
} | ||
if (prevValue != null) { | ||
fields.put("override", "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. this is an abstraction leak. Why should baggage manager care about log fields? Also, because there is no span context here, we're creating the map every time even though the span may be unsampled and logs will be no-op. |
||
return SanitizedBaggage.of(true, key, baggage.getTruncatedValue(), fields, | ||
baggage.isTruncated()); | ||
} | ||
|
||
TruncatedBaggage truncateBaggage(String value, int maxValueLength) { | ||
if (value.length() > maxValueLength) { | ||
return new TruncatedBaggage(value.substring(0, maxValueLength), true); | ||
} | ||
return new TruncatedBaggage(value, false); | ||
} | ||
|
||
@Getter(AccessLevel.PACKAGE) | ||
class TruncatedBaggage { | ||
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. we should really consider the performance impact of all these helper classes. Conceptually we're doing this:
Aside from logFields there are no memory allocations here. |
||
final String truncatedValue; | ||
final boolean truncated; | ||
|
||
TruncatedBaggage(String truncatedValue, boolean truncated) { | ||
this.truncatedValue = truncatedValue; | ||
this.truncated = truncated; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.baggage.http.BaggageRestriction; | ||
import com.uber.jaeger.exceptions.BaggageRestrictionException; | ||
|
||
import java.util.List; | ||
|
||
public interface BaggageRestrictionProxy { | ||
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. baggage restriction manager proxy |
||
List<BaggageRestriction> getBaggageRestrictions(String serviceName) | ||
throws BaggageRestrictionException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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; | ||
|
||
public class DefaultBaggageRestrictionManager extends BaggageRestrictionManager { | ||
|
||
@Override | ||
public SanitizedBaggage sanitizeBaggage(String key, String value, String prevValue) { | ||
return sanitizeBaggage(key, value, prevValue, DEFAULT_MAX_VALUE_LENGTH); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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 com.uber.jaeger.utils.Utils.makeGetRequest; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonSyntaxException; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.uber.jaeger.baggage.http.BaggageRestriction; | ||
import com.uber.jaeger.exceptions.BaggageRestrictionException; | ||
|
||
import java.lang.reflect.Type; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.http.client.utils.URIBuilder; | ||
|
||
public class HttpBaggageRestrictionProxy implements BaggageRestrictionProxy { | ||
private static final String DEFAULT_HOST_PORT = "localhost:5778"; | ||
private final URIBuilder builder; | ||
private final Gson gson = new Gson(); | ||
|
||
public HttpBaggageRestrictionProxy(String hostPort) throws URISyntaxException { | ||
hostPort = hostPort != null ? hostPort : DEFAULT_HOST_PORT; | ||
this.builder = new URIBuilder("http://" + hostPort).setPath("/baggageRestrictions"); | ||
} | ||
|
||
List<BaggageRestriction> parseJson(String json) throws BaggageRestrictionException { | ||
try { | ||
Type listType = new TypeToken<ArrayList<BaggageRestriction>>(){}.getType(); | ||
return gson.fromJson(json, listType); | ||
} catch (JsonSyntaxException e) { | ||
throw new BaggageRestrictionException("Cannot deserialize json", e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<BaggageRestriction> getBaggageRestrictions(String serviceName) | ||
throws BaggageRestrictionException { | ||
String jsonString; | ||
try { | ||
jsonString = | ||
makeGetRequest(builder.setParameter("service", serviceName).build()); | ||
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. is the url builder really reusable like that? Especially from multiple threads? |
||
} catch (Exception e) { | ||
throw new BaggageRestrictionException( | ||
"http call to get baggage restriction from local agent failed.", e); | ||
} | ||
return parseJson(jsonString); | ||
} | ||
} |
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.
setBaggageItem
should check thatkey
is not null or empty andvalue
isn't null.