forked from tensorflow/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_request_logger.cc
More file actions
89 lines (78 loc) · 3.63 KB
/
Copy pathserver_request_logger.cc
File metadata and controls
89 lines (78 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow_serving/core/server_request_logger.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/gtl/map_util.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow_serving/apis/model.pb.h"
#include "tensorflow_serving/core/logging.pb.h"
namespace tensorflow {
namespace serving {
// static
Status ServerRequestLogger::Create(
const std::function<Status(const LoggingConfig& logging_config,
std::unique_ptr<RequestLogger>*)>&
request_logger_creator,
std::unique_ptr<ServerRequestLogger>* server_request_logger) {
server_request_logger->reset(new ServerRequestLogger(request_logger_creator));
return Status::OK();
}
ServerRequestLogger::ServerRequestLogger(
const std::function<Status(const LoggingConfig& logging_config,
std::unique_ptr<RequestLogger>*)>&
request_logger_creator)
: request_logger_map_(
std::unique_ptr<RequestLoggerMap>(new RequestLoggerMap())),
request_logger_creator_(request_logger_creator) {}
Status ServerRequestLogger::Update(
const std::map<string, LoggingConfig>& logging_config_map) {
if (!logging_config_map.empty() && !request_logger_creator_) {
return errors::InvalidArgument("No request-logger-creator provided.");
}
std::set<string> filename_prefixes;
std::unique_ptr<RequestLoggerMap> request_logger_map(new RequestLoggerMap());
for (const auto& model_and_logging_config : logging_config_map) {
auto& request_logger =
(*request_logger_map)[model_and_logging_config.first];
const string& filename_prefix =
model_and_logging_config.second.log_collector_config()
.filename_prefix();
if (!gtl::InsertIfNotPresent(&filename_prefixes, filename_prefix)) {
// Logs for each model is supposed to be separated from each other.
return errors::InvalidArgument(
"Duplicate LogCollectorConfig::filename_prefix(): ", filename_prefix);
}
TF_RETURN_IF_ERROR(request_logger_creator_(model_and_logging_config.second,
&request_logger));
}
request_logger_map_.Update(std::move(request_logger_map));
return Status::OK();
}
Status ServerRequestLogger::Log(const google::protobuf::Message& request,
const google::protobuf::Message& response,
const LogMetadata& log_metadata) {
const string& model_name = log_metadata.model_spec().name();
auto request_logger_map = request_logger_map_.get();
if (request_logger_map->empty()) {
VLOG(2) << "Request logger map is empty.";
return Status::OK();
}
auto found_it = request_logger_map->find(model_name);
if (found_it == request_logger_map->end()) {
VLOG(2) << "Cannot find request-logger for model: " << model_name;
return Status::OK();
}
auto& request_logger = found_it->second;
return request_logger->Log(request, response, log_metadata);
}
} // namespace serving
} // namespace tensorflow