Skip to content
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

Baggage API : refactor trace_state, and add Baggage implementation. #610

Closed
wants to merge 3 commits into from

Conversation

lalitb
Copy link
Member

@lalitb lalitb commented Mar 15, 2021

Fixes #79

Changes

Please provide a brief description of the changes here.

  • Refactor TraceState implementation
  • Model Baggage API close to TraceState, and both sharing same internal data structure of key-value list.

Converting to draft for some more refactoring, and add more tests.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@lalitb lalitb requested a review from a team March 15, 2021 18:44
@lalitb lalitb changed the title Baggage API : refactor trace_state code, and reuse internal data structure of key-value list for Baggage. Baggage API : refactor trace_state, and add Baggage implementation. Mar 15, 2021
@lalitb lalitb marked this pull request as draft March 15, 2021 18:45
@codecov
Copy link

codecov bot commented Mar 15, 2021

Codecov Report

Merging #610 (7aaa78a) into main (1e5751e) will decrease coverage by 0.00%.
The diff coverage is 98.38%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #610      +/-   ##
==========================================
- Coverage   94.45%   94.45%   -0.01%     
==========================================
  Files         197      198       +1     
  Lines        9183     9195      +12     
==========================================
+ Hits         8674     8685      +11     
- Misses        509      510       +1     
Impacted Files Coverage Δ
api/include/opentelemetry/common/kv_properties.h 97.50% <97.50%> (ø)
api/include/opentelemetry/trace/trace_state.h 97.39% <98.30%> (-0.17%) ⬇️
api/test/trace/trace_state_test.cc 97.39% <100.00%> (-0.39%) ⬇️

nostd::shared_ptr<Baggage> Set(const nostd::string_view &key, const nostd::string_view &value)
{
nostd::shared_ptr<Baggage> baggage(new Baggage(kv_properties_->Size() + 1));
baggage->kv_properties_->AddEntry(key, value);
Copy link
Contributor

@nikhil1511 nikhil1511 Mar 19, 2021

Choose a reason for hiding this comment

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

I have a question here about the key_value pairs in the baggage. With this implementation, we are adding duplicate keys in our baggage (eg. if key already existed in the baggage, we still prepend a new entry with the same key). Do we want to keep this behaviour or we should avoid adding old entry in our returned baggage.
Standard says to keep only one value for each baggage name ref: https:/open-telemetry/opentelemetry-specification/blob/main/specification/baggage/api.md#overview
We are always returning fresh value in getter so in a way it does not seem to be a big issue. If we keep this behaviour same, I think we need to change couple of things in Delete function as size could be less than kv_properties_->Size() - 1 (line 37)

Let me know what do you think about this. Please correct me if I am missing anything.

Copy link
Member Author

@lalitb lalitb Mar 19, 2021

Choose a reason for hiding this comment

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

Good point, we should first add the k-v pair from current baggage except for key passed as argument ( if it exist). And in the end, add the new k-v. It should be fine to store one less element than actual allocation as long as we track number of elements internally. What do you think ?

Copy link
Contributor

@nikhil1511 nikhil1511 Mar 19, 2021

Choose a reason for hiding this comment

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

Yes, I think that should be the way. One more thing, I am planning to use linked list (deep clone everytime) in kv_properties for optimum memory usage (as I don't see usecase of any random access). Currently, context also uses linked list but its not deep clone as context does not have delete key api. So, context code will be unchanged in this refactoring.
Do you agree with the approach ? Wanted to give you heads up before making change to make sure that I am not missing anything.

Copy link
Member Author

@lalitb lalitb Mar 19, 2021

Choose a reason for hiding this comment

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

What benefit do you see with linked list as compared to fixed size array ( which we use now )? Is it because of extra processing we may have to do initially to find exact allocation size in case of array?
The idea is to minimize multiple dynamic memory allocation in api. And iterating a linked list would be slow as compared to array due to non-contiguous memory allocation of it's nodes.

Copy link
Member Author

@lalitb lalitb Mar 19, 2021

Choose a reason for hiding this comment

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

@pyohannes - would like to hear your thoughts too.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nikhil- That's true. just wondering if you have any further questions on this design ?

Copy link
Contributor

@nikhil1511 nikhil1511 Mar 25, 2021

Choose a reason for hiding this comment

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

Should I keep the array structure only and not the linked list ? (and not worry about extra memory usage for some corner cases)

Copy link
Contributor

@nikhil1511 nikhil1511 Mar 25, 2021

Choose a reason for hiding this comment

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

I think, I can also clarify thing about baggage metadata. We have not incorporated baggage metadata here. In my solution, I had kept baggage metadata along with key, value. Its an opaque structure with no semantic meaning for now so languages like golang are not keeping it separately (keeping it as a part of value). How should we proceed ? I am kind of stuck there. We might want to make kv_properties more generic if we decide to keep it separately.

Copy link
Member Author

@lalitb lalitb Mar 25, 2021

Choose a reason for hiding this comment

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

Should I keep the array structure only and not the linked list ? (and not worry about extra memory usage for some corner cases)

Yes that's correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think, I can also clarify thing about baggage metadata. We have not incorporated baggage metadata here. In my solution, I had kept baggage metadata along with key, value. Its an opaque structure with no semantic meaning for now so languages like golang are not keeping it separately (keeping it as a part of value). How should we proceed ? I am kind of stuck there. We might want to make kv_properties more generic if we decide to keep it separately.

I won't be much worried about incorporating metadata as of now, and probably revisit once it's semantics/usage is properly defined by W3C. Don't see languages like python, dotnet supporting it too.

@@ -18,13 +18,16 @@
#include <cstring>
#include <string>

#include <iostream>
Copy link
Contributor

Choose a reason for hiding this comment

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

Think this is unused?

{

// Class to store fixed size array of key-value pairs of string type
class KeyValueProperties
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there reasoning to not use std library containers, e.g. unordered_map over KeyValueProperties?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is to guarantee ABI stability in api. We don't use std library

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, good to know! 🙂

@lalitb
Copy link
Member Author

lalitb commented Apr 19, 2021

closing for #676

@lalitb lalitb closed this Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Baggage ( prev: CorrelationContext )
3 participants