Add severity

reviewable/pr13883/r13
ncteisen 7 years ago
parent bec1a61c26
commit 09703ea300
  1. 38
      src/core/lib/channel/channel_trace.cc
  2. 22
      src/core/lib/channel/channel_trace.h
  3. 1
      src/core/lib/surface/channel.cc
  4. 2
      src/proto/grpc/channelz/channelz.proto
  5. 16
      test/core/channel/channel_trace_test.cc
  6. 5
      test/cpp/util/channel_trace_proto_helper.cc

@ -41,17 +41,19 @@
namespace grpc_core { namespace grpc_core {
ChannelTrace::TraceEvent::TraceEvent( ChannelTrace::TraceEvent::TraceEvent(
grpc_slice data, RefCountedPtr<ChannelTrace> referenced_tracer, Severity severity, grpc_slice data,
ReferencedType type) RefCountedPtr<ChannelTrace> referenced_tracer, ReferencedType type)
: data_(data), : severity_(severity),
data_(data),
timestamp_(grpc_millis_to_timespec(grpc_core::ExecCtx::Get()->Now(), timestamp_(grpc_millis_to_timespec(grpc_core::ExecCtx::Get()->Now(),
GPR_CLOCK_REALTIME)), GPR_CLOCK_REALTIME)),
next_(nullptr), next_(nullptr),
referenced_tracer_(std::move(referenced_tracer)), referenced_tracer_(std::move(referenced_tracer)),
referenced_type_(type) {} referenced_type_(type) {}
ChannelTrace::TraceEvent::TraceEvent(grpc_slice data) ChannelTrace::TraceEvent::TraceEvent(Severity severity, grpc_slice data)
: data_(data), : severity_(severity),
data_(data),
timestamp_(grpc_millis_to_timespec(grpc_core::ExecCtx::Get()->Now(), timestamp_(grpc_millis_to_timespec(grpc_core::ExecCtx::Get()->Now(),
GPR_CLOCK_REALTIME)), GPR_CLOCK_REALTIME)),
next_(nullptr) {} next_(nullptr) {}
@ -107,25 +109,27 @@ void ChannelTrace::AddTraceEventHelper(TraceEvent* new_trace_event) {
} }
} }
void ChannelTrace::AddTraceEventReferencingChannel( void ChannelTrace::AddTraceEvent(Severity severity, grpc_slice data) {
grpc_slice data, RefCountedPtr<ChannelTrace> referenced_tracer) {
if (max_list_size_ == 0) return; // tracing is disabled if max_events == 0 if (max_list_size_ == 0) return; // tracing is disabled if max_events == 0
// create and fill up the new event AddTraceEventHelper(New<TraceEvent>(severity, data));
AddTraceEventHelper(
New<TraceEvent>(data, std::move(referenced_tracer), Channel));
} }
void ChannelTrace::AddTraceEventReferencingSubchannel( void ChannelTrace::AddTraceEventReferencingChannel(
grpc_slice data, RefCountedPtr<ChannelTrace> referenced_tracer) { Severity severity, grpc_slice data,
RefCountedPtr<ChannelTrace> referenced_tracer) {
if (max_list_size_ == 0) return; // tracing is disabled if max_events == 0 if (max_list_size_ == 0) return; // tracing is disabled if max_events == 0
// create and fill up the new event // create and fill up the new event
AddTraceEventHelper( AddTraceEventHelper(
New<TraceEvent>(data, std::move(referenced_tracer), Subchannel)); New<TraceEvent>(severity, data, std::move(referenced_tracer), Channel));
} }
void ChannelTrace::AddTraceEvent(grpc_slice data) { void ChannelTrace::AddTraceEventReferencingSubchannel(
Severity severity, grpc_slice data,
RefCountedPtr<ChannelTrace> referenced_tracer) {
if (max_list_size_ == 0) return; // tracing is disabled if max_events == 0 if (max_list_size_ == 0) return; // tracing is disabled if max_events == 0
AddTraceEventHelper(New<TraceEvent>(data)); // create and fill up the new event
AddTraceEventHelper(New<TraceEvent>(
severity, data, std::move(referenced_tracer), Subchannel));
} }
namespace { namespace {
@ -147,6 +151,10 @@ void ChannelTrace::TraceEvent::RenderTraceEvent(grpc_json* json) const {
json_iterator = grpc_json_create_child(json_iterator, json, "description", json_iterator = grpc_json_create_child(json_iterator, json, "description",
grpc_slice_to_c_string(data_), grpc_slice_to_c_string(data_),
GRPC_JSON_STRING, true); GRPC_JSON_STRING, true);
char* trace_level_str;
gpr_asprintf(&trace_level_str, "%d", static_cast<int32_t>(severity_));
json_iterator = grpc_json_create_child(
json_iterator, json, "severity", trace_level_str, GRPC_JSON_NUMBER, true);
json_iterator = json_iterator =
grpc_json_create_child(json_iterator, json, "timestamp", grpc_json_create_child(json_iterator, json, "timestamp",
fmt_time(timestamp_), GRPC_JSON_STRING, true); fmt_time(timestamp_), GRPC_JSON_STRING, true);

@ -40,8 +40,15 @@ class ChannelTrace : public RefCounted<ChannelTrace> {
// returns the tracer's uuid // returns the tracer's uuid
intptr_t GetUuid() const; intptr_t GetUuid() const;
enum Severity {
Unset = 0, // never to be used
Info, // we start at 1 to avoid using proto default values
Warning,
Error
};
// Adds a new trace event to the tracing object // Adds a new trace event to the tracing object
void AddTraceEvent(grpc_slice data); void AddTraceEvent(Severity severity, grpc_slice data);
// Adds a new trace event to the tracing object. This trace event refers to a // Adds a new trace event to the tracing object. This trace event refers to a
// an event on a child of the channel. For example, if this channel has // an event on a child of the channel. For example, if this channel has
@ -51,15 +58,18 @@ class ChannelTrace : public RefCounted<ChannelTrace> {
// TODO(ncteisen): Once channelz is implemented, the events should reference // TODO(ncteisen): Once channelz is implemented, the events should reference
// the overall channelz object, not just the ChannelTrace object. // the overall channelz object, not just the ChannelTrace object.
void AddTraceEventReferencingChannel( void AddTraceEventReferencingChannel(
grpc_slice data, RefCountedPtr<ChannelTrace> referenced_tracer); Severity severity, grpc_slice data,
RefCountedPtr<ChannelTrace> referenced_tracer);
void AddTraceEventReferencingSubchannel( void AddTraceEventReferencingSubchannel(
grpc_slice data, RefCountedPtr<ChannelTrace> referenced_tracer); Severity severity, grpc_slice data,
RefCountedPtr<ChannelTrace> referenced_tracer);
// Returns the tracing data rendered as a grpc json string. // Returns the tracing data rendered as a grpc json string.
// The string is owned by the caller and must be freed. // The string is owned by the caller and must be freed.
char* RenderTrace() const; char* RenderTrace() const;
private: private:
// Types of objects that can be references by trace events.
enum ReferencedType { Channel, Subchannel }; enum ReferencedType { Channel, Subchannel };
// Private class to encapsulate all the data and bookkeeping needed for a // Private class to encapsulate all the data and bookkeeping needed for a
// a trace event. // a trace event.
@ -68,12 +78,13 @@ class ChannelTrace : public RefCounted<ChannelTrace> {
// Constructor for a TraceEvent that references a different channel. // Constructor for a TraceEvent that references a different channel.
// TODO(ncteisen): once channelz is implemented, this should reference the // TODO(ncteisen): once channelz is implemented, this should reference the
// overall channelz object, not just the ChannelTrace object // overall channelz object, not just the ChannelTrace object
TraceEvent(grpc_slice data, RefCountedPtr<ChannelTrace> referenced_tracer, TraceEvent(Severity severity, grpc_slice data,
RefCountedPtr<ChannelTrace> referenced_tracer,
ReferencedType type); ReferencedType type);
// Constructor for a TraceEvent that does not reverence a different // Constructor for a TraceEvent that does not reverence a different
// channel. // channel.
TraceEvent(grpc_slice data); TraceEvent(Severity severity, grpc_slice data);
~TraceEvent(); ~TraceEvent();
@ -86,6 +97,7 @@ class ChannelTrace : public RefCounted<ChannelTrace> {
void set_next(TraceEvent* next) { next_ = next; } void set_next(TraceEvent* next) { next_ = next; }
private: private:
Severity severity_;
grpc_slice data_; grpc_slice data_;
gpr_timespec timestamp_; gpr_timespec timestamp_;
TraceEvent* next_; TraceEvent* next_;

@ -184,6 +184,7 @@ grpc_channel* grpc_channel_create_with_builder(
channel->tracer = grpc_core::MakeRefCounted<grpc_core::ChannelTrace>( channel->tracer = grpc_core::MakeRefCounted<grpc_core::ChannelTrace>(
channel_tracer_max_nodes); channel_tracer_max_nodes);
channel->tracer->AddTraceEvent( channel->tracer->AddTraceEvent(
grpc_core::ChannelTrace::Severity::Info,
grpc_slice_from_static_string("Channel created")); grpc_slice_from_static_string("Channel created"));
return channel; return channel;
} }

@ -98,7 +98,7 @@ message ChannelTraceEvent {
// the severity of the trace event. Options are INFO, WARNING, and ERROR, // the severity of the trace event. Options are INFO, WARNING, and ERROR,
// which are represented by the values 1, 2, and 3, respectively. Any other // which are represented by the values 1, 2, and 3, respectively. Any other
// value will be treated as ERROR. // value will be treated as ERROR.
int32 trace_level = 2; int32 severity = 2;
// When this event occurred. // When this event occurred.
google.protobuf.Timestamp timestamp = 3; google.protobuf.Timestamp timestamp = 3;
// ref of referenced channel or subchannel. // ref of referenced channel or subchannel.

@ -73,7 +73,8 @@ void ValidateChannelTraceData(grpc_json* json,
} }
void AddSimpleTrace(RefCountedPtr<ChannelTrace> tracer) { void AddSimpleTrace(RefCountedPtr<ChannelTrace> tracer) {
tracer->AddTraceEvent(grpc_slice_from_static_string("simple trace")); tracer->AddTraceEvent(ChannelTrace::Severity::Info,
grpc_slice_from_static_string("simple trace"));
} }
// checks for the existence of all the required members of the tracer. // checks for the existence of all the required members of the tracer.
@ -113,8 +114,10 @@ TEST_P(ChannelTracerTest, BasicTest) {
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
ValidateTraceDataMatchedUuidLookup(tracer); ValidateTraceDataMatchedUuidLookup(tracer);
tracer->AddTraceEvent(grpc_slice_from_static_string("trace three")); tracer->AddTraceEvent(ChannelTrace::Severity::Info,
tracer->AddTraceEvent(grpc_slice_from_static_string("trace four")); grpc_slice_from_static_string("trace three"));
tracer->AddTraceEvent(ChannelTrace::Severity::Error,
grpc_slice_from_static_string("trace four error"));
ValidateChannelTrace(tracer, 4, GetParam()); ValidateChannelTrace(tracer, 4, GetParam());
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
@ -138,6 +141,7 @@ TEST_P(ChannelTracerTest, ComplexTest) {
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
RefCountedPtr<ChannelTrace> sc1 = MakeRefCounted<ChannelTrace>(GetParam()); RefCountedPtr<ChannelTrace> sc1 = MakeRefCounted<ChannelTrace>(GetParam());
tracer->AddTraceEventReferencingSubchannel( tracer->AddTraceEventReferencingSubchannel(
ChannelTrace::Severity::Info,
grpc_slice_from_static_string("subchannel one created"), sc1); grpc_slice_from_static_string("subchannel one created"), sc1);
ValidateChannelTrace(tracer, 3, GetParam()); ValidateChannelTrace(tracer, 3, GetParam());
AddSimpleTrace(sc1); AddSimpleTrace(sc1);
@ -154,8 +158,10 @@ TEST_P(ChannelTracerTest, ComplexTest) {
ValidateTraceDataMatchedUuidLookup(tracer); ValidateTraceDataMatchedUuidLookup(tracer);
RefCountedPtr<ChannelTrace> sc2 = MakeRefCounted<ChannelTrace>(GetParam()); RefCountedPtr<ChannelTrace> sc2 = MakeRefCounted<ChannelTrace>(GetParam());
tracer->AddTraceEventReferencingChannel( tracer->AddTraceEventReferencingChannel(
ChannelTrace::Severity::Info,
grpc_slice_from_static_string("LB channel two created"), sc2); grpc_slice_from_static_string("LB channel two created"), sc2);
tracer->AddTraceEventReferencingSubchannel( tracer->AddTraceEventReferencingSubchannel(
ChannelTrace::Severity::Warning,
grpc_slice_from_static_string("subchannel one inactive"), sc1); grpc_slice_from_static_string("subchannel one inactive"), sc1);
ValidateChannelTrace(tracer, 7, GetParam()); ValidateChannelTrace(tracer, 7, GetParam());
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
@ -180,21 +186,25 @@ TEST_P(ChannelTracerTest, TestNesting) {
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
RefCountedPtr<ChannelTrace> sc1 = MakeRefCounted<ChannelTrace>(GetParam()); RefCountedPtr<ChannelTrace> sc1 = MakeRefCounted<ChannelTrace>(GetParam());
tracer->AddTraceEventReferencingChannel( tracer->AddTraceEventReferencingChannel(
ChannelTrace::Severity::Info,
grpc_slice_from_static_string("subchannel one created"), sc1); grpc_slice_from_static_string("subchannel one created"), sc1);
AddSimpleTrace(sc1); AddSimpleTrace(sc1);
RefCountedPtr<ChannelTrace> conn1 = MakeRefCounted<ChannelTrace>(GetParam()); RefCountedPtr<ChannelTrace> conn1 = MakeRefCounted<ChannelTrace>(GetParam());
// nesting one level deeper. // nesting one level deeper.
sc1->AddTraceEventReferencingSubchannel( sc1->AddTraceEventReferencingSubchannel(
ChannelTrace::Severity::Info,
grpc_slice_from_static_string("connection one created"), conn1); grpc_slice_from_static_string("connection one created"), conn1);
AddSimpleTrace(conn1); AddSimpleTrace(conn1);
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
RefCountedPtr<ChannelTrace> sc2 = MakeRefCounted<ChannelTrace>(GetParam()); RefCountedPtr<ChannelTrace> sc2 = MakeRefCounted<ChannelTrace>(GetParam());
tracer->AddTraceEventReferencingSubchannel( tracer->AddTraceEventReferencingSubchannel(
ChannelTrace::Severity::Info,
grpc_slice_from_static_string("subchannel two created"), sc2); grpc_slice_from_static_string("subchannel two created"), sc2);
// this trace should not get added to the parents children since it is already // this trace should not get added to the parents children since it is already
// present in the tracer. // present in the tracer.
tracer->AddTraceEventReferencingChannel( tracer->AddTraceEventReferencingChannel(
ChannelTrace::Severity::Warning,
grpc_slice_from_static_string("subchannel one inactive"), sc1); grpc_slice_from_static_string("subchannel one inactive"), sc1);
AddSimpleTrace(tracer); AddSimpleTrace(tracer);
tracer.reset(nullptr); tracer.reset(nullptr);

@ -22,6 +22,7 @@
#include <google/protobuf/util/json_util.h> #include <google/protobuf/util/json_util.h>
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include <grpc/support/log.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "src/proto/grpc/channelz/channelz.pb.h" #include "src/proto/grpc/channelz/channelz.pb.h"
@ -32,14 +33,14 @@ namespace testing {
void ValidateChannelTraceProtoJsonTranslation(char* tracer_json_c_str) { void ValidateChannelTraceProtoJsonTranslation(char* tracer_json_c_str) {
std::string tracer_json_str(tracer_json_c_str); std::string tracer_json_str(tracer_json_c_str);
grpc::channelz::ChannelTrace channel_trace; grpc::channelz::ChannelTrace channel_trace;
google::protobuf::util::JsonParseOptions options; google::protobuf::util::JsonParseOptions parse_options;
// If the following line is failing, then uncomment the last line of the // If the following line is failing, then uncomment the last line of the
// comment, and uncomment the lines that print the two strings. You can // comment, and uncomment the lines that print the two strings. You can
// then compare the output, and determine what fields are missing. // then compare the output, and determine what fields are missing.
// //
// options.ignore_unknown_fields = true; // options.ignore_unknown_fields = true;
ASSERT_EQ(google::protobuf::util::JsonStringToMessage( ASSERT_EQ(google::protobuf::util::JsonStringToMessage(
tracer_json_str, &channel_trace, options), tracer_json_str, &channel_trace, parse_options),
google::protobuf::util::Status::OK); google::protobuf::util::Status::OK);
std::string proto_json_str; std::string proto_json_str;
ASSERT_EQ(google::protobuf::util::MessageToJsonString(channel_trace, ASSERT_EQ(google::protobuf::util::MessageToJsonString(channel_trace,

Loading…
Cancel
Save