GCP Observability: Fix constant labels support for stats when tracing is disabled (#32451)

<!--

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/32461/head
Yash Tibrewal 2 years ago committed by GitHub
parent 29d4679750
commit d93d0022cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      include/grpcpp/opencensus.h
  2. 21
      src/cpp/ext/filters/census/client_filter.cc
  3. 10
      src/cpp/ext/filters/census/context.cc
  4. 8
      src/cpp/ext/filters/census/server_filter.cc
  5. 1
      test/cpp/ext/filters/census/constant_labels_wait_test.cc

@ -174,6 +174,9 @@ class CensusContext {
const ::opencensus::tags::TagMap& tags)
: span_(::opencensus::trace::Span::StartSpan(name)), tags_(tags) {}
explicit CensusContext(const ::opencensus::tags::TagMap& tags)
: span_(::opencensus::trace::Span::BlankSpan()), tags_(tags) {}
CensusContext(absl::string_view name, const ::opencensus::trace::Span* parent,
const ::opencensus::tags::TagMap& tags)
: span_(::opencensus::trace::Span::StartSpan(name, parent)),

@ -294,13 +294,11 @@ OpenCensusCallTracer::OpenCensusCallTracer(
method_(GetMethod(path_)),
arena_(arena),
tracing_enabled_(tracing_enabled) {
if (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);
}
auto* parent_context = reinterpret_cast<CensusContext*>(
call_context_[GRPC_CONTEXT_TRACING].value);
GenerateClientContext(tracing_enabled_ ? absl::StrCat("Sent.", method_) : "",
&context_,
(parent_context == nullptr) ? nullptr : parent_context);
}
OpenCensusCallTracer::~OpenCensusCallTracer() {
@ -319,13 +317,6 @@ OpenCensusCallTracer::~OpenCensusCallTracer() {
}
}
void OpenCensusCallTracer::GenerateContext() {
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::OpenCensusCallAttemptTracer*
OpenCensusCallTracer::StartNewAttempt(bool is_transparent_retry) {
// We allocate the first attempt on the arena and all subsequent attempts on
@ -363,7 +354,7 @@ void OpenCensusCallTracer::RecordAnnotation(absl::string_view annotation) {
}
CensusContext OpenCensusCallTracer::CreateCensusContextForCallAttempt() {
if (!tracing_enabled_) return CensusContext();
if (!tracing_enabled_) return CensusContext(context_.tags());
GPR_DEBUG_ASSERT(context_.Context().IsValid());
auto context = CensusContext(absl::StrCat("Attempt.", method_),
&(context_.Span()), context_.tags());

@ -42,6 +42,11 @@ void GenerateServerContext(absl::string_view tracing, absl::string_view method,
// Destruct the current CensusContext to free the Span memory before
// overwriting it below.
context->~CensusContext();
if (method.empty()) {
new (context) CensusContext(grpc::internal::OpenCensusRegistry::Get()
.PopulateTagMapWithConstantLabels({}));
return;
}
SpanContext parent_ctx =
opencensus::trace::propagation::FromGrpcTraceBinHeader(tracing);
if (parent_ctx.IsValid()) {
@ -62,6 +67,11 @@ void GenerateClientContext(absl::string_view method, CensusContext* ctxt,
// Destruct the current CensusContext to free the Span memory before
// overwriting it below.
ctxt->~CensusContext();
if (method.empty()) {
new (ctxt) CensusContext(grpc::internal::OpenCensusRegistry::Get()
.PopulateTagMapWithConstantLabels({}));
return;
}
if (parent_ctxt != nullptr) {
SpanContext span_ctxt = parent_ctxt->Context();
Span span = parent_ctxt->Span();

@ -141,9 +141,11 @@ OpenCensusServerCallData::OpenCensusServerCallData(
FilterInitialMetadata(client_initial_metadata, &sml);
path_ = std::move(sml.path);
method_ = GetMethod(path_);
if (OpenCensusTracingEnabled()) {
GenerateServerContext(sml.tracing_slice.as_string_view(),
absl::StrCat("Recv.", method_), &context_);
auto tracing_enabled = OpenCensusTracingEnabled();
GenerateServerContext(
tracing_enabled ? sml.tracing_slice.as_string_view() : "",
absl::StrCat("Recv.", method_), &context_);
if (tracing_enabled) {
auto* call_context = grpc_core::GetContext<grpc_call_context_element>();
call_context[GRPC_CONTEXT_TRACING].value = &context_;
}

@ -49,6 +49,7 @@ class ConstantLabelsWaitTest : public StatsPluginEnd2EndTest {
protected:
static void SetUpTestSuite() {
grpc::internal::OpenCensusRegistry::Get().RegisterWaitOnReady();
grpc::internal::EnableOpenCensusTracing(false);
StatsPluginEnd2EndTest::SetUpTestSuite();
}
};

Loading…
Cancel
Save