Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Add BaggageRestrictionManager and BaggageSetter #142

Merged
merged 5 commits into from
Aug 21, 2017
Merged

Conversation

black-adder
Copy link
Contributor

subset of: #131

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.05%) to 98.76% when pulling c1da07b on baggage_stuffs into 178dddf on master.

setBaggage(span: Span, key: string, value: string): SpanContext {
let truncated = false;
let prevItem = span.getBaggageItem(key);
if (!this._valid) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Member

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);
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid

@oibe
Copy link
Contributor

oibe commented Jul 31, 2017

Some small nits, and also coverage dropped a bit, but otherwise looks good to me.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.04%) to 98.769% when pulling 2534873 on baggage_stuffs into 178dddf on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 99.239% when pulling 9958178 on baggage_stuffs into f29839b on master.

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) {
Copy link
Contributor

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
);


it ('fail for invalid baggage key', (done) => {
let mgr = new DefaultBaggageRestrictionManager();
let stub = sinon.stub(mgr, 'getRestriction', function(key) {
Copy link
Contributor

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.

let setter = new BaggageSetter(mgr, metrics);
let key = "key";
let value = "value";
let ctx = setter.setBaggage(span, key, value);
Copy link
Contributor

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));
Copy link
Contributor

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);

Copy link
Contributor Author

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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/paylod/payload/

@oibe
Copy link
Contributor

oibe commented Aug 8, 2017

After you address the comments I above I would say "LGTM"

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 99.237% when pulling 26a8e71 on baggage_stuffs into f29839b on master.

@black-adder black-adder requested a review from tiffon August 11, 2017 19:04
Copy link
Member

@tiffon tiffon left a 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.

let prevItem = '';
let restriction = this._restrictionManager.getRestriction(key);
if (!restriction.keyAllowed) {
this._logFields(span, key, value, prevItem, truncated, restriction.keyAllowed);
Copy link
Member

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.

Copy link
Member

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);
Copy link
Member

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/

Copy link
Member

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

let fields: { [key: string]: string } = {
'event': 'baggage',
'key': key,
'value': value,
Copy link
Member

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.

'value': value,
};
if (prevItem) {
fields['override'] = 'true';
Copy link
Member

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;
Copy link
Member

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.

log.fields.forEach((kv) => {
fields[kv.key] = kv.value;
});
assert.equal(fields['event'], 'baggage');
Copy link
Member

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?

tracer.close();
});

it ('fail for invalid baggage key', (done) => {
Copy link
Member

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,
}
Copy link
Member

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?

Copy link
Member

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

done();
});

it ('succeed for valid baggage key', (done) => {
Copy link
Member

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();
Copy link
Member

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.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 99.238% when pulling fda00c4 on baggage_stuffs into f29839b on master.

Copy link
Member

@yurishkuro yurishkuro left a 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;
Copy link
Member

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?

let prevItem = '';
let restriction = this._restrictionManager.getRestriction(key);
if (!restriction.keyAllowed) {
this._logFields(span, key, value, prevItem, truncated, restriction.keyAllowed);
Copy link
Member

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);
Copy link
Member

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;
Copy link
Member

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);
Copy link
Member

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(),
Copy link
Member

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,
}
Copy link
Member

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

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 99.239% when pulling a8f5a44 on baggage_stuffs into f29839b on master.

@black-adder black-adder merged commit 5bd0763 into master Aug 21, 2017
@yurishkuro yurishkuro deleted the baggage_stuffs branch September 27, 2017 17:34
Iuriy-Budnikov pushed a commit to agile-pm/jaeger-client-node that referenced this pull request Sep 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants