Implementation:Tensorflow Serving stream logger h
| Knowledge Sources | |
|---|---|
| Domains | Model Serving, Request Logging |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
StreamLogger is an abstract template class for logging a stream of requests and responses, with support for multiple log callback sinks.
Description
StreamLogger<Request, Response> provides the interface for logging streaming (multi-turn) request/response interactions. The class is templated on the Request and Response types, which must be protobuf message types (enforced via static_assert). Its lifetime is tied to the lifetime of a stream.
The class accumulates requests and responses through LogStreamRequest() and LogStreamResponse() (implemented by subclasses). Multiple log callbacks can be registered via AddLogCallback(), each associated with a LogMetadata and a LogMessageFn callback function.
When LogMessage() is called (typically at the end of a stream), it:
- Iterates through all registered callbacks.
- For each callback, calls the subclass-implemented
CreateLogMessage()to produce the log protobuf. - Invokes the callback's
LogMessageFnwith the created message. - Aggregates errors, returning the first failure while continuing to attempt all callbacks.
After LogMessage() returns, any subsequent calls to other methods result in undefined behavior.
The class is not thread-safe; callers must ensure sequential access.
Usage
Subclass StreamLogger for each streaming API type (e.g., streaming predict). The ServerRequestLogger creates StreamLogger instances and registers log callbacks from each applicable RequestLogger.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File: tensorflow_serving/core/stream_logger.h
- Lines: 1-111
Signature
template <typename Request, typename Response>
class StreamLogger {
public:
StreamLogger();
virtual ~StreamLogger() = default;
virtual void LogStreamRequest(Request request) = 0;
virtual void LogStreamResponse(Response response) = 0;
using LogMessageFn = std::function<absl::Status(const google::protobuf::Message&)>;
void AddLogCallback(const LogMetadata& log_metadata,
LogMessageFn log_message_fn);
absl::Status LogMessage();
private:
virtual absl::Status CreateLogMessage(
const LogMetadata& log_metadata,
std::unique_ptr<google::protobuf::Message>* log) = 0;
};
Import
#include "tensorflow_serving/core/stream_logger.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| request | Request | Yes | A streaming request message to accumulate |
| response | Response | Yes | A streaming response message to accumulate |
| log_metadata | const LogMetadata& | Yes | Metadata for the log callback registration |
| log_message_fn | LogMessageFn | Yes | Callback function invoked with the created log message |
Outputs
| Name | Type | Description |
|---|---|---|
| LogMessage() | absl::Status | OK if all callbacks succeeded; first error from any failed callback |
Usage Examples
Using StreamLogger for a Streaming API
#include "tensorflow_serving/core/stream_logger.h"
using namespace tensorflow::serving;
// Subclass for a specific streaming API
class MyStreamLogger : public StreamLogger<MyRequest, MyResponse> {
public:
void LogStreamRequest(MyRequest request) override {
requests_.push_back(std::move(request));
}
void LogStreamResponse(MyResponse response) override {
responses_.push_back(std::move(response));
}
private:
absl::Status CreateLogMessage(
const LogMetadata& log_metadata,
std::unique_ptr<google::protobuf::Message>* log) override {
// Create aggregated log message from accumulated requests/responses
auto msg = std::make_unique<MyLogProto>();
// ... populate msg ...
*log = std::move(msg);
return absl::OkStatus();
}
std::vector<MyRequest> requests_;
std::vector<MyResponse> responses_;
};