-
Notifications
You must be signed in to change notification settings - Fork 232
Add BaggageRestrictionManager #217
Changes from 2 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 |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
|
||
package com.uber.jaeger; | ||
|
||
import com.uber.jaeger.baggage.BaggageValidity; | ||
import io.opentracing.tag.Tags; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
@@ -117,21 +118,13 @@ public List<LogData> getLogs() { | |
@Override | ||
public Span setBaggageItem(String key, String value) { | ||
synchronized (this) { | ||
// TODO emit a metric whenever baggage is updated | ||
String prevItem = this.getBaggageItem(key); | ||
this.context = this.context.withBaggageItem(key, value); | ||
if (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"); | ||
} | ||
return this.log(fields); | ||
if (key == null || value == null) { | ||
return this; | ||
} | ||
BaggageValidity baggageValidity = this.getTracer().getBaggageRestrictionManager().isBaggageValid(key, 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. how about
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. or |
||
this.context = baggageValidity.sanitizeBaggage(this, key, value); | ||
return this; | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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 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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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 lombok.Value; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Value(staticConstructor = "of") | ||
public class BaggageValidity { | ||
final boolean valid; | ||
final int maxValueLength; | ||
|
||
public SpanContext sanitizeBaggage(Span span, String key, String value) { | ||
Metrics metrics = span.getTracer().getMetrics(); | ||
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. I'd initialize the metrics object by accepting a Metrics when creating an instance of the class. There is no necessity to retrieve it once per method call. See also: Law of Demeter |
||
if (!this.isValid()) { | ||
metrics.baggageUpdateFailure.inc(1); | ||
this.logFields(span, key, value, null, false, true); | ||
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); | ||
this.logFields(span, key, value, prevItem, truncated, false); | ||
SpanContext context = span.context().withBaggageItem(key, 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. I have to create the new context and pass it back given the setter isn't public. Also the BaggageValidity class reeks of feature envy. I could move this file down to the same namespace as Span and make the context setter protected or I could remove the functionality in this class completely and inline it in Span.setBaggageItem() |
||
metrics.baggageUpdateSuccess.inc(1); | ||
return context; | ||
} | ||
|
||
void logFields(Span span, String key, String value, String prevItem, boolean truncated, boolean invalid) { | ||
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"); | ||
} | ||
span.log(fields); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 { | ||
|
||
private final BaggageValidity baggageValidity; | ||
|
||
public DefaultBaggageRestrictionManager() { | ||
this(DEFAULT_MAX_VALUE_LENGTH); | ||
} | ||
|
||
public DefaultBaggageRestrictionManager(int maxValueLength) { | ||
baggageValidity = BaggageValidity.of(true, maxValueLength); | ||
} | ||
|
||
@Override | ||
public BaggageValidity isBaggageValid(String key, String value) { | ||
return baggageValidity; | ||
} | ||
} |
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 test doesn't need to be synchronized.