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_SERVER_FILTER_H
20 #define GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_SERVER_FILTER_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "absl/strings/string_view.h"
25 #include "absl/time/clock.h"
26 #include "absl/time/time.h"
27 
28 #include <grpc/grpc_security.h>
29 
30 #include "src/cpp/ext/filters/census/channel_filter.h"
31 #include "src/cpp/ext/filters/census/context.h"
32 
33 namespace grpc {
34 
35 // A CallData class will be created for every grpc call within a channel. It is
36 // used to store data and methods specific to that call. CensusServerCallData is
37 // thread-compatible, however typically only 1 thread should be interacting with
38 // a call at a time.
39 class CensusServerCallData : public CallData {
40  public:
41   // Maximum size of server stats that are sent on the wire.
42   static constexpr uint32_t kMaxServerStatsLen = 16;
43 
CensusServerCallData()44   CensusServerCallData()
45       : gc_(nullptr),
46         auth_context_(nullptr),
47         recv_initial_metadata_(nullptr),
48         initial_on_done_recv_initial_metadata_(nullptr),
49         initial_on_done_recv_message_(nullptr),
50         recv_message_(nullptr),
51         recv_message_count_(0),
52         sent_message_count_(0) {
53     memset(&census_bin_, 0, sizeof(grpc_linked_mdelem));
54     memset(&path_, 0, sizeof(grpc_slice));
55     memset(&on_done_recv_initial_metadata_, 0, sizeof(grpc_closure));
56     memset(&on_done_recv_message_, 0, sizeof(grpc_closure));
57   }
58 
59   grpc_error_handle Init(grpc_call_element* elem,
60                          const grpc_call_element_args* args) override;
61 
62   void Destroy(grpc_call_element* elem, const grpc_call_final_info* final_info,
63                grpc_closure* then_call_closure) override;
64 
65   void StartTransportStreamOpBatch(grpc_call_element* elem,
66                                    TransportStreamOpBatch* op) override;
67 
68   static void OnDoneRecvInitialMetadataCb(void* user_data,
69                                           grpc_error_handle error);
70 
71   static void OnDoneRecvMessageCb(void* user_data, grpc_error_handle error);
72 
73  private:
74   CensusContext context_;
75   // server method
76   absl::string_view method_;
77   std::string qualified_method_;
78   grpc_slice path_;
79   // Pointer to the grpc_call element
80   grpc_call* gc_;
81   // Authorization context for the call.
82   grpc_auth_context* auth_context_;
83   // Metadata element for census stats.
84   grpc_linked_mdelem census_bin_;
85   // recv callback
86   grpc_metadata_batch* recv_initial_metadata_;
87   grpc_closure* initial_on_done_recv_initial_metadata_;
88   grpc_closure on_done_recv_initial_metadata_;
89   // recv message
90   grpc_closure* initial_on_done_recv_message_;
91   grpc_closure on_done_recv_message_;
92   absl::Time start_time_;
93   absl::Duration elapsed_time_;
94   grpc_core::OrphanablePtr<grpc_core::ByteStream>* recv_message_;
95   uint64_t recv_message_count_;
96   uint64_t sent_message_count_;
97   // Buffer needed for grpc_slice to reference it when adding metatdata to
98   // response.
99   char stats_buf_[kMaxServerStatsLen];
100 };
101 
102 }  // namespace grpc
103 
104 #endif /* GRPC_INTERNAL_CPP_EXT_FILTERS_CENSUS_SERVER_FILTER_H */
105