[metadata] Get logging to work safely (#32343)

This compiles for //:grpc, but not for tests yet.

It's the right approach though - @veblush hoping this is something you
can pick up and finish off.

<!--

If you know who should review your pull request, please assign it to
that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the
appropriate
lang label.

-->
pull/32500/head
Craig Tiller 2 years ago committed by GitHub
parent 2036ab972d
commit 292d0a55aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      src/core/lib/transport/metadata_batch.cc
  2. 56
      src/core/lib/transport/metadata_batch.h
  3. 6
      src/core/lib/transport/parsed_metadata.h
  4. 15
      test/core/transport/parsed_metadata_test.cc

@ -93,7 +93,7 @@ StaticSlice ContentTypeMetadata::Encode(ValueType x) {
return StaticSlice::FromStaticString("unrepresentable value"));
}
const char* ContentTypeMetadata::DisplayValue(MementoType content_type) {
const char* ContentTypeMetadata::DisplayValue(ValueType content_type) {
switch (content_type) {
case ValueType::kApplicationGrpc:
return "application/grpc";
@ -137,7 +137,7 @@ TeMetadata::MementoType TeMetadata::ParseMemento(
return out;
}
const char* TeMetadata::DisplayValue(MementoType te) {
const char* TeMetadata::DisplayValue(ValueType te) {
switch (te) {
case ValueType::kTrailers:
return "trailers";
@ -168,7 +168,7 @@ StaticSlice HttpSchemeMetadata::Encode(ValueType x) {
}
}
const char* HttpSchemeMetadata::DisplayValue(MementoType content_type) {
const char* HttpSchemeMetadata::DisplayValue(ValueType content_type) {
switch (content_type) {
case kHttp:
return "http";
@ -211,7 +211,7 @@ StaticSlice HttpMethodMetadata::Encode(ValueType x) {
}
}
const char* HttpMethodMetadata::DisplayValue(MementoType content_type) {
const char* HttpMethodMetadata::DisplayValue(ValueType content_type) {
switch (content_type) {
case kPost:
return "POST";
@ -253,7 +253,7 @@ Slice LbCostBinMetadata::Encode(const ValueType& x) {
return Slice(std::move(slice));
}
std::string LbCostBinMetadata::DisplayValue(MementoType x) {
std::string LbCostBinMetadata::DisplayValue(ValueType x) {
return absl::StrCat(x.name, ":", x.cost);
}

@ -64,7 +64,8 @@ struct GrpcTimeoutMetadata {
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error);
static ValueType MementoToValue(MementoType timeout);
static Slice Encode(ValueType x);
static std::string DisplayValue(MementoType x) { return x.ToString(); }
static std::string DisplayValue(ValueType x) { return x.ToString(); }
static std::string DisplayMemento(MementoType x) { return x.ToString(); }
};
// TE metadata trait.
@ -85,7 +86,8 @@ struct TeMetadata {
GPR_ASSERT(x == kTrailers);
return StaticSlice::FromStaticString("trailers");
}
static const char* DisplayValue(MementoType te);
static const char* DisplayValue(ValueType te);
static const char* DisplayMemento(MementoType te) { return DisplayValue(te); }
};
// content-type metadata trait.
@ -107,7 +109,10 @@ struct ContentTypeMetadata {
}
static StaticSlice Encode(ValueType x);
static const char* DisplayValue(MementoType content_type);
static const char* DisplayValue(ValueType content_type);
static const char* DisplayMemento(ValueType content_type) {
return DisplayValue(content_type);
}
};
// scheme metadata trait.
@ -129,7 +134,10 @@ struct HttpSchemeMetadata {
return content_type;
}
static StaticSlice Encode(ValueType x);
static const char* DisplayValue(MementoType content_type);
static const char* DisplayValue(ValueType content_type);
static const char* DisplayMemento(MementoType content_type) {
return DisplayValue(content_type);
}
};
// method metadata trait.
@ -148,7 +156,10 @@ struct HttpMethodMetadata {
return content_type;
}
static StaticSlice Encode(ValueType x);
static const char* DisplayValue(MementoType content_type);
static const char* DisplayValue(ValueType content_type);
static const char* DisplayMemento(MementoType content_type) {
return DisplayValue(content_type);
}
};
// Base type for metadata pertaining to a single compression algorithm
@ -162,13 +173,14 @@ struct CompressionAlgorithmBasedMetadata {
GPR_ASSERT(x != GRPC_COMPRESS_ALGORITHMS_COUNT);
return Slice::FromStaticString(CompressionAlgorithmAsString(x));
}
static const char* DisplayValue(MementoType x) {
static const char* DisplayValue(ValueType x) {
if (const char* p = CompressionAlgorithmAsString(x)) {
return p;
} else {
return "<discarded-invalid-value>";
}
}
static const char* DisplayMemento(MementoType x) { return DisplayValue(x); }
};
// grpc-encoding metadata trait.
@ -194,7 +206,10 @@ struct GrpcAcceptEncodingMetadata {
}
static ValueType MementoToValue(MementoType x) { return x; }
static Slice Encode(ValueType x) { return x.ToSlice(); }
static absl::string_view DisplayValue(MementoType x) { return x.ToString(); }
static absl::string_view DisplayValue(ValueType x) { return x.ToString(); }
static absl::string_view DisplayMemento(MementoType x) {
return DisplayValue(x);
}
};
struct SimpleSliceBasedMetadata {
@ -205,7 +220,10 @@ struct SimpleSliceBasedMetadata {
}
static ValueType MementoToValue(MementoType value) { return value; }
static Slice Encode(const ValueType& x) { return x.Ref(); }
static absl::string_view DisplayValue(const MementoType& value) {
static absl::string_view DisplayValue(const ValueType& value) {
return value.as_string_view();
}
static absl::string_view DisplayMemento(const MementoType& value) {
return value.as_string_view();
}
};
@ -273,7 +291,8 @@ struct SimpleIntBasedMetadataBase {
using MementoType = Int;
static ValueType MementoToValue(MementoType value) { return value; }
static Slice Encode(ValueType x) { return Slice::FromInt64(x); }
static Int DisplayValue(MementoType x) { return x; }
static Int DisplayValue(ValueType x) { return x; }
static Int DisplayMemento(MementoType x) { return x; }
};
template <typename Int, Int kInvalidValue>
@ -312,6 +331,7 @@ struct GrpcRetryPushbackMsMetadata {
static ValueType MementoToValue(MementoType x) { return x; }
static Slice Encode(Duration x) { return Slice::FromInt64(x.millis()); }
static int64_t DisplayValue(Duration x) { return x.millis(); }
static int64_t DisplayMemento(Duration x) { return DisplayValue(x); }
static Duration ParseMemento(Slice value, MetadataParseErrorFn on_error);
};
@ -333,7 +353,10 @@ struct GrpcLbClientStatsMetadata {
using MementoType = ValueType;
static ValueType MementoToValue(MementoType value) { return value; }
static Slice Encode(ValueType) { abort(); }
static const char* DisplayValue(MementoType) { return "<internal-lb-stats>"; }
static const char* DisplayValue(ValueType) { return "<internal-lb-stats>"; }
static const char* DisplayMemento(MementoType) {
return "<internal-lb-stats>";
}
static MementoType ParseMemento(Slice, MetadataParseErrorFn) {
return nullptr;
}
@ -356,7 +379,8 @@ struct LbCostBinMetadata {
using MementoType = ValueType;
static ValueType MementoToValue(MementoType value) { return value; }
static Slice Encode(const ValueType& x);
static std::string DisplayValue(MementoType x);
static std::string DisplayValue(ValueType x);
static std::string DisplayMemento(MementoType x) { return DisplayValue(x); }
static MementoType ParseMemento(Slice value, MetadataParseErrorFn on_error);
};
@ -677,6 +701,11 @@ struct AdaptDisplayValueToLog<Slice> {
}
};
template <>
struct AdaptDisplayValueToLog<const char*> {
static std::string ToString(const char* value) { return std::string(value); }
};
template <>
struct AdaptDisplayValueToLog<StaticSlice> {
static absl::string_view ToString(StaticSlice value) {
@ -718,7 +747,7 @@ struct Value<Which, absl::enable_if_t<Which::kRepeatable == false &&
return EncodeTo(encoder);
}
void LogTo(LogFn log_fn) const {
LogKeyValueTo(Which::key(), value, Which::Encode, log_fn);
LogKeyValueTo(Which::key(), value, Which::DisplayValue, log_fn);
}
using StorageType = typename Which::ValueType;
GPR_NO_UNIQUE_ADDRESS StorageType value;
@ -1004,7 +1033,8 @@ MetadataValueAsSlice(typename Which::ValueType value) {
// // Convert a value to something that can be passed to StrCat and
// displayed
// // for debugging
// static SomeStrCatableType DisplayValue(MementoType value) { ... }
// static SomeStrCatableType DisplayValue(ValueType value) { ... }
// static SomeStrCatableType DisplayMemento(MementoType value) { ... }
// };
//
// Non-encodable traits are determined by missing the key() method, and have

@ -300,7 +300,7 @@ ParsedMetadata<MetadataContainer>::TrivialTraitVTable() {
return metadata_detail::MakeDebugStringPipeline(
Which::key(), value,
metadata_detail::FieldFromTrivial<typename Which::MementoType>,
Which::DisplayValue);
Which::DisplayMemento);
},
// key
Which::key(),
@ -334,7 +334,7 @@ ParsedMetadata<MetadataContainer>::NonTrivialTraitVTable() {
return metadata_detail::MakeDebugStringPipeline(
Which::key(), value,
metadata_detail::FieldFromPointer<typename Which::MementoType>,
Which::DisplayValue);
Which::DisplayMemento);
},
// key
Which::key(),
@ -362,7 +362,7 @@ ParsedMetadata<MetadataContainer>::SliceTraitVTable() {
[](const Buffer& value) {
return metadata_detail::MakeDebugStringPipeline(
Which::key(), value, metadata_detail::SliceFromBuffer,
Which::DisplayValue);
Which::DisplayMemento);
},
// key
Which::key(),

@ -41,6 +41,9 @@ struct CharTrait {
return slice[0];
}
static std::string DisplayValue(char value) { return std::string(1, value); }
static std::string DisplayMemento(MementoType memento) {
return DisplayValue(memento);
}
};
struct Int32Trait {
@ -58,6 +61,9 @@ struct Int32Trait {
static std::string DisplayValue(int32_t value) {
return std::to_string(value);
}
static std::string DisplayMemento(MementoType memento) {
return DisplayValue(memento);
}
};
struct Int64Trait {
@ -75,6 +81,9 @@ struct Int64Trait {
static std::string DisplayValue(int64_t value) {
return std::to_string(value);
}
static std::string DisplayMemento(MementoType memento) {
return DisplayValue(memento);
}
};
struct IntptrTrait {
@ -92,6 +101,9 @@ struct IntptrTrait {
static std::string DisplayValue(intptr_t value) {
return std::to_string(value);
}
static std::string DisplayMemento(MementoType memento) {
return DisplayValue(memento);
}
};
struct StringTrait {
@ -108,6 +120,9 @@ struct StringTrait {
return std::string(view.begin(), view.end());
}
static std::string DisplayValue(const std::string& value) { return value; }
static std::string DisplayMemento(MementoType memento) {
return DisplayValue(memento);
}
};
class FakeContainer {

Loading…
Cancel
Save