TensorFlow Serving C++ API Documentation
log_collector.h
1 /* Copyright 2016 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_SERVING_CORE_LOG_COLLECTOR_H_
17 #define TENSORFLOW_SERVING_CORE_LOG_COLLECTOR_H_
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 
23 #include "google/protobuf/message.h"
24 #include "tensorflow/core/lib/core/status.h"
25 #include "tensorflow_serving/config/log_collector_config.pb.h"
26 
27 namespace tensorflow {
28 namespace serving {
29 
30 // LogCollector defines an abstract interface to use for collecting logs.
31 //
32 // Each LogCollector implementation is registered along with a 'type', and if a
33 // LogCollector is created using this API, we create the LogCollector
34 // corresponding to the 'type' specified.
35 class LogCollector {
36  public:
37  virtual ~LogCollector() = default;
38 
39  // Creates a log-collector for a given 'log_collector_config' and 'id'. The
40  // factory registered for the type, mentioned in the config, can then be used
41  // to create the log-collector. The 'id' argument helps in disambiguating logs
42  // from replicated servers (processes), so it could be a combination of
43  // task-id and replica-id or process-id and timestamp, etc.
44  static Status Create(const LogCollectorConfig& log_collector_config,
45  const uint32 id,
46  std::unique_ptr<LogCollector>* log_collector);
47 
48  using Factory = std::function<decltype(Create)>;
49  // Registers a factory for creating log-collectors for a particular 'type'.
50  // Returns an error status if a factory is already registered for the
51  // particular 'type'.
52  static Status RegisterFactory(const string& type, const Factory& factory);
53 
54  // Collects the log as a protocol buffer.
55  virtual Status CollectMessage(const google::protobuf::Message& message) = 0;
56 
57  // Flushes buffered data so that the data can survive an application crash
58  // (but not an OS crash).
59  virtual Status Flush() = 0;
60 
61  protected:
62  LogCollector() = default;
63 };
64 
65 namespace register_log_collector {
66 
68  RegisterFactory(const string& type, const LogCollector::Factory& factory) {
69  // This check happens during global object construction time, even before
70  // control reaches main(), so we are ok with the crash.
71  TF_CHECK_OK(LogCollector::RegisterFactory(type, factory)); // Crash ok.
72  }
73 };
74 
75 } // namespace register_log_collector
76 
77 } // namespace serving
78 } // namespace tensorflow
79 
80 #define REGISTER_LOG_COLLECTOR_UNIQ_HELPER(ctr, type, factory) \
81  REGISTER_LOG_COLLECTOR_UNIQ(ctr, type, factory)
82 #define REGISTER_LOG_COLLECTOR_UNIQ(ctr, type, factory) \
83  static ::tensorflow::serving::register_log_collector::RegisterFactory \
84  register_lgc##ctr TF_ATTRIBUTE_UNUSED = \
85  ::tensorflow::serving::register_log_collector::RegisterFactory( \
86  type, factory)
87 
88 // Registers a LogCollector factory implementation for a type.
89 #define REGISTER_LOG_COLLECTOR(type, factory) \
90  REGISTER_LOG_COLLECTOR_UNIQ_HELPER(__COUNTER__, type, factory)
91 
92 #endif // TENSORFLOW_SERVING_CORE_LOG_COLLECTOR_H_