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