xref: /aosp_15_r20/external/grpc-grpc/src/core/lib/transport/metadata_info.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2023 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <grpc/support/port_platform.h>
16 
17 #include "src/core/lib/transport/metadata_info.h"
18 
19 #include "src/core/lib/slice/slice.h"
20 
21 namespace grpc_core {
22 
23 class MetadataSizesAnnotation::MetadataSizeEncoder {
24  public:
MetadataSizeEncoder(std::string & summary)25   explicit MetadataSizeEncoder(std::string& summary) : summary_(summary) {}
26 
Encode(const Slice & key,const Slice & value)27   void Encode(const Slice& key, const Slice& value) {
28     AddToSummary(key.as_string_view(), value.size());
29   }
30 
31   template <typename Key, typename Value>
Encode(Key,const Value & value)32   void Encode(Key, const Value& value) {
33     AddToSummary(Key::key(), EncodedSizeOfKey(Key(), value));
34   }
35 
36  private:
AddToSummary(absl::string_view key,size_t value_length)37   void AddToSummary(absl::string_view key,
38                     size_t value_length) GPR_ATTRIBUTE_NOINLINE {
39     absl::StrAppend(&summary_, key, ":",
40                     hpack_constants::SizeForEntry(key.size(), value_length),
41                     ",");
42   }
43   std::string& summary_;
44 };
45 
ToString() const46 std::string MetadataSizesAnnotation::ToString() const {
47   std::string metadata_annotation =
48       absl::StrCat("gRPC metadata soft_limit:", soft_limit_,
49                    ",hard_limit:", hard_limit_, ",");
50   MetadataSizeEncoder encoder(metadata_annotation);
51   metadata_buffer_->Encode(&encoder);
52   return metadata_annotation;
53 }
54 
55 }  // namespace grpc_core