3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_EXT_FILTERS_LOAD_REPORTING_SERVER_LOAD_REPORTING_FILTER_H
20 #define GRPC_CORE_EXT_FILTERS_LOAD_REPORTING_SERVER_LOAD_REPORTING_FILTER_H
22 #include <grpc/support/port_platform.h>
24 #include "src/core/lib/channel/channel_stack.h"
25 #include "src/cpp/common/channel_filter.h"
29 class ServerLoadReportingChannelData : public ChannelData {
31 grpc_error* Init(grpc_channel_element* elem,
32 grpc_channel_element_args* args) override;
35 const char* peer_identity() { return peer_identity_; }
36 size_t peer_identity_len() { return peer_identity_len_; }
39 // The peer's authenticated identity.
40 char* peer_identity_ = nullptr;
41 size_t peer_identity_len_ = 0;
44 class ServerLoadReportingCallData : public CallData {
46 grpc_error* Init(grpc_call_element* elem,
47 const grpc_call_element_args* args) override;
49 void Destroy(grpc_call_element* elem, const grpc_call_final_info* final_info,
50 grpc_closure* then_call_closure) override;
52 void StartTransportStreamOpBatch(grpc_call_element* elem,
53 TransportStreamOpBatch* op) override;
56 // From the peer_string_ in calld, extracts the client IP string (owned by
57 // caller), e.g., "01020a0b". Upon failure, set the output pointer to null and
59 void GetCensusSafeClientIpString(char** client_ip_string, size_t* size);
61 // Concatenates the client IP address and the load reporting token, then
62 // stores the result into the call data.
63 void StoreClientIpAndLrToken(const char* lr_token, size_t lr_token_len);
65 // This matches the classification of the status codes in
66 // googleapis/google/rpc/code.proto.
67 static const char* GetStatusTagForStatus(grpc_status_code status);
69 // Records the call start.
70 static void RecvInitialMetadataReady(void* arg, grpc_error* err);
72 // From the initial metadata, extracts the service_method_, target_host_, and
73 // client_ip_and_lr_token_.
74 static grpc_filtered_mdelem RecvInitialMetadataFilter(void* user_data,
77 // Records the other call metrics.
78 static grpc_filtered_mdelem SendTrailingMetadataFilter(void* user_data,
81 // The peer string (a member of the recv_initial_metadata op). Note that
82 // gpr_atm itself is a pointer type here, making "peer_string_" effectively a
84 const gpr_atm* peer_string_;
86 // The received initial metadata (a member of the recv_initial_metadata op).
87 // When it is ready, we will extract some data from it via
88 // recv_initial_metadata_ready_ closure, before the original
89 // recv_initial_metadata_ready closure.
90 grpc_metadata_batch* recv_initial_metadata_;
92 // The original recv_initial_metadata closure, which is wrapped by our own
93 // closure (recv_initial_metadata_ready_) to capture the incoming initial
95 grpc_closure* original_recv_initial_metadata_ready_;
97 // The closure that wraps the original closure. Scheduled when
98 // recv_initial_metadata_ is ready.
99 grpc_closure recv_initial_metadata_ready_;
101 // Corresponds to the :path header.
102 grpc_slice service_method_;
104 // The backend host that the client thinks it's talking to. This may be
105 // different from the actual backend in the case of, for example,
106 // load-balanced targets. We store a copy of the metadata slice in order to
109 size_t target_host_len_;
111 // The client IP address (including a length prefix) and the load reporting
113 char* client_ip_and_lr_token_;
114 size_t client_ip_and_lr_token_len_;
119 #endif /* GRPC_CORE_EXT_FILTERS_LOAD_REPORTING_SERVER_LOAD_REPORTING_FILTER_H \