Implementation:Tensorflow Serving request logger h
| Knowledge Sources | |
|---|---|
| Domains | Model Serving, Request Logging |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
RequestLogger is an abstract class that logs sampled requests and responses hitting a server, delegating storage to a LogCollector.
Description
The RequestLogger class provides the core abstraction for request/response logging in TensorFlow Serving. It combines sampling, log message creation, and log collection into a unified interface. All subclasses must be created as shared_ptr instances (the class inherits from std::enable_shared_from_this).
The logger performs uniform random sampling based on the configured sampling rate from LoggingConfig. When a request is selected for logging, the logger calls the subclass-defined CreateLogMessage() to produce a protobuf log message, which is then forwarded to the LogCollector.
For streaming requests, MaybeStartLoggingStream() performs the sampling decision upfront and, if selected, registers a callback on the provided StreamLogger. The callback uses a weak_ptr to the RequestLogger, ensuring that if the logger is destroyed (e.g., due to config update) during an active stream, the callback gracefully becomes a no-op.
Subclasses must implement:
CreateLogMessage()- Produces the specific protobuf log message.FillLogMetadata()- Enriches the LogMetadata with additional information.
Usage
Subclass RequestLogger to create protocol-specific loggers (e.g., for Predict, Classify, Regress APIs). The subclass is registered with ServerRequestLogger which manages per-model logging configuration.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File: tensorflow_serving/core/request_logger.h
- Lines: 1-130
Signature
class RequestLogger : public std::enable_shared_from_this<RequestLogger> {
public:
RequestLogger(const LoggingConfig& logging_config,
const std::vector<string>& saved_model_tags,
std::unique_ptr<LogCollector> log_collector);
virtual ~RequestLogger() = default;
Status Log(const google::protobuf::Message& request,
const google::protobuf::Message& response,
const LogMetadata& log_metadata);
template <typename Request, typename Response>
void MaybeStartLoggingStream(
const LogMetadata& log_metadata,
GetStreamLoggerFn<Request, Response> get_stream_logger_fn);
const LoggingConfig& logging_config() const;
private:
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;
virtual LogMetadata FillLogMetadata(const LogMetadata& lm_in) = 0;
};
Import
#include "tensorflow_serving/core/request_logger.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| logging_config | const LoggingConfig& | Yes | Configuration with sampling rate and log collector settings |
| saved_model_tags | const std::vector<string>& | Yes | Tags identifying the SavedModel being served |
| log_collector | std::unique_ptr<LogCollector> | Yes | The backend log collector for persisting log messages |
| request | const google::protobuf::Message& | Yes | The incoming request protobuf to log |
| response | const google::protobuf::Message& | Yes | The outgoing response protobuf to log |
| log_metadata | const LogMetadata& | Yes | Metadata about the request (model spec, etc.) |
Outputs
| Name | Type | Description |
|---|---|---|
| Log() | Status | OK if logged successfully or not sampled; error on collection failure |
| MaybeStartLoggingStream() | void | Registers a log callback on the StreamLogger if the stream is sampled for logging |
Usage Examples
Logging a Unary Request
#include "tensorflow_serving/core/request_logger.h"
using namespace tensorflow::serving;
// Assume logger is a shared_ptr<RequestLogger> subclass
LogMetadata log_metadata;
log_metadata.mutable_model_spec()->set_name("my_model");
Status status = logger->Log(predict_request, predict_response, log_metadata);
if (!status.ok()) {
LOG(WARNING) << "Failed to log request: " << status;
}