From 6a97f492ff46042e924acd4135b9a8c823e1d88c Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Fri, 16 Dec 2022 02:28:59 +0530 Subject: [PATCH] Add method to add annotations to call tracer (#31901) --- include/grpcpp/opencensus.h | 5 +++++ src/core/lib/channel/call_tracer.h | 8 ++++++++ src/cpp/ext/filters/census/client_filter.cc | 9 +++++++++ src/cpp/ext/filters/census/open_census_call_tracer.h | 2 ++ 4 files changed, 24 insertions(+) diff --git a/include/grpcpp/opencensus.h b/include/grpcpp/opencensus.h index 1eb0a775596..6baec882b25 100644 --- a/include/grpcpp/opencensus.h +++ b/include/grpcpp/opencensus.h @@ -186,6 +186,11 @@ class CensusContext { span_.AddAttribute(key, attribute); } + void AddSpanAnnotation(absl::string_view description, + opencensus::trace::AttributesRef attributes) { + span_.AddAnnotation(description, attributes); + } + const ::opencensus::trace::Span& Span() const { return span_; } const ::opencensus::tags::TagMap& tags() const { return tags_; } diff --git a/src/core/lib/channel/call_tracer.h b/src/core/lib/channel/call_tracer.h index 6a4bd427acd..1f9e088114f 100644 --- a/src/core/lib/channel/call_tracer.h +++ b/src/core/lib/channel/call_tracer.h @@ -75,6 +75,10 @@ class CallTracer { // Should be the last API call to the object. Once invoked, the tracer // library is free to destroy the object. virtual void RecordEnd(const gpr_timespec& latency) = 0; + // Records an annotation on the call attempt. + // TODO(yashykt): If needed, extend this to attach attributes with + // annotations. + virtual void RecordAnnotation(absl::string_view annotation) = 0; }; virtual ~CallTracer() {} @@ -87,6 +91,10 @@ class CallTracer { // 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; + // Records an annotation on the call attempt. + // TODO(yashykt): If needed, extend this to attach attributes with + // annotations. + virtual void RecordAnnotation(absl::string_view annotation) = 0; }; } // namespace grpc_core diff --git a/src/cpp/ext/filters/census/client_filter.cc b/src/cpp/ext/filters/census/client_filter.cc index 4d11e8ea3c5..6f07d33044b 100644 --- a/src/cpp/ext/filters/census/client_filter.cc +++ b/src/cpp/ext/filters/census/client_filter.cc @@ -246,6 +246,11 @@ void OpenCensusCallTracer::OpenCensusCallAttemptTracer::RecordEnd( } } +void OpenCensusCallTracer::OpenCensusCallAttemptTracer::RecordAnnotation( + absl::string_view annotation) { + context_.AddSpanAnnotation(annotation, {}); +} + // // OpenCensusCallTracer // @@ -312,6 +317,10 @@ OpenCensusCallTracer::StartNewAttempt(bool is_transparent_retry) { this, attempt_num, is_transparent_retry, false /* arena_allocated */); } +void OpenCensusCallTracer::RecordAnnotation(absl::string_view annotation) { + context_.AddSpanAnnotation(annotation, {}); +} + CensusContext OpenCensusCallTracer::CreateCensusContextForCallAttempt() { if (!OpenCensusTracingEnabled() || !tracing_enabled_) return CensusContext(); GPR_DEBUG_ASSERT(context_.Context().IsValid()); diff --git a/src/cpp/ext/filters/census/open_census_call_tracer.h b/src/cpp/ext/filters/census/open_census_call_tracer.h index 5ffba79f407..99c160311de 100644 --- a/src/cpp/ext/filters/census/open_census_call_tracer.h +++ b/src/cpp/ext/filters/census/open_census_call_tracer.h @@ -81,6 +81,7 @@ class OpenCensusCallTracer : public grpc_core::CallTracer { const grpc_transport_stream_stats* transport_stream_stats) override; void RecordCancel(grpc_error_handle cancel_error) override; void RecordEnd(const gpr_timespec& /*latency*/) override; + void RecordAnnotation(absl::string_view annotation) override; experimental::CensusContext* context() { return &context_; } @@ -108,6 +109,7 @@ class OpenCensusCallTracer : public grpc_core::CallTracer { void GenerateContext(); OpenCensusCallAttemptTracer* StartNewAttempt( bool is_transparent_retry) override; + void RecordAnnotation(absl::string_view annotation) override; private: experimental::CensusContext CreateCensusContextForCallAttempt();