-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gRPC Interceptors
- Loading branch information
Showing
9 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
add_library(logger | ||
logger.cpp logger.h | ||
) | ||
|
||
target_link_libraries(logger | ||
PRIVATE | ||
glaze::glaze | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "logger.h" | ||
|
||
#include <chrono> | ||
|
||
namespace logger { | ||
std::string timestamp() { | ||
const auto now = std::chrono::system_clock::now(); | ||
auto days = std::chrono::floor<std::chrono::days>(now); | ||
std::chrono::year_month_day ymd(days); | ||
std::chrono::hh_mm_ss time(std::chrono::floor<std::chrono::nanoseconds>(now - days)); | ||
|
||
char buf[std::size("2006-01-02T15:04:05.999999999Z")]; | ||
std::snprintf( | ||
std::data(buf), | ||
std::size(buf), | ||
"%d-%.2u-%.2uT%.2ld:%.2ld:%.2lld.%lldZ", | ||
static_cast<int>(ymd.year()), | ||
static_cast<unsigned>(ymd.month()), | ||
static_cast<unsigned>(ymd.day()), | ||
time.hours().count(), | ||
time.minutes().count(), | ||
time.seconds().count(), | ||
time.subseconds().count()); | ||
|
||
return buf; | ||
} | ||
} // namespace logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <string> | ||
|
||
#include <glaze/glaze.hpp> | ||
|
||
namespace logger { | ||
std::string timestamp(); | ||
|
||
template <typename... Args> void info(std::string_view source, Args &&...args) { | ||
glz::obj obj{ | ||
"@timestamp", | ||
timestamp(), | ||
"severity", | ||
"info", | ||
"source", | ||
source, | ||
args..., | ||
}; | ||
|
||
std::cout << glz::write_json(obj) << std::endl; | ||
} | ||
} // namespace logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "logger.h" | ||
|
||
#include "logger/logger.h" | ||
|
||
namespace service { | ||
namespace interceptors { | ||
Logger::Logger(grpc::experimental::ServerRpcInfo *rpc) : | ||
_rpc(rpc), _start(std::chrono::high_resolution_clock::now()) {} | ||
|
||
Logger::~Logger() { | ||
std::chrono::duration<double, std::milli> diff = | ||
std::chrono::high_resolution_clock::now() - _start; | ||
|
||
logger::info("service", "latency_ms", diff.count(), "method", _rpc->method()); | ||
} | ||
|
||
void Logger::Intercept(grpc::experimental::InterceptorBatchMethods *methods) { | ||
methods->Proceed(); | ||
} | ||
} // namespace interceptors | ||
} // namespace service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <chrono> | ||
|
||
#include <grpcpp/support/server_interceptor.h> | ||
|
||
namespace service { | ||
namespace interceptors { | ||
class Logger final : public grpc::experimental::Interceptor { | ||
public: | ||
Logger(grpc::experimental::ServerRpcInfo *rpc); | ||
~Logger(); | ||
|
||
void Intercept(grpc::experimental::InterceptorBatchMethods *methods) override; | ||
|
||
private: | ||
grpc::experimental::ServerRpcInfo *_rpc; | ||
|
||
std::chrono::high_resolution_clock::time_point _start; | ||
}; | ||
|
||
class LoggerFactory final : public grpc::experimental::ServerInterceptorFactoryInterface { | ||
public: | ||
grpc::experimental::Interceptor *CreateServerInterceptor( | ||
grpc::experimental::ServerRpcInfo *info) override { | ||
return new Logger(info); | ||
} | ||
}; | ||
} // namespace interceptors | ||
} // namespace service |