1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef FCP_CLIENT_OPSTATS_OPSTATS_LOGGER_H_ 17 #define FCP_CLIENT_OPSTATS_OPSTATS_LOGGER_H_ 18 19 #include <memory> 20 #include <string> 21 22 #include "fcp/client/opstats/opstats_db.h" 23 #include "fcp/client/stats.h" 24 #include "fcp/protos/federated_api.pb.h" 25 #include "fcp/protos/opstats.pb.h" 26 27 namespace fcp { 28 namespace client { 29 namespace opstats { 30 31 // Base no-op class for the OpStats logger. 32 class OpStatsLogger { 33 public: 34 OpStatsLogger() = default; 35 OpStatsLogger(bool opstats_enabled)36 explicit OpStatsLogger(bool opstats_enabled) 37 : opstats_enabled_(opstats_enabled), 38 db_(std::make_unique<OpStatsDb>()), 39 init_status_(absl::OkStatus()) {} 40 OpStatsLogger(bool opstats_enabled,absl::Status init_status)41 OpStatsLogger(bool opstats_enabled, absl::Status init_status) 42 : opstats_enabled_(opstats_enabled), 43 db_(std::make_unique<OpStatsDb>()), 44 init_status_(init_status) {} 45 46 virtual ~OpStatsLogger() = default; 47 48 // Log a checkin accepted event and the corresponding task name. AddEventAndSetTaskName(const std::string & task_name,OperationalStats::Event::EventKind event)49 virtual void AddEventAndSetTaskName( 50 const std::string& task_name, OperationalStats::Event::EventKind event) {} 51 52 // Log an event. AddEvent(OperationalStats::Event::EventKind event)53 virtual void AddEvent(OperationalStats::Event::EventKind event) {} 54 55 // Log an event and corresponding error message. AddEventWithErrorMessage(OperationalStats::Event::EventKind event,const std::string & error_message)56 virtual void AddEventWithErrorMessage( 57 OperationalStats::Event::EventKind event, 58 const std::string& error_message) {} 59 60 // Log info associated with a dataset created for a given collection. If this 61 // is called multiple times for the same collection, the example counts and 62 // sizes should be aggregated. UpdateDatasetStats(const std::string & collection_uri,int additional_example_count,int64_t additional_example_size_bytes)63 virtual void UpdateDatasetStats(const std::string& collection_uri, 64 int additional_example_count, 65 int64_t additional_example_size_bytes) {} 66 67 // Log network stats, replacing any old stats for the run. SetNetworkStats(const NetworkStats & network_stats)68 virtual void SetNetworkStats(const NetworkStats& network_stats) {} 69 70 // Log the retry window, replacing any old retry window. Ignore any retry 71 // token in the retry window message. SetRetryWindow(google::internal::federatedml::v2::RetryWindow retry_window)72 virtual void SetRetryWindow( 73 google::internal::federatedml::v2::RetryWindow retry_window) {} 74 75 // Get the underlying opstats database. GetOpStatsDb()76 virtual OpStatsDb* GetOpStatsDb() { return db_.get(); } 77 78 // Whether opstats is enabled. IsOpStatsEnabled()79 virtual bool IsOpStatsEnabled() const { return opstats_enabled_; } 80 81 // Syncs all logged events to storage. CommitToStorage()82 virtual absl::Status CommitToStorage() { return absl::OkStatus(); } 83 84 // Returns a status holding an initialization error if OpStats was enabled but 85 // failed to initialize. GetInitStatus()86 absl::Status GetInitStatus() { return init_status_; } 87 88 // Returns the task name of the currently executing task. Only returns a valid 89 // task name if called after `AddEventAndSetTaskName` is called. GetCurrentTaskName()90 virtual std::string GetCurrentTaskName() { return ""; } 91 92 private: 93 bool opstats_enabled_; 94 std::unique_ptr<OpStatsDb> db_; 95 // If there was an error initializing the OpStats logger such that the no-op 96 // impl was returned instead, this will hold the status detailing the error. 97 absl::Status init_status_; 98 }; 99 100 } // namespace opstats 101 } // namespace client 102 } // namespace fcp 103 104 #endif // FCP_CLIENT_OPSTATS_OPSTATS_LOGGER_H_ 105