• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2022 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_SRC_CORE_EXT_FILTERS_LOGGING_LOGGING_FILTER_H
20 #define GRPC_SRC_CORE_EXT_FILTERS_LOGGING_LOGGING_FILTER_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <string>
25 #include <utility>
26 
27 #include "absl/status/statusor.h"
28 #include "src/core/ext/filters/logging/logging_sink.h"
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/channel/channel_fwd.h"
31 #include "src/core/lib/channel/promise_based_filter.h"
32 #include "src/core/lib/promise/arena_promise.h"
33 #include "src/core/lib/transport/transport.h"
34 
35 namespace grpc_core {
36 
37 namespace logging_filter_detail {
38 
39 class CallData {
40  public:
41   CallData(bool is_client, const ClientMetadata& client_initial_metadata,
42            const std::string& authority);
43 
ShouldLog()44   bool ShouldLog() { return config_.ShouldLog(); }
45 
46   void LogClientHeader(bool is_client, CallTracerAnnotationInterface* tracer,
47                        const ClientMetadata& metadata);
48   void LogClientHalfClose(bool is_client,
49                           CallTracerAnnotationInterface* tracer);
50   void LogServerHeader(bool is_client, CallTracerAnnotationInterface* tracer,
51                        const ServerMetadata* metadata);
52   void LogServerTrailer(bool is_client, CallTracerAnnotationInterface* tracer,
53                         const ServerMetadata* metadata);
54   void LogClientMessage(bool is_client, CallTracerAnnotationInterface* tracer,
55                         const SliceBuffer* message);
56   void LogServerMessage(bool is_client, CallTracerAnnotationInterface* tracer,
57                         const SliceBuffer* message);
58   void LogCancel(bool is_client, CallTracerAnnotationInterface* tracer);
59 
60  private:
61   void SetCommonEntryFields(LoggingSink::Entry* entry, bool is_client,
62                             CallTracerAnnotationInterface* tracer,
63                             LoggingSink::Entry::EventType event_type);
64   absl::uint128 call_id_;
65   uint32_t sequence_id_ = 0;
66   std::string service_name_;
67   std::string method_name_;
68   std::string authority_;
69   LoggingSink::Entry::Address peer_;
70   LoggingSink::Config config_;
71 };
72 
73 }  // namespace logging_filter_detail
74 
75 class ClientLoggingFilter final
76     : public ImplementChannelFilter<ClientLoggingFilter> {
77  public:
78   static const grpc_channel_filter kFilter;
79 
TypeName()80   static absl::string_view TypeName() { return "logging"; }
81 
82   static absl::StatusOr<std::unique_ptr<ClientLoggingFilter>> Create(
83       const ChannelArgs& args, ChannelFilter::Args /*filter_args*/);
84 
ClientLoggingFilter(std::string default_authority)85   explicit ClientLoggingFilter(std::string default_authority)
86       : default_authority_(std::move(default_authority)) {}
87 
88   class Call {
89    public:
90     void OnClientInitialMetadata(ClientMetadata& md,
91                                  ClientLoggingFilter* filter);
92     void OnServerInitialMetadata(ServerMetadata& md);
93     void OnServerTrailingMetadata(ServerMetadata& md);
94     void OnClientToServerMessage(const Message& message);
95     void OnClientToServerHalfClose();
96     void OnServerToClientMessage(const Message& message);
97     static const NoInterceptor OnFinalize;
98 
99    private:
100     absl::optional<logging_filter_detail::CallData> call_data_;
101   };
102 
103  private:
104   const std::string default_authority_;
105 };
106 
107 class ServerLoggingFilter final
108     : public ImplementChannelFilter<ServerLoggingFilter> {
109  public:
110   static const grpc_channel_filter kFilter;
111 
TypeName()112   static absl::string_view TypeName() { return "logging"; }
113 
114   static absl::StatusOr<std::unique_ptr<ServerLoggingFilter>> Create(
115       const ChannelArgs& args, ChannelFilter::Args /*filter_args*/);
116 
117   class Call {
118    public:
119     void OnClientInitialMetadata(ClientMetadata& md);
120     void OnServerInitialMetadata(ServerMetadata& md);
121     void OnServerTrailingMetadata(ServerMetadata& md);
122     void OnClientToServerMessage(const Message& message);
123     void OnClientToServerHalfClose();
124     void OnServerToClientMessage(const Message& message);
125     static const NoInterceptor OnFinalize;
126 
127    private:
128     absl::optional<logging_filter_detail::CallData> call_data_;
129   };
130 };
131 
132 void RegisterLoggingFilter(LoggingSink* sink);
133 
134 }  // namespace grpc_core
135 
136 #endif  // GRPC_SRC_CORE_EXT_FILTERS_LOGGING_LOGGING_FILTER_H
137