1 /*
2  *
3  * Copyright 2018 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_CENSUS_CLIENT_FILTER_H
20 #define GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CLIENT_FILTER_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "absl/strings/string_view.h"
25 #include "absl/time/time.h"
26 #include "src/cpp/ext/filters/census/channel_filter.h"
27 #include "src/cpp/ext/filters/census/context.h"
28 
29 namespace grpc {
30 
31 // A CallData class will be created for every grpc call within a channel. It is
32 // used to store data and methods specific to that call. CensusClientCallData is
33 // thread-compatible, however typically only 1 thread should be interacting with
34 // a call at a time.
35 class CensusClientCallData : public CallData {
36  public:
37   // Maximum size of trace context is sent on the wire.
38   static constexpr uint32_t kMaxTraceContextLen = 64;
39   // Maximum size of tags that are sent on the wire.
40   static constexpr uint32_t kMaxTagsLen = 2048;
41 
CensusClientCallData()42   CensusClientCallData()
43       : recv_trailing_metadata_(nullptr),
44         initial_on_done_recv_trailing_metadata_(nullptr),
45         initial_on_done_recv_message_(nullptr),
46         elapsed_time_(0),
47         recv_message_(nullptr),
48         recv_message_count_(0),
49         sent_message_count_(0) {
50     memset(&stats_bin_, 0, sizeof(grpc_linked_mdelem));
51     memset(&tracing_bin_, 0, sizeof(grpc_linked_mdelem));
52     memset(&path_, 0, sizeof(grpc_slice));
53     memset(&on_done_recv_trailing_metadata_, 0, sizeof(grpc_closure));
54     memset(&on_done_recv_message_, 0, sizeof(grpc_closure));
55   }
56 
57   grpc_error* Init(grpc_call_element* elem,
58                    const grpc_call_element_args* args) override;
59 
60   void Destroy(grpc_call_element* elem, const grpc_call_final_info* final_info,
61                grpc_closure* then_call_closure) override;
62 
63   void StartTransportStreamOpBatch(grpc_call_element* elem,
64                                    TransportStreamOpBatch* op) override;
65 
66   static void OnDoneRecvTrailingMetadataCb(void* user_data, grpc_error* error);
67 
68   static void OnDoneSendInitialMetadataCb(void* user_data, grpc_error* error);
69 
70   static void OnDoneRecvMessageCb(void* user_data, grpc_error* error);
71 
72  private:
73   CensusContext context_;
74   // Metadata elements for tracing and census stats data.
75   grpc_linked_mdelem stats_bin_;
76   grpc_linked_mdelem tracing_bin_;
77   // Client method.
78   absl::string_view method_;
79   std::string qualified_method_;
80   grpc_slice path_;
81   // The recv trailing metadata callbacks.
82   grpc_metadata_batch* recv_trailing_metadata_;
83   grpc_closure* initial_on_done_recv_trailing_metadata_;
84   grpc_closure on_done_recv_trailing_metadata_;
85   // recv message
86   grpc_closure* initial_on_done_recv_message_;
87   grpc_closure on_done_recv_message_;
88   // Start time (for measuring latency).
89   absl::Time start_time_;
90   // Server elapsed time in nanoseconds.
91   uint64_t elapsed_time_;
92   // The received message--may be null.
93   grpc_core::OrphanablePtr<grpc_core::ByteStream>* recv_message_;
94   // Number of messages in this RPC.
95   uint64_t recv_message_count_;
96   uint64_t sent_message_count_;
97   // Buffer needed for grpc_slice to reference when adding trace context
98   // metatdata to outgoing message.
99   char tracing_buf_[kMaxTraceContextLen];
100 };
101 
102 }  // namespace grpc
103 
104 #endif /* GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_CLIENT_FILTER_H */
105