|
|
|
@ -46,6 +46,7 @@ |
|
|
|
|
#include <grpc/support/log.h> |
|
|
|
|
#include <grpcpp/support/config.h> |
|
|
|
|
|
|
|
|
|
#include "src/core/lib/channel/channel_args.h" |
|
|
|
|
#include "src/core/lib/channel/context.h" |
|
|
|
|
#include "src/core/lib/gprpp/sync.h" |
|
|
|
|
#include "src/core/lib/resource_quota/arena.h" |
|
|
|
@ -64,9 +65,27 @@ constexpr uint32_t |
|
|
|
|
constexpr uint32_t |
|
|
|
|
OpenCensusCallTracer::OpenCensusCallAttemptTracer::kMaxTagsLen; |
|
|
|
|
|
|
|
|
|
grpc_error_handle CensusClientCallData::Init( |
|
|
|
|
grpc_call_element* /* elem */, const grpc_call_element_args* args) { |
|
|
|
|
tracer_ = args->arena->New<OpenCensusCallTracer>(args); |
|
|
|
|
//
|
|
|
|
|
// CensusClientChannelData
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
grpc_error_handle CensusClientChannelData::Init( |
|
|
|
|
grpc_channel_element* /*elem*/, grpc_channel_element_args* args) { |
|
|
|
|
tracing_enabled_ = grpc_core::ChannelArgs::FromC(args->channel_args) |
|
|
|
|
.GetInt(GRPC_ARG_ENABLE_OBSERVABILITY) |
|
|
|
|
.value_or(true); |
|
|
|
|
return GRPC_ERROR_NONE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// CensusClientChannelData::CensusClientCallData
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
grpc_error_handle CensusClientChannelData::CensusClientCallData::Init( |
|
|
|
|
grpc_call_element* elem, const grpc_call_element_args* args) { |
|
|
|
|
tracer_ = args->arena->New<OpenCensusCallTracer>( |
|
|
|
|
args, (static_cast<CensusClientChannelData*>(elem->channel_data)) |
|
|
|
|
->tracing_enabled_); |
|
|
|
|
GPR_DEBUG_ASSERT(args->context[GRPC_CONTEXT_CALL_TRACER].value == nullptr); |
|
|
|
|
args->context[GRPC_CONTEXT_CALL_TRACER].value = tracer_; |
|
|
|
|
args->context[GRPC_CONTEXT_CALL_TRACER].destroy = [](void* tracer) { |
|
|
|
@ -75,14 +94,16 @@ grpc_error_handle CensusClientCallData::Init( |
|
|
|
|
return GRPC_ERROR_NONE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CensusClientCallData::StartTransportStreamOpBatch( |
|
|
|
|
void CensusClientChannelData::CensusClientCallData::StartTransportStreamOpBatch( |
|
|
|
|
grpc_call_element* elem, TransportStreamOpBatch* op) { |
|
|
|
|
// Note that we are generating the overall call context here instead of in
|
|
|
|
|
// the constructor of `OpenCensusCallTracer` due to the semantics of
|
|
|
|
|
// `grpc_census_call_set_context` which allows the application to set the
|
|
|
|
|
// census context for a call anytime before the first call to
|
|
|
|
|
// `grpc_call_start_batch`.
|
|
|
|
|
if (op->op()->send_initial_metadata) { |
|
|
|
|
if (op->op()->send_initial_metadata && |
|
|
|
|
(static_cast<CensusClientChannelData*>(elem->channel_data)) |
|
|
|
|
->tracing_enabled_) { |
|
|
|
|
tracer_->GenerateContext(); |
|
|
|
|
} |
|
|
|
|
grpc_call_next_op(elem, op->op()); |
|
|
|
@ -92,27 +113,17 @@ void CensusClientCallData::StartTransportStreamOpBatch( |
|
|
|
|
// OpenCensusCallTracer::OpenCensusCallAttemptTracer
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
CensusContext CreateCensusContextForCallAttempt( |
|
|
|
|
absl::string_view method, const CensusContext& parent_context) { |
|
|
|
|
GPR_DEBUG_ASSERT(parent_context.Context().IsValid()); |
|
|
|
|
return CensusContext(absl::StrCat("Attempt.", method), &parent_context.Span(), |
|
|
|
|
parent_context.tags()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
OpenCensusCallTracer::OpenCensusCallAttemptTracer::OpenCensusCallAttemptTracer( |
|
|
|
|
OpenCensusCallTracer* parent, uint64_t attempt_num, |
|
|
|
|
bool is_transparent_retry, bool arena_allocated) |
|
|
|
|
: parent_(parent), |
|
|
|
|
arena_allocated_(arena_allocated), |
|
|
|
|
context_(CreateCensusContextForCallAttempt(parent_->method_, |
|
|
|
|
parent_->context_)), |
|
|
|
|
context_(parent_->CreateCensusContextForCallAttempt()), |
|
|
|
|
start_time_(absl::Now()) { |
|
|
|
|
context_.AddSpanAttribute("previous-rpc-attempts", attempt_num); |
|
|
|
|
context_.AddSpanAttribute("transparent-retry", is_transparent_retry); |
|
|
|
|
if (parent_->tracing_enabled_) { |
|
|
|
|
context_.AddSpanAttribute("previous-rpc-attempts", attempt_num); |
|
|
|
|
context_.AddSpanAttribute("transparent-retry", is_transparent_retry); |
|
|
|
|
} |
|
|
|
|
std::vector<std::pair<opencensus::tags::TagKey, std::string>> tags = |
|
|
|
|
context_.tags().tags(); |
|
|
|
|
tags.emplace_back(ClientMethodTagKey(), std::string(parent_->method_)); |
|
|
|
@ -121,20 +132,22 @@ OpenCensusCallTracer::OpenCensusCallAttemptTracer::OpenCensusCallAttemptTracer( |
|
|
|
|
|
|
|
|
|
void OpenCensusCallTracer::OpenCensusCallAttemptTracer:: |
|
|
|
|
RecordSendInitialMetadata(grpc_metadata_batch* send_initial_metadata) { |
|
|
|
|
char tracing_buf[kMaxTraceContextLen]; |
|
|
|
|
size_t tracing_len = TraceContextSerialize(context_.Context(), tracing_buf, |
|
|
|
|
kMaxTraceContextLen); |
|
|
|
|
if (tracing_len > 0) { |
|
|
|
|
send_initial_metadata->Set( |
|
|
|
|
grpc_core::GrpcTraceBinMetadata(), |
|
|
|
|
grpc_core::Slice::FromCopiedBuffer(tracing_buf, tracing_len)); |
|
|
|
|
} |
|
|
|
|
grpc_slice tags = grpc_empty_slice(); |
|
|
|
|
// TODO(unknown): Add in tagging serialization.
|
|
|
|
|
size_t encoded_tags_len = StatsContextSerialize(kMaxTagsLen, &tags); |
|
|
|
|
if (encoded_tags_len > 0) { |
|
|
|
|
send_initial_metadata->Set(grpc_core::GrpcTagsBinMetadata(), |
|
|
|
|
grpc_core::Slice(tags)); |
|
|
|
|
if (parent_->tracing_enabled_) { |
|
|
|
|
char tracing_buf[kMaxTraceContextLen]; |
|
|
|
|
size_t tracing_len = TraceContextSerialize(context_.Context(), tracing_buf, |
|
|
|
|
kMaxTraceContextLen); |
|
|
|
|
if (tracing_len > 0) { |
|
|
|
|
send_initial_metadata->Set( |
|
|
|
|
grpc_core::GrpcTraceBinMetadata(), |
|
|
|
|
grpc_core::Slice::FromCopiedBuffer(tracing_buf, tracing_len)); |
|
|
|
|
} |
|
|
|
|
grpc_slice tags = grpc_empty_slice(); |
|
|
|
|
// TODO(unknown): Add in tagging serialization.
|
|
|
|
|
size_t encoded_tags_len = StatsContextSerialize(kMaxTagsLen, &tags); |
|
|
|
|
if (encoded_tags_len > 0) { |
|
|
|
|
send_initial_metadata->Set(grpc_core::GrpcTagsBinMetadata(), |
|
|
|
|
grpc_core::Slice(tags)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -204,11 +217,13 @@ void OpenCensusCallTracer::OpenCensusCallAttemptTracer::RecordEnd( |
|
|
|
|
{RpcClientSentMessagesPerRpc(), sent_message_count_}, |
|
|
|
|
{RpcClientReceivedMessagesPerRpc(), recv_message_count_}}, |
|
|
|
|
tags); |
|
|
|
|
if (status_code_ != absl::StatusCode::kOk) { |
|
|
|
|
context_.Span().SetStatus(opencensus::trace::StatusCode(status_code_), |
|
|
|
|
StatusCodeToString(status_code_)); |
|
|
|
|
if (parent_->tracing_enabled_) { |
|
|
|
|
if (status_code_ != absl::StatusCode::kOk) { |
|
|
|
|
context_.Span().SetStatus(opencensus::trace::StatusCode(status_code_), |
|
|
|
|
StatusCodeToString(status_code_)); |
|
|
|
|
} |
|
|
|
|
context_.EndSpan(); |
|
|
|
|
} |
|
|
|
|
context_.EndSpan(); |
|
|
|
|
grpc_core::MutexLock lock(&parent_->mu_); |
|
|
|
|
if (--parent_->num_active_rpcs_ == 0) { |
|
|
|
|
parent_->time_at_last_attempt_end_ = absl::Now(); |
|
|
|
@ -224,11 +239,13 @@ void OpenCensusCallTracer::OpenCensusCallAttemptTracer::RecordEnd( |
|
|
|
|
// OpenCensusCallTracer
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
OpenCensusCallTracer::OpenCensusCallTracer(const grpc_call_element_args* args) |
|
|
|
|
OpenCensusCallTracer::OpenCensusCallTracer(const grpc_call_element_args* args, |
|
|
|
|
bool tracing_enabled) |
|
|
|
|
: call_context_(args->context), |
|
|
|
|
path_(grpc_slice_ref(args->path)), |
|
|
|
|
method_(GetMethod(path_)), |
|
|
|
|
arena_(args->arena) {} |
|
|
|
|
arena_(args->arena), |
|
|
|
|
tracing_enabled_(tracing_enabled) {} |
|
|
|
|
|
|
|
|
|
OpenCensusCallTracer::~OpenCensusCallTracer() { |
|
|
|
|
std::vector<std::pair<opencensus::tags::TagKey, std::string>> tags = |
|
|
|
@ -239,7 +256,9 @@ OpenCensusCallTracer::~OpenCensusCallTracer() { |
|
|
|
|
{RpcClientTransparentRetriesPerCall(), transparent_retries_}, |
|
|
|
|
{RpcClientRetryDelayPerCall(), ToDoubleMilliseconds(retry_delay_)}}, |
|
|
|
|
tags); |
|
|
|
|
context_.EndSpan(); |
|
|
|
|
if (tracing_enabled_) { |
|
|
|
|
context_.EndSpan(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void OpenCensusCallTracer::GenerateContext() { |
|
|
|
@ -280,4 +299,11 @@ OpenCensusCallTracer::StartNewAttempt(bool is_transparent_retry) { |
|
|
|
|
this, attempt_num, is_transparent_retry, false /* arena_allocated */); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CensusContext OpenCensusCallTracer::CreateCensusContextForCallAttempt() { |
|
|
|
|
if (!tracing_enabled_) return CensusContext(); |
|
|
|
|
GPR_DEBUG_ASSERT(context_.Context().IsValid()); |
|
|
|
|
return CensusContext(absl::StrCat("Attempt.", method_), &(context_.Span()), |
|
|
|
|
context_.tags()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace grpc
|
|
|
|
|