forked from tensorflow/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_logger.h
More file actions
78 lines (60 loc) · 2.8 KB
/
Copy pathrequest_logger.h
File metadata and controls
78 lines (60 loc) · 2.8 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
/* 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.
==============================================================================*/
#ifndef TENSORFLOW_SERVING_CORE_REQUEST_LOGGER_H_
#define TENSORFLOW_SERVING_CORE_REQUEST_LOGGER_H_
#include <random>
#include "google/protobuf/message.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow_serving/config/logging_config.pb.h"
#include "tensorflow_serving/core/log_collector.h"
#include "tensorflow_serving/core/logging.pb.h"
namespace tensorflow {
namespace serving {
// Abstraction to log requests and responses hitting a server. The log storage
// is handled by the log-collector. We sample requests based on the config.
class RequestLogger {
public:
RequestLogger(const LoggingConfig& logging_config,
std::unique_ptr<LogCollector> log_collector);
virtual ~RequestLogger() = default;
// Writes the log for the particular request, respone and metadata, if we
// decide to sample it.
Status Log(const google::protobuf::Message& request, const google::protobuf::Message& response,
const LogMetadata& log_metadata);
const LoggingConfig& logging_config() const { return logging_config_; }
private:
// Creates the log message given the request, response and metadata.
// Implementations override it to create the particular message that they want
// to be logged.
virtual Status CreateLogMessage(const google::protobuf::Message& request,
const google::protobuf::Message& response,
const LogMetadata& log_metadata,
std::unique_ptr<google::protobuf::Message>* log) = 0;
// A sampler which samples uniformly at random.
class UniformSampler {
public:
UniformSampler() : rd_(), gen_(rd_()), dist_(0, 1) {}
// Returns true if the sampler decides to sample it with a probability
// 'rate'.
bool Sample(const double rate) { return dist_(gen_) < rate; }
private:
std::random_device rd_;
std::mt19937 gen_;
std::uniform_real_distribution<double> dist_;
};
const LoggingConfig logging_config_;
std::unique_ptr<LogCollector> log_collector_;
UniformSampler uniform_sampler_;
};
} // namespace serving
} // namespace tensorflow
#endif // TENSORFLOW_SERVING_CORE_REQUEST_LOGGER_H_