TensorFlow Serving C++ API Documentation
request_logger.cc
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 #include "tensorflow_serving/core/request_logger.h"
17 
18 #include <memory>
19 #include <random>
20 #include <utility>
21 #include <vector>
22 
23 #include "tensorflow/core/lib/core/errors.h"
24 #include "tensorflow/core/lib/monitoring/counter.h"
25 #include "tensorflow/core/lib/random/random.h"
26 #include "tensorflow/core/platform/status.h"
27 #include "tensorflow/core/protobuf/error_codes.pb.h"
28 #include "tensorflow_serving/apis/model.pb.h"
29 
30 namespace tensorflow {
31 namespace serving {
32 namespace {
33 
34 auto* request_log_count = monitoring::Counter<2>::New(
35  "/tensorflow/serving/request_log_count",
36  "The total number of requests logged from the model server sliced "
37  "down by model_name and status code.",
38  "model_name", "status_code");
39 }
40 
41 RequestLogger::RequestLogger(const LoggingConfig& logging_config,
42  const std::vector<string>& saved_model_tags,
43  std::unique_ptr<LogCollector> log_collector)
44  : logging_config_(logging_config),
45  saved_model_tags_(saved_model_tags),
46  log_collector_(std::move(log_collector)),
47  uniform_sampler_() {}
48 
49 Status RequestLogger::Log(const google::protobuf::Message& request,
50  const google::protobuf::Message& response,
51  const LogMetadata& log_metadata) {
52  const double sampling_rate =
53  logging_config_.sampling_config().sampling_rate();
54  LogMetadata log_metadata_with_config = log_metadata;
55  *log_metadata_with_config.mutable_sampling_config() =
56  logging_config_.sampling_config();
57  if (!saved_model_tags_.empty()) {
58  *log_metadata_with_config.mutable_saved_model_tags() = {
59  saved_model_tags_.begin(), saved_model_tags_.end()};
60  }
61  if (uniform_sampler_.Sample(sampling_rate)) {
62  const auto status = [&]() {
63  std::unique_ptr<google::protobuf::Message> log;
64  TF_RETURN_IF_ERROR(
65  CreateLogMessage(request, response, log_metadata_with_config, &log));
66  return Log(*log);
67  }();
68  request_log_count
69  ->GetCell(log_metadata.model_spec().name(),
70  error::Code_Name(static_cast<error::Code>(status.code())))
71  ->IncrementBy(1);
72  return status;
73  }
74  return OkStatus();
75 }
76 
77 Status RequestLogger::Log(const google::protobuf::Message& log) {
78  return log_collector_->CollectMessage(log);
79 }
80 
81 } // namespace serving
82 } // namespace tensorflow