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