Add internal API to help trace retries and collect metrics (#26673)

* Add internal API to help trace retries and collect metrics

* Add headers

* Reviewer comments

* Revert changes for grpc_metadata_batch

* Regenerate projects

* Reviewer comments

* Reviewer comments

* Reviewer comments

* reviewer comments

* Fix sanity
pull/26736/head
Yash Tibrewal 4 years ago committed by GitHub
parent ab00c88a41
commit dc250d1c68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      BUILD
  2. 1
      BUILD.gn
  3. 2
      build_autogenerated.yaml
  4. 2
      gRPC-C++.podspec
  5. 2
      gRPC-Core.podspec
  6. 1
      grpc.gemspec
  7. 1
      package.xml
  8. 84
      src/core/lib/channel/call_tracer.h
  9. 15
      src/cpp/ext/filters/census/client_filter.cc
  10. 4
      src/cpp/ext/filters/census/client_filter.h
  11. 70
      src/cpp/ext/filters/census/open_census_call_tracer.h
  12. 1
      tools/doxygen/Doxyfile.c++.internal
  13. 1
      tools/doxygen/Doxyfile.core.internal

@ -1009,6 +1009,7 @@ grpc_cc_library(
"src/core/lib/address_utils/sockaddr_utils.h",
"src/core/lib/avl/avl.h",
"src/core/lib/backoff/backoff.h",
"src/core/lib/channel/call_tracer.h",
"src/core/lib/channel/channel_args.h",
"src/core/lib/channel/channel_stack.h",
"src/core/lib/channel/channel_stack_builder.h",
@ -3144,6 +3145,7 @@ grpc_cc_library(
"src/cpp/ext/filters/census/context.h",
"src/cpp/ext/filters/census/grpc_plugin.h",
"src/cpp/ext/filters/census/measures.h",
"src/cpp/ext/filters/census/open_census_call_tracer.h",
"src/cpp/ext/filters/census/rpc_encoding.h",
"src/cpp/ext/filters/census/server_filter.h",
],

@ -822,6 +822,7 @@ config("grpc_config") {
"src/core/lib/avl/avl.h",
"src/core/lib/backoff/backoff.cc",
"src/core/lib/backoff/backoff.h",
"src/core/lib/channel/call_tracer.h",
"src/core/lib/channel/channel_args.cc",
"src/core/lib/channel/channel_args.h",
"src/core/lib/channel/channel_stack.cc",

@ -699,6 +699,7 @@ libs:
- src/core/lib/address_utils/sockaddr_utils.h
- src/core/lib/avl/avl.h
- src/core/lib/backoff/backoff.h
- src/core/lib/channel/call_tracer.h
- src/core/lib/channel/channel_args.h
- src/core/lib/channel/channel_stack.h
- src/core/lib/channel/channel_stack_builder.h
@ -1724,6 +1725,7 @@ libs:
- src/core/lib/address_utils/sockaddr_utils.h
- src/core/lib/avl/avl.h
- src/core/lib/backoff/backoff.h
- src/core/lib/channel/call_tracer.h
- src/core/lib/channel/channel_args.h
- src/core/lib/channel/channel_stack.h
- src/core/lib/channel/channel_stack_builder.h

@ -494,6 +494,7 @@ Pod::Spec.new do |s|
'src/core/lib/address_utils/sockaddr_utils.h',
'src/core/lib/avl/avl.h',
'src/core/lib/backoff/backoff.h',
'src/core/lib/channel/call_tracer.h',
'src/core/lib/channel/channel_args.h',
'src/core/lib/channel/channel_stack.h',
'src/core/lib/channel/channel_stack_builder.h',
@ -1146,6 +1147,7 @@ Pod::Spec.new do |s|
'src/core/lib/address_utils/sockaddr_utils.h',
'src/core/lib/avl/avl.h',
'src/core/lib/backoff/backoff.h',
'src/core/lib/channel/call_tracer.h',
'src/core/lib/channel/channel_args.h',
'src/core/lib/channel/channel_stack.h',
'src/core/lib/channel/channel_stack_builder.h',

@ -801,6 +801,7 @@ Pod::Spec.new do |s|
'src/core/lib/avl/avl.h',
'src/core/lib/backoff/backoff.cc',
'src/core/lib/backoff/backoff.h',
'src/core/lib/channel/call_tracer.h',
'src/core/lib/channel/channel_args.cc',
'src/core/lib/channel/channel_args.h',
'src/core/lib/channel/channel_stack.cc',
@ -1726,6 +1727,7 @@ Pod::Spec.new do |s|
'src/core/lib/address_utils/sockaddr_utils.h',
'src/core/lib/avl/avl.h',
'src/core/lib/backoff/backoff.h',
'src/core/lib/channel/call_tracer.h',
'src/core/lib/channel/channel_args.h',
'src/core/lib/channel/channel_stack.h',
'src/core/lib/channel/channel_stack_builder.h',

@ -716,6 +716,7 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/avl/avl.h )
s.files += %w( src/core/lib/backoff/backoff.cc )
s.files += %w( src/core/lib/backoff/backoff.h )
s.files += %w( src/core/lib/channel/call_tracer.h )
s.files += %w( src/core/lib/channel/channel_args.cc )
s.files += %w( src/core/lib/channel/channel_args.h )
s.files += %w( src/core/lib/channel/channel_stack.cc )

@ -696,6 +696,7 @@
<file baseinstalldir="/" name="src/core/lib/avl/avl.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/backoff/backoff.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/backoff/backoff.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/call_tracer.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_args.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_args.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/channel/channel_stack.cc" role="src" />

@ -0,0 +1,84 @@
//
//
// Copyright 2021 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#ifndef GRPC_CORE_LIB_CHANNEL_CALL_TRACER_H
#define GRPC_CORE_LIB_CHANNEL_CALL_TRACER_H
#include <grpc/support/port_platform.h>
#include "absl/strings/string_view.h"
#include "src/core/lib/channel/channel_stack.h"
#include "src/core/lib/transport/byte_stream.h"
#include "src/core/lib/transport/metadata_batch.h"
namespace grpc_core {
// Interface for a tracer that records activities on a call. Actual attempts for
// this call are traced with CallAttemptTracer after invoking RecordNewAttempt()
// on the CallTracer object.
class CallTracer {
public:
// Interface for a tracer that records activities on a particular call
// attempt.
// (A single RPC can have multiple attempts due to retry/hedging policies or
// as transparent retry attempts.)
class CallAttemptTracer {
public:
virtual ~CallAttemptTracer() {}
// Please refer to `grpc_transport_stream_op_batch_payload` for details on
// arguments.
virtual void RecordSendInitialMetadata(
grpc_metadata_batch* send_initial_metadata, uint32_t flags) = 0;
virtual void RecordOnDoneSendInitialMetadata() = 0;
virtual void RecordSendTrailingMetadata(
grpc_metadata_batch* send_trailing_metadata) = 0;
virtual void RecordSendMessage(const ByteStream& send_message) = 0;
// The `RecordReceivedxx()` methods should only be invoked when the
// metadata/message was successfully received, i.e., without any error.
// TODO(yashkt): We are using gpr_atm here instead of absl::string_view
// since that's what the transport API uses, and performing an atomic load
// is unnecessary if the census tracer does not need it at present. Fix this
// when the transport API changes.
virtual void RecordReceivedInitialMetadata(
grpc_metadata_batch* recv_initial_metadata, uint32_t flags,
gpr_atm* peer_string) = 0;
virtual void RecordReceivedMessage(const ByteStream& recv_message) = 0;
virtual void RecordReceivedTrailingMetadata(
grpc_metadata_batch* recv_trailing_metadata) = 0;
virtual void RecordCancel(grpc_error_handle cancel_error) = 0;
// Should be the last API call to the object. Once invoked, the tracer
// library is free to destroy the object.
virtual void RecordEnd(const grpc_call_final_info& final_info) = 0;
};
virtual ~CallTracer() {}
// Records a new attempt for the associated call. \a transparent denotes
// whether the attempt is being made as a transparent retry or as a
// non-transparent retry/heding attempt. (There will be at least one attempt
// even if the call is not being retried.) The `CallTracer` object retains
// ownership to the newly created `CallAttemptTracer` object. RecordEnd()
// serves as an indication that the call stack is done with all API calls, and
// the tracer library is free to destroy it after that.
virtual CallAttemptTracer* StartNewAttempt(bool is_transparent_retry) = 0;
};
} // namespace grpc_core
#endif // GRPC_CORE_LIB_CHANNEL_CALL_TRACER_H

@ -91,10 +91,10 @@ void CensusClientCallData::StartTransportStreamOpBatch(
if (op->send_initial_metadata() != nullptr) {
census_context* ctxt = op->get_census_context();
GenerateClientContext(
qualified_method_, &context_,
qualified_method_, tracer_.context(),
(ctxt == nullptr) ? nullptr : reinterpret_cast<CensusContext*>(ctxt));
size_t tracing_len = TraceContextSerialize(context_.Context(), tracing_buf_,
kMaxTraceContextLen);
size_t tracing_len = TraceContextSerialize(
tracer_.context()->Context(), tracing_buf_, kMaxTraceContextLen);
if (tracing_len > 0) {
GRPC_LOG_IF_ERROR(
"census grpc_filter",
@ -159,7 +159,7 @@ void CensusClientCallData::Destroy(grpc_call_element* /*elem*/,
const uint64_t response_size = GetIncomingDataSize(final_info);
double latency_ms = absl::ToDoubleMilliseconds(absl::Now() - start_time_);
std::vector<std::pair<opencensus::tags::TagKey, std::string>> tags =
context_.tags().tags();
tracer_.context()->tags().tags();
std::string method = absl::StrCat(method_);
tags.emplace_back(ClientMethodTagKey(), method);
std::string final_status =
@ -177,10 +177,11 @@ void CensusClientCallData::Destroy(grpc_call_element* /*elem*/,
grpc_slice_unref_internal(path_);
if (final_info->final_status != GRPC_STATUS_OK) {
// TODO(unknown): Map grpc_status_code to trace::StatusCode.
context_.Span().SetStatus(opencensus::trace::StatusCode::UNKNOWN,
StatusCodeToString(final_info->final_status));
tracer_.context()->Span().SetStatus(
opencensus::trace::StatusCode::UNKNOWN,
StatusCodeToString(final_info->final_status));
}
context_.EndSpan();
tracer_.context()->EndSpan();
}
} // namespace grpc

@ -24,7 +24,7 @@
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "src/cpp/ext/filters/census/channel_filter.h"
#include "src/cpp/ext/filters/census/context.h"
#include "src/cpp/ext/filters/census/open_census_call_tracer.h"
namespace grpc {
@ -72,7 +72,7 @@ class CensusClientCallData : public CallData {
static void OnDoneRecvMessageCb(void* user_data, grpc_error_handle error);
private:
CensusContext context_;
OpenCensusCallTracer tracer_;
// Metadata elements for tracing and census stats data.
grpc_linked_mdelem stats_bin_;
grpc_linked_mdelem tracing_bin_;

@ -0,0 +1,70 @@
//
//
// Copyright 2021 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#ifndef GRPC_INTERNAL_CPP_EXT_FILTERS_OPEN_CENSUS_CALL_TRACER_H
#define GRPC_INTERNAL_CPP_EXT_FILTERS_OPEN_CENSUS_CALL_TRACER_H
#include <grpc/support/port_platform.h>
#include "src/core/lib/channel/call_tracer.h"
#include "src/cpp/ext/filters/census/context.h"
namespace grpc {
class OpenCensusCallTracer : public grpc_core::CallTracer {
public:
class OpenCensusCallAttemptTracer : public CallAttemptTracer {
public:
void RecordSendInitialMetadata(
grpc_metadata_batch* /* send_initial_metadata */,
uint32_t /* flags */) override {}
void RecordOnDoneSendInitialMetadata() override {}
void RecordSendTrailingMetadata(
grpc_metadata_batch* /* send_trailing_metadata */) override {}
void RecordSendMessage(
const grpc_core::ByteStream& /* send_message */) override {}
void RecordReceivedInitialMetadata(
grpc_metadata_batch* /* recv_initial_metadata */, uint32_t /* flags */,
gpr_atm* /* peer_string */) override {}
void RecordReceivedMessage(
const grpc_core::ByteStream& /* recv_message */) override {}
void RecordReceivedTrailingMetadata(
grpc_metadata_batch* /* recv_trailing_metadata */) override {}
void RecordCancel(grpc_error_handle /* cancel_error */) override {}
void RecordEnd(const grpc_call_final_info& /* final_info */) override {}
CensusContext* context() { return &context_; }
private:
CensusContext context_;
};
OpenCensusCallAttemptTracer* StartNewAttempt(
bool /* is_transparent_retry */) override {
return nullptr;
}
CensusContext* context() { return &context_; }
private:
CensusContext context_;
};
}; // namespace grpc
#endif // GRPC_INTERNAL_CPP_EXT_FILTERS_OPEN_CENSUS_CALL_TRACER_H

@ -1649,6 +1649,7 @@ src/core/lib/avl/avl.cc \
src/core/lib/avl/avl.h \
src/core/lib/backoff/backoff.cc \
src/core/lib/backoff/backoff.h \
src/core/lib/channel/call_tracer.h \
src/core/lib/channel/channel_args.cc \
src/core/lib/channel/channel_args.h \
src/core/lib/channel/channel_stack.cc \

@ -1486,6 +1486,7 @@ src/core/lib/avl/avl.h \
src/core/lib/backoff/backoff.cc \
src/core/lib/backoff/backoff.h \
src/core/lib/channel/README.md \
src/core/lib/channel/call_tracer.h \
src/core/lib/channel/channel_args.cc \
src/core/lib/channel/channel_args.h \
src/core/lib/channel/channel_stack.cc \

Loading…
Cancel
Save