-
Notifications
You must be signed in to change notification settings - Fork 819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: specification compliant baggage #1876
Conversation
@open-telemetry/javascript-approvers this one is important because it will be a part of the API 1.0 and is a pretty drastic change from what we had. Please look over it. |
Some tests would be nice. And please update the breaking changes section in readme. |
I was just working on tests :) |
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.
First pass - commented on few issues.
I think we should have unit tests for cases when baggage is separated with spaces
https://www.w3.org/TR/baggage/#examples-of-http-headers
/** | ||
* Get a list of all entries in the Baggage | ||
*/ | ||
getAllEntries(): [string, BaggageEntry][]; |
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.
ImhogetAllEntries
should return map of getEntry
. Because getEntry
returns BaggageEntry
it should return Map<key, BaggageEntry>
not [string, BaggageEntry][]
. Or name the function for example getEntriesAsArray
etc. WDYT ?
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 just thought the most common reason to use getAllEntries would be to iterate over all of them for serialization. I can change it to map if you prefer. This is especially easy since order doesn't matter in 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.
Generally this looks good and appears to be on the right track. One thing that bothered me when I was working on the Ruby version of this, is that you need to create a new baggage instance for each modification of baggage. I wanted a way to be able to make multiple changes and then create a new baggage instance with those changes applied. To do this, I introduced a builder.
For example, you have to create three baggage instances in order to remove one key and add two new keys:
const updated = baggage.removeEntry('foo)
.setEntry('bar', { value: 'baz' })
.setEntry('baz', { value: 'quux' });
With a builder, you can batch changes and return a baggage instance with the changes applied. For example:
baggage.build(b => {
b.removeEntry('foo);
b.setEntry('bar', { value: 'baz' });
b.setEntry('baz', { value: 'quux' });
});
I'm not sure if it's essential to have something like this, and it can always be added later, but I thought it'd be worth mentioning.
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.
One thing which confuses me most is why this._entries
are never changed during all operations. Shouldn't the entries be treated as the baggage state from which you can remove or add ?. As this way if I have this
const baggage = new BaggageImpl();
baggage.setEntry('a',{b:1});
console.log(baggage.getEntry('a')); // undefined
Wheres this baggage
should return the entry that has been just set.
I checked java for example and they are removing or adding this too.
https://github.com/open-telemetry/opentelemetry-java/blob/main/api/all/src/main/java/io/opentelemetry/api/baggage/ImmutableBaggage.java#L98
Python is doing similar thing
https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/baggage/__init__.py#L76
But instead of keeping the state of baggage inside class they are updating the baggage directly in context - so context is required. But still you have one place where the baggage state is kept and entries are being removed or added.
We can keep the baggage state inside baggage or in context but I think functions should modify the state anyway so that you can reference the baggage and have updated state of it whenever it changes.
After thinking for a while I'm hesitant to say that keeping state of baggage in context as python did makes a lot of sense, WDYT ? |
Will address @obecny's comments first
Spec states baggage is immutable:
Both of these are not doing what you think. In python, they create a new dict object and modify only the new one. They also create a new context. In java, you linked the builder. After a context has been built it is immutable.
Functions cannot modify the state. See the link in the first point |
I added a I think creating a builder might be something we can consider later, but for now we need to get a spec compliant baggage out the door so it doesn't block GA. |
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
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
This is a pretty big change to baggage making it spec compliant, but fortunately baggage isn't used in too many places.