|
|
|
@ -24,6 +24,8 @@ |
|
|
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
|
|
#include <algorithm> |
|
|
|
|
#include <functional> |
|
|
|
|
#include <memory> |
|
|
|
|
#include <string> |
|
|
|
|
#include <utility> |
|
|
|
|
#include <vector> |
|
|
|
@ -47,9 +49,11 @@ |
|
|
|
|
#include <grpcpp/opencensus.h> |
|
|
|
|
|
|
|
|
|
#include "src/core/lib/channel/channel_args.h" |
|
|
|
|
#include "src/core/lib/channel/channel_stack.h" |
|
|
|
|
#include "src/core/lib/channel/context.h" |
|
|
|
|
#include "src/core/lib/experiments/experiments.h" |
|
|
|
|
#include "src/core/lib/gprpp/sync.h" |
|
|
|
|
#include "src/core/lib/promise/context.h" |
|
|
|
|
#include "src/core/lib/resource_quota/arena.h" |
|
|
|
|
#include "src/core/lib/slice/slice.h" |
|
|
|
|
#include "src/core/lib/slice/slice_buffer.h" |
|
|
|
@ -58,6 +62,7 @@ |
|
|
|
|
#include "src/cpp/ext/filters/census/context.h" |
|
|
|
|
#include "src/cpp/ext/filters/census/grpc_plugin.h" |
|
|
|
|
#include "src/cpp/ext/filters/census/measures.h" |
|
|
|
|
#include "src/cpp/ext/filters/census/open_census_call_tracer.h" |
|
|
|
|
|
|
|
|
|
namespace grpc { |
|
|
|
|
namespace internal { |
|
|
|
@ -68,53 +73,42 @@ constexpr uint32_t |
|
|
|
|
OpenCensusCallTracer::OpenCensusCallAttemptTracer::kMaxTagsLen; |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// OpenCensusClientChannelData
|
|
|
|
|
// OpenCensusClientFilter
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
grpc_error_handle OpenCensusClientChannelData::Init( |
|
|
|
|
grpc_channel_element* /*elem*/, grpc_channel_element_args* args) { |
|
|
|
|
const grpc_channel_filter OpenCensusClientFilter::kFilter = |
|
|
|
|
grpc_core::MakePromiseBasedFilter<OpenCensusClientFilter, |
|
|
|
|
grpc_core::FilterEndpoint::kClient, 0>( |
|
|
|
|
"opencensus_client"); |
|
|
|
|
|
|
|
|
|
absl::StatusOr<OpenCensusClientFilter> OpenCensusClientFilter::Create( |
|
|
|
|
const grpc_core::ChannelArgs& args, ChannelFilter::Args /*filter_args*/) { |
|
|
|
|
bool observability_enabled = |
|
|
|
|
args->channel_args.GetInt(GRPC_ARG_ENABLE_OBSERVABILITY).value_or(true); |
|
|
|
|
args.GetInt(GRPC_ARG_ENABLE_OBSERVABILITY).value_or(true); |
|
|
|
|
// Only run the Post-Init Registry if observability is enabled to avoid
|
|
|
|
|
// running into a cyclic loop for exporter channels.
|
|
|
|
|
if (observability_enabled) { |
|
|
|
|
OpenCensusRegistry::Get().RunFunctionsPostInit(); |
|
|
|
|
} |
|
|
|
|
tracing_enabled_ = observability_enabled; |
|
|
|
|
return absl::OkStatus(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// OpenCensusClientChannelData::OpenCensusClientCallData
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
grpc_error_handle OpenCensusClientChannelData::OpenCensusClientCallData::Init( |
|
|
|
|
grpc_call_element* elem, const grpc_call_element_args* args) { |
|
|
|
|
tracer_ = args->arena->New<OpenCensusCallTracer>( |
|
|
|
|
args, (static_cast<OpenCensusClientChannelData*>(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) { |
|
|
|
|
(static_cast<OpenCensusCallTracer*>(tracer))->~OpenCensusCallTracer(); |
|
|
|
|
}; |
|
|
|
|
return absl::OkStatus(); |
|
|
|
|
return OpenCensusClientFilter(/*tracing_enabled=*/observability_enabled); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void OpenCensusClientChannelData::OpenCensusClientCallData:: |
|
|
|
|
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 && OpenCensusTracingEnabled() && |
|
|
|
|
(static_cast<OpenCensusClientChannelData*>(elem->channel_data)) |
|
|
|
|
->tracing_enabled_) { |
|
|
|
|
tracer_->GenerateContext(); |
|
|
|
|
} |
|
|
|
|
grpc_call_next_op(elem, op->op()); |
|
|
|
|
grpc_core::ArenaPromise<grpc_core::ServerMetadataHandle> |
|
|
|
|
OpenCensusClientFilter::MakeCallPromise( |
|
|
|
|
grpc_core::CallArgs call_args, |
|
|
|
|
grpc_core::NextPromiseFactory next_promise_factory) { |
|
|
|
|
auto* call_context = grpc_core::GetContext<grpc_call_context_element>(); |
|
|
|
|
auto* path = call_args.client_initial_metadata->get_pointer( |
|
|
|
|
grpc_core::HttpPathMetadata()); |
|
|
|
|
auto* tracer = |
|
|
|
|
grpc_core::GetContext<grpc_core::Arena>() |
|
|
|
|
->ManagedNew<OpenCensusCallTracer>( |
|
|
|
|
call_context, path != nullptr ? path->Ref() : grpc_core::Slice(), |
|
|
|
|
grpc_core::GetContext<grpc_core::Arena>(), tracing_enabled_); |
|
|
|
|
GPR_DEBUG_ASSERT(call_context[GRPC_CONTEXT_CALL_TRACER].value == nullptr); |
|
|
|
|
call_context[GRPC_CONTEXT_CALL_TRACER].value = tracer; |
|
|
|
|
call_context[GRPC_CONTEXT_CALL_TRACER].destroy = nullptr; |
|
|
|
|
return next_promise_factory(std::move(call_args)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
@ -226,7 +220,7 @@ void OpenCensusCallTracer::OpenCensusCallAttemptTracer:: |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void OpenCensusCallTracer::OpenCensusCallAttemptTracer::RecordCancel( |
|
|
|
|
grpc_error_handle /*cancel_error*/) { |
|
|
|
|
absl::Status /*cancel_error*/) { |
|
|
|
|
status_code_ = absl::StatusCode::kCancelled; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -273,13 +267,19 @@ void OpenCensusCallTracer::OpenCensusCallAttemptTracer::RecordAnnotation( |
|
|
|
|
// OpenCensusCallTracer
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
OpenCensusCallTracer::OpenCensusCallTracer(const grpc_call_element_args* args, |
|
|
|
|
bool tracing_enabled) |
|
|
|
|
: call_context_(args->context), |
|
|
|
|
path_(grpc_slice_ref(args->path)), |
|
|
|
|
OpenCensusCallTracer::OpenCensusCallTracer( |
|
|
|
|
grpc_call_context_element* call_context, grpc_core::Slice path, |
|
|
|
|
grpc_core::Arena* arena, bool tracing_enabled) |
|
|
|
|
: call_context_(call_context), |
|
|
|
|
path_(std::move(path)), |
|
|
|
|
method_(GetMethod(path_)), |
|
|
|
|
arena_(args->arena), |
|
|
|
|
tracing_enabled_(tracing_enabled) {} |
|
|
|
|
arena_(arena), |
|
|
|
|
tracing_enabled_(tracing_enabled) { |
|
|
|
|
auto* parent_context = reinterpret_cast<CensusContext*>( |
|
|
|
|
call_context_[GRPC_CONTEXT_TRACING].value); |
|
|
|
|
GenerateClientContext(absl::StrCat("Sent.", method_), &context_, |
|
|
|
|
(parent_context == nullptr) ? nullptr : parent_context); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
OpenCensusCallTracer::~OpenCensusCallTracer() { |
|
|
|
|
if (OpenCensusStatsEnabled()) { |
|
|
|
|