1 // 2 // 3 // Copyright 2021 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPC_INTERNAL_CPP_EXT_FILTERS_OPEN_CENSUS_CALL_TRACER_H 20 #define GRPC_INTERNAL_CPP_EXT_FILTERS_OPEN_CENSUS_CALL_TRACER_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include "src/core/lib/channel/call_tracer.h" 25 #include "src/cpp/ext/filters/census/context.h" 26 27 namespace grpc { 28 29 class OpenCensusCallTracer : public grpc_core::CallTracer { 30 public: 31 class OpenCensusCallAttemptTracer : public CallAttemptTracer { 32 public: 33 OpenCensusCallAttemptTracer(OpenCensusCallTracer* parent, 34 uint64_t attempt_num, bool is_transparent_retry, 35 bool arena_allocated); 36 void RecordSendInitialMetadata( 37 grpc_metadata_batch* /* send_initial_metadata */, 38 uint32_t /* flags */) override; RecordOnDoneSendInitialMetadata(gpr_atm *)39 void RecordOnDoneSendInitialMetadata(gpr_atm* /* peer_string */) override {} RecordSendTrailingMetadata(grpc_metadata_batch *)40 void RecordSendTrailingMetadata( 41 grpc_metadata_batch* /* send_trailing_metadata */) override {} 42 void RecordSendMessage( 43 const grpc_core::ByteStream& /* send_message */) override; RecordReceivedInitialMetadata(grpc_metadata_batch *,uint32_t)44 void RecordReceivedInitialMetadata( 45 grpc_metadata_batch* /* recv_initial_metadata */, 46 uint32_t /* flags */) override {} 47 void RecordReceivedMessage( 48 const grpc_core::ByteStream& /* recv_message */) override; 49 void RecordReceivedTrailingMetadata( 50 absl::Status /* status */, grpc_metadata_batch* recv_trailing_metadata, 51 const grpc_transport_stream_stats& /* transport_stream_stats */) 52 override; 53 void RecordCancel(grpc_error_handle cancel_error) override; 54 void RecordEnd(const gpr_timespec& /* latency */) override; 55 context()56 CensusContext* context() { return &context_; } 57 58 private: 59 // Maximum size of trace context is sent on the wire. 60 static constexpr uint32_t kMaxTraceContextLen = 64; 61 // Maximum size of tags that are sent on the wire. 62 static constexpr uint32_t kMaxTagsLen = 2048; 63 OpenCensusCallTracer* parent_; 64 const bool arena_allocated_; 65 CensusContext context_; 66 // Metadata elements for tracing and census stats data. 67 grpc_linked_mdelem stats_bin_; 68 grpc_linked_mdelem tracing_bin_; 69 // Start time (for measuring latency). 70 absl::Time start_time_; 71 // Server elapsed time in nanoseconds. 72 uint64_t elapsed_time_ = 0; 73 // Number of messages in this RPC. 74 uint64_t recv_message_count_ = 0; 75 uint64_t sent_message_count_ = 0; 76 // End status code 77 absl::StatusCode status_code_; 78 // Buffer needed for grpc_slice to reference when adding trace context 79 // metatdata to outgoing message. 80 char tracing_buf_[kMaxTraceContextLen]; 81 }; 82 83 explicit OpenCensusCallTracer(const grpc_call_element_args* args); 84 ~OpenCensusCallTracer() override; 85 86 void GenerateContext(); 87 OpenCensusCallAttemptTracer* StartNewAttempt( 88 bool is_transparent_retry) override; 89 90 private: 91 const grpc_call_context_element* call_context_; 92 // Client method. 93 grpc_slice path_; 94 absl::string_view method_; 95 CensusContext context_; 96 grpc_core::Arena* arena_; 97 grpc_core::Mutex mu_; 98 // Non-transparent attempts per call 99 uint64_t retries_ ABSL_GUARDED_BY(&mu_) = 0; 100 // Transparent retries per call 101 uint64_t transparent_retries_ ABSL_GUARDED_BY(&mu_) = 0; 102 // Retry delay 103 absl::Duration retry_delay_ ABSL_GUARDED_BY(&mu_); 104 absl::Time time_at_last_attempt_end_ ABSL_GUARDED_BY(&mu_); 105 uint64_t num_active_rpcs_ ABSL_GUARDED_BY(&mu_) = 0; 106 }; 107 108 }; // namespace grpc 109 110 #endif // GRPC_INTERNAL_CPP_EXT_FILTERS_OPEN_CENSUS_CALL_TRACER_H 111