@@ -6,7 +6,7 @@ namespace tracing {
66namespace {
77
88Expected<std::unordered_map<std::string, std::string>, Baggage::Error>
9- parse_baggage (StringView input, size_t max_capacity ) {
9+ parse_baggage (StringView input) {
1010 std::unordered_map<std::string, std::string> result;
1111 if (input.empty ()) return result;
1212
@@ -29,9 +29,6 @@ parse_baggage(StringView input, size_t max_capacity) {
2929 switch (internal_state) {
3030 case state::leading_spaces_key: {
3131 if (input[i] != ' ' ) {
32- if (result.size () == max_capacity)
33- return Baggage::Error::MAXIMUM_CAPACITY_REACHED;
34-
3532 beg = i;
3633 tmp_end = i;
3734 internal_state = state::key;
@@ -40,7 +37,7 @@ parse_baggage(StringView input, size_t max_capacity) {
4037
4138 case state::key: {
4239 if (input[i] == ' ,' ) {
43- return Baggage::Error:: MALFORMED_BAGGAGE_HEADER;
40+ return Baggage::Error{Baggage::Error:: MALFORMED_BAGGAGE_HEADER, i} ;
4441 } else if (input[i] == ' =' ) {
4542 key = StringView{input.data () + beg, tmp_end - beg + 1 };
4643 internal_state = state::leading_spaces_value;
@@ -72,7 +69,7 @@ parse_baggage(StringView input, size_t max_capacity) {
7269 }
7370
7471 if (internal_state != state::value) {
75- return Baggage::Error::MALFORMED_BAGGAGE_HEADER;
72+ return { Baggage::Error::MALFORMED_BAGGAGE_HEADER} ;
7673 }
7774
7875 value = StringView{input.data () + beg, tmp_end - beg + 1 };
@@ -85,11 +82,6 @@ parse_baggage(StringView input, size_t max_capacity) {
8582
8683Baggage::Baggage (size_t max_capacity) : max_capacity_(max_capacity) {}
8784
88- Baggage::Baggage (
89- std::initializer_list<std::pair<const std::string, std::string>> baggage,
90- size_t max_capacity)
91- : max_capacity_(max_capacity), baggage_(baggage) {}
92-
9385Baggage::Baggage (std::unordered_map<std::string, std::string> baggage,
9486 size_t max_capacity)
9587 : max_capacity_(max_capacity), baggage_(std::move(baggage)) {}
@@ -127,12 +119,15 @@ void Baggage::visit(std::function<void(StringView, StringView)>&& visitor) {
127119 }
128120}
129121
130- Expected<void > Baggage::inject (DictWriter& writer, size_t max_bytes ) const {
122+ Expected<void > Baggage::inject (DictWriter& writer, const Options& opts ) const {
131123 if (baggage_.empty ()) return {};
124+ if (baggage_.size () > opts.max_items )
125+ return datadog::tracing::Error{
126+ datadog::tracing::Error::Code::BAGGAGE_MAXIMUM_BYTES_REACHED, " " };
132127
133128 // TODO(@dmehala): Memory alloc optimization, (re)use fixed size buffer.
134129 std::string res;
135- res.reserve (max_bytes);
130+ res.reserve (opts. max_bytes );
136131
137132 auto it = baggage_.cbegin ();
138133 res += it->first ;
@@ -146,7 +141,7 @@ Expected<void> Baggage::inject(DictWriter& writer, size_t max_bytes) const {
146141 res += it->second ;
147142 }
148143
149- if (res.size () >= max_bytes)
144+ if (res.size () >= opts. max_bytes )
150145 return datadog::tracing::Error{
151146 datadog::tracing::Error::Code::BAGGAGE_MAXIMUM_BYTES_REACHED, " " };
152147
@@ -156,20 +151,19 @@ Expected<void> Baggage::inject(DictWriter& writer, size_t max_bytes) const {
156151 return {};
157152}
158153
159- Expected<Baggage, Baggage::Error> Baggage::extract (const DictReader& headers,
160- size_t max_capacity) {
154+ Expected<Baggage, Baggage::Error> Baggage::extract (const DictReader& headers) {
161155 auto found = headers.lookup (" baggage" );
162156 if (!found) {
163- return Error::MISSING_HEADER;
157+ return Baggage:: Error{Error ::MISSING_HEADER} ;
164158 }
165159
166160 // TODO(@dmehala): Avoid allocation
167- auto bv = parse_baggage (*found, max_capacity );
161+ auto bv = parse_baggage (*found);
168162 if (auto error = bv.if_error ()) {
169163 return *error;
170164 }
171165
172- Baggage result (*bv, max_capacity );
166+ Baggage result (*bv);
173167 return result;
174168}
175169
0 commit comments