xref: /aosp_15_r20/external/grpc-grpc/src/cpp/ext/gcp/observability_logging_sink.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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_CPP_EXT_GCP_OBSERVABILITY_LOGGING_SINK_H
20 #define GRPC_SRC_CPP_EXT_GCP_OBSERVABILITY_LOGGING_SINK_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stdint.h>
25 
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 #include "absl/base/thread_annotations.h"
33 #include "absl/strings/string_view.h"
34 #include "google/logging/v2/logging.grpc.pb.h"
35 
36 #include <grpc/event_engine/event_engine.h>
37 
38 #include "src/core/ext/filters/logging/logging_sink.h"
39 #include "src/core/lib/gprpp/sync.h"
40 #include "src/cpp/ext/gcp/environment_autodetect.h"
41 #include "src/cpp/ext/gcp/observability_config.h"
42 
43 namespace grpc {
44 namespace internal {
45 
46 // Interface for a logging sink that will be used by the logging filter.
47 class ObservabilityLoggingSink : public grpc_core::LoggingSink {
48  public:
49   ObservabilityLoggingSink(GcpObservabilityConfig::CloudLogging logging_config,
50                            std::string project_id,
51                            std::map<std::string, std::string> labels);
52 
53   ~ObservabilityLoggingSink() override = default;
54 
55   LoggingSink::Config FindMatch(bool is_client, absl::string_view service,
56                                 absl::string_view method) override;
57 
58   void LogEntry(Entry entry) override;
59 
60   // Triggers a final flush of all the currently buffered logging entries and
61   // closes the sink preventing any more entries to be logged.
62   void FlushAndClose();
63 
64  private:
65   struct Configuration {
66     explicit Configuration(
67         const GcpObservabilityConfig::CloudLogging::RpcEventConfiguration&
68             rpc_event_config);
69     struct ParsedMethod {
70       std::string service;
71       std::string method;
72     };
73     std::vector<ParsedMethod> parsed_methods;
74     bool exclude = false;
75     uint32_t max_metadata_bytes = 0;
76     uint32_t max_message_bytes = 0;
77   };
78 
79   void RegisterEnvironmentResource(
80       const EnvironmentAutoDetect::ResourceType* resource);
81 
82   // Flushes the currently stored entries. \a timed_flush denotes whether this
83   // Flush was triggered from a timer.
84   void Flush();
85   void FlushEntriesHelper(
86       google::logging::v2::LoggingServiceV2::StubInterface* stub,
87       std::vector<Entry> entries,
88       const EnvironmentAutoDetect::ResourceType* resource);
89 
90   void MaybeTriggerFlush();
91   void MaybeTriggerFlushLocked() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
92 
93   std::vector<Configuration> client_configs_;
94   std::vector<Configuration> server_configs_;
95   const std::string project_id_;
96   std::string authority_;
97   const std::vector<std::pair<std::string, std::string>> labels_;
98   grpc_core::Mutex mu_;
99   bool registered_env_fetch_notification_ = false;
100   std::shared_ptr<grpc_event_engine::experimental::EventEngine> ABSL_GUARDED_BY(
101       mu_) event_engine_;
102   std::unique_ptr<google::logging::v2::LoggingServiceV2::StubInterface> stub_
103       ABSL_GUARDED_BY(mu_);
104   std::vector<Entry> entries_ ABSL_GUARDED_BY(mu_);
105   uint64_t entries_memory_footprint_ ABSL_GUARDED_BY(mu_) = 0;
106   const EnvironmentAutoDetect::ResourceType* resource_ ABSL_GUARDED_BY(mu_) =
107       nullptr;
108   bool flush_triggered_ ABSL_GUARDED_BY(mu_) = false;
109   bool flush_in_progress_ ABSL_GUARDED_BY(mu_) = false;
110   bool flush_timer_in_progress_ ABSL_GUARDED_BY(mu_) = false;
111   bool sink_closed_ ABSL_GUARDED_BY(mu_) = false;
112   grpc_core::CondVar sink_flushed_after_close_;
113 };
114 
115 // Exposed for just for testing purposes
116 void EntryToJsonStructProto(grpc_core::LoggingSink::Entry entry,
117                             ::google::protobuf::Struct* json_payload);
118 
119 }  // namespace internal
120 }  // namespace grpc
121 
122 #endif  // GRPC_SRC_CPP_EXT_GCP_OBSERVABILITY_LOGGING_SINK_H
123