Home | History | Annotate | Download | only in load_reporting
      1 /*
      2  *
      3  * Copyright 2016 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_CORE_EXT_FILTERS_LOAD_REPORTING_SERVER_LOAD_REPORTING_FILTER_H
     20 #define GRPC_CORE_EXT_FILTERS_LOAD_REPORTING_SERVER_LOAD_REPORTING_FILTER_H
     21 
     22 #include <grpc/support/port_platform.h>
     23 
     24 #include "src/core/lib/channel/channel_stack.h"
     25 #include "src/cpp/common/channel_filter.h"
     26 
     27 namespace grpc {
     28 
     29 class ServerLoadReportingChannelData : public ChannelData {
     30  public:
     31   grpc_error* Init(grpc_channel_element* elem,
     32                    grpc_channel_element_args* args) override;
     33 
     34   // Getters.
     35   const char* peer_identity() { return peer_identity_; }
     36   size_t peer_identity_len() { return peer_identity_len_; }
     37 
     38  private:
     39   // The peer's authenticated identity.
     40   char* peer_identity_ = nullptr;
     41   size_t peer_identity_len_ = 0;
     42 };
     43 
     44 class ServerLoadReportingCallData : public CallData {
     45  public:
     46   grpc_error* Init(grpc_call_element* elem,
     47                    const grpc_call_element_args* args) override;
     48 
     49   void Destroy(grpc_call_element* elem, const grpc_call_final_info* final_info,
     50                grpc_closure* then_call_closure) override;
     51 
     52   void StartTransportStreamOpBatch(grpc_call_element* elem,
     53                                    TransportStreamOpBatch* op) override;
     54 
     55  private:
     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
     58   // size to zero.
     59   void GetCensusSafeClientIpString(char** client_ip_string, size_t* size);
     60 
     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);
     64 
     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);
     68 
     69   // Records the call start.
     70   static void RecvInitialMetadataReady(void* arg, grpc_error* err);
     71 
     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,
     75                                                         grpc_mdelem md);
     76 
     77   // Records the other call metrics.
     78   static grpc_filtered_mdelem SendTrailingMetadataFilter(void* user_data,
     79                                                          grpc_mdelem md);
     80 
     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
     83   // double pointer.
     84   const gpr_atm* peer_string_;
     85 
     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_;
     91 
     92   // The original recv_initial_metadata closure, which is wrapped by our own
     93   // closure (recv_initial_metadata_ready_) to capture the incoming initial
     94   // metadata.
     95   grpc_closure* original_recv_initial_metadata_ready_;
     96 
     97   // The closure that wraps the original closure. Scheduled when
     98   // recv_initial_metadata_ is ready.
     99   grpc_closure recv_initial_metadata_ready_;
    100 
    101   // Corresponds to the :path header.
    102   grpc_slice service_method_;
    103 
    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
    107   // lowercase it. */
    108   char* target_host_;
    109   size_t target_host_len_;
    110 
    111   // The client IP address (including a length prefix) and the load reporting
    112   // token.
    113   char* client_ip_and_lr_token_;
    114   size_t client_ip_and_lr_token_len_;
    115 };
    116 
    117 }  // namespace grpc
    118 
    119 #endif /* GRPC_CORE_EXT_FILTERS_LOAD_REPORTING_SERVER_LOAD_REPORTING_FILTER_H \
    120         */
    121