-
Notifications
You must be signed in to change notification settings - Fork 131
Add BaggageRestrictionManager and BaggageSetter #142
Conversation
src/baggage/baggage_setter.js
Outdated
setBaggage(span: Span, key: string, value: string): SpanContext { | ||
let truncated = false; | ||
let prevItem = span.getBaggageItem(key); | ||
if (!this._valid) { |
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.
I find this use of this._valid confusing. Where is this._valid set? what makes baggage 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.
The collectors will have a list of valid baggage keys and their maximum size. The clients will retrieve these restrictions once a minute. Anytime the client writes baggage, it'll be validated against these rules. If invalid, the baggage is logged in the span but is not propagated.
The remote fetching of baggage rules will come in a future PR. Right now, the DefaultBaggageRestrictionManager will return a BaggageSetter that allows all baggage. ie all baggage is valid.
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.
it's because a better way would be to have different implementations for the Setter
|
||
constructor(metrics: Metrics, maxValueLength: ?number) { | ||
maxValueLength = maxValueLength || DEFAULT_MAX_VALUE_LENGTH; | ||
this._baggageSetter = new BaggageSetter(true, maxValueLength, metrics); |
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.
nit: Me and you go back and forth on this a lot, but personally I would prefer.
let valid = true;
this._baggageSetter = new BaggageSetter(valid, maxValueLength, metrics);
As of now when I read this code I have to jump to BaggageSetter to know what true means, whereas everything else has a variable name. Anyway really a nit.
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.
valid
Some small nits, and also coverage dropped a bit, but otherwise looks good to me. |
2534873
to
9958178
Compare
src/tracer.js
Outdated
@@ -70,6 +75,11 @@ export default class Tracer { | |||
this._reporter = reporter; | |||
this._sampler = sampler; | |||
this._logger = options.logger || new NullLogger(); | |||
if (options.baggageRestrictionManager) { |
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 could be simpler written as:
this._baggageSetter = new BaggageSetter(
options.baggageRestrictionManager || new DefaultBaggageRestrictionManager(),
this._metrics
);
test/baggage/baggage_setter.js
Outdated
|
||
it ('fail for invalid baggage key', (done) => { | ||
let mgr = new DefaultBaggageRestrictionManager(); | ||
let stub = sinon.stub(mgr, 'getRestriction', function(key) { |
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.
I don't think you need "let stub", since the variable is never used.
test/baggage/baggage_setter.js
Outdated
let setter = new BaggageSetter(mgr, metrics); | ||
let key = "key"; | ||
let value = "value"; | ||
let ctx = setter.setBaggage(span, key, value); |
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.
nit: s/ctx/spanContext/
|
||
it ('return a Restriction', (done) => { | ||
let mgr = new DefaultBaggageRestrictionManager(); | ||
assert.deepEqual(mgr.getRestriction("key"), new Restriction(true, 2048)); |
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.
If the default restrictions change this test will change.
Perhaps: assert.deepEqual(mgr.getRestriction("key"), mgr._restriction);
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.
I believe for things like this we would want it to break because what you're suggesting is tantamount to this.var == this.getVar.
I remember there being a informative Google Testing Blog entry about this but can't find it.
test/span.js
Outdated
@@ -149,7 +139,7 @@ describe('span should', () => { | |||
assert.equal(span._logs[0].fields[0].value, event); | |||
}); | |||
|
|||
it('add logs with paylaod', () => { | |||
it('add logs with paylod', () => { |
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.
s/paylod/payload/
After you address the comments I above I would say "LGTM" |
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.
Great work. Had a few questions and suggestions.
src/baggage/baggage_setter.js
Outdated
let prevItem = ''; | ||
let restriction = this._restrictionManager.getRestriction(key); | ||
if (!restriction.keyAllowed) { | ||
this._logFields(span, key, value, prevItem, truncated, restriction.keyAllowed); |
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.
I don't have much context for this, if the key is not allowed, should the value be logged? I'm just thinking one reason for a restriction (bearing in mind I'm not familiar with the feature) might be to protect sensitive information.
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.
I don't think sensitive info is a concern - we cannot protect against it in case the user calls the general span.log() method, so why worry about protecting here.
} | ||
if (value.length > restriction.maxValueLength) { | ||
truncated = true; | ||
value = value.substring(0, restriction.maxValueLength); |
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.
It's generally discourage to reassign function parameters, although in this case it's not specifically a problem. As a practice, I generally avoid it.
http://airbnb.io/javascript/#functions--reassign-params
https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/
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.
seems like a stale comment, since value
is a local var now
src/baggage/baggage_setter.js
Outdated
let fields: { [key: string]: string } = { | ||
'event': 'baggage', | ||
'key': key, | ||
'value': value, |
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.
Single quotes around 'event'
, etc., shouldn't be necessary.
src/baggage/baggage_setter.js
Outdated
'value': value, | ||
}; | ||
if (prevItem) { | ||
fields['override'] = 'true'; |
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.
I believe this can be written as fields.override = 'true';
_restriction: Restriction; | ||
|
||
constructor(maxValueLength: ?number) { | ||
maxValueLength = maxValueLength || DEFAULT_MAX_VALUE_LENGTH; |
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.
See suggestion above about reassigning parameters.
test/baggage/baggage_setter.js
Outdated
log.fields.forEach((kv) => { | ||
fields[kv.key] = kv.value; | ||
}); | ||
assert.equal(fields['event'], 'baggage'); |
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.
Just curious, why not use fields.event
?
test/baggage/baggage_setter.js
Outdated
tracer.close(); | ||
}); | ||
|
||
it ('fail for invalid baggage key', (done) => { |
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.
Re done
, is this an async test?
new ConstSampler(true), | ||
{ | ||
metrics: metrics, | ||
} |
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.
With the babel transformations, does { metrics }
work?
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.
perhaps, but certainly makes the code more cryptic
test/baggage/baggage_setter.js
Outdated
done(); | ||
}); | ||
|
||
it ('succeed for valid baggage key', (done) => { |
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.
Test case description doesn't mention truncation is also being verified.
it ('return a Restriction', (done) => { | ||
let mgr = new DefaultBaggageRestrictionManager(); | ||
assert.deepEqual(mgr.getRestriction("key"), new Restriction(true, 2048)); | ||
done(); |
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.
See question above about async test case.
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.
lgtm
* allowed and any other applicable restrictions on the baggage value. | ||
*/ | ||
declare interface BaggageRestrictionManager { | ||
getRestriction(key: string): Restriction; |
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.
do you want to add the service name here, similar to Go client?
src/baggage/baggage_setter.js
Outdated
let prevItem = ''; | ||
let restriction = this._restrictionManager.getRestriction(key); | ||
if (!restriction.keyAllowed) { | ||
this._logFields(span, key, value, prevItem, truncated, restriction.keyAllowed); |
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.
I don't think sensitive info is a concern - we cannot protect against it in case the user calls the general span.log() method, so why worry about protecting here.
} | ||
if (value.length > restriction.maxValueLength) { | ||
truncated = true; | ||
value = value.substring(0, restriction.maxValueLength); |
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.
seems like a stale comment, since value
is a local var now
@@ -36,6 +36,7 @@ export default class Span { | |||
_tags: Array<Tag>; | |||
static _baggageHeaderCache: any; |
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.
+1, I did't even know JS had a Map. But I'd leave it out of this diff, it sounds like a global refactoring
// frequently than items being added to the baggage. | ||
this._spanContext = this._spanContext.withBaggageItem(normalizedKey, value); | ||
this._spanContext = this._baggageSetter.setBaggage(this, normalizedKey, value); |
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.
we need to get rid of normalizing baggage keys altogether, but it's a larger change across all clients (https://github.com/uber/jaeger/issues/346)
src/tracer.js
Outdated
@@ -70,6 +75,8 @@ export default class Tracer { | |||
this._reporter = reporter; | |||
this._sampler = sampler; | |||
this._logger = options.logger || new NullLogger(); | |||
this._baggageSetter = new BaggageSetter(options.baggageRestrictionManager || new DefaultBaggageRestrictionManager(), |
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.
maybe add \n
after new BaggageSetter(
for better readability
new ConstSampler(true), | ||
{ | ||
metrics: metrics, | ||
} |
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.
perhaps, but certainly makes the code more cryptic
subset of: #131