@@ -15,6 +15,28 @@ USERVER_NAMESPACE_BEGIN
1515
1616namespace {
1717
18+ constexpr std::string_view kBaseTskvPattern = R"( tskv\t)"
19+ R"( timestamp=\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\t)"
20+ R"( timezone=[-+]\d{2}\d{2}\t)"
21+ R"( user_agent=.+\t)"
22+ R"( ip=[.0-9a-f:\[\]]+\:[0-9]+\t)"
23+ R"( x_real_ip=[.0-9a-f:\[\]]+\:[0-9]+\t)"
24+ R"( request=[a-zA-Z./0-9]+\t)"
25+ R"( request_time=\d+\.\d+\t)"
26+ R"( upstream_response_time=\d+\.\d+\t)"
27+ R"( grpc_status=\d+\t)"
28+ R"( grpc_status_code=[A-Z_]+)" ;
29+
30+ const utils::regex kBaseTskvRegex {std::string{kBaseTskvPattern } + R"( \n)" };
31+
32+ // Constant to check result of LogExtra middleware
33+ const utils::regex kTskvWithTagsRegex {
34+ std::string{kBaseTskvPattern } + R"( \tuser_id=12345)"
35+ R"( \tsession_id=abc-def-ghi)"
36+ R"( \trequest_id=req-98765)"
37+ R"( \tcustom_tag=test-value)"
38+ R"( \tnumeric_tag=42\n)" };
39+
1840class UnitTestService final : public sample::ugrpc::UnitTestServiceBase {
1941public:
2042 SayHelloResult SayHello (CallContext& /* context*/ , sample::ugrpc::GreetingRequest&& request) override {
@@ -48,9 +70,51 @@ class ServiceWithAccessLogFixture : public ugrpc::tests::ServiceFixtureBase {
4870 GrpcService service_{};
4971};
5072
73+ // Test middleware that adds custom tags to LogExtra
74+ class TagsMiddleware final : public ugrpc::server::MiddlewareBase {
75+ public:
76+ void OnCallStart (ugrpc::server::MiddlewareCallContext& context) const override {
77+ auto * log_extra = context.GetStorageContext ().GetOptional (ugrpc::server::middlewares::access_log::kLogExtraTag );
78+
79+ if (log_extra) {
80+ log_extra->Extend (" user_id" , " 12345" );
81+ log_extra->Extend (" session_id" , " abc-def-ghi" );
82+ log_extra->Extend (" request_id" , " req-98765" );
83+ log_extra->Extend (" custom_tag" , " test-value" );
84+ log_extra->Extend (" numeric_tag" , " 42" );
85+ }
86+ }
87+ };
88+
89+ template <typename GrpcService>
90+ class ServiceWithAccessLogTagsFixture : public ugrpc ::tests::ServiceFixtureBase {
91+ public:
92+ ServiceWithAccessLogTagsFixture () {
93+ ugrpc::server::middlewares::access_log::Settings access_log_settings;
94+ access_log_settings.access_tskv_logger = access_logger_.GetLogger ();
95+
96+ SetServerMiddlewares ({
97+ std::make_shared<ugrpc::server::middlewares::access_log::Middleware>(std::move (access_log_settings)),
98+ std::make_shared<TagsMiddleware>(),
99+ });
100+
101+ RegisterService (service_);
102+ StartServer ();
103+ }
104+
105+ ~ServiceWithAccessLogTagsFixture () override { StopServer (); }
106+
107+ utest::LogCaptureLogger& AccessLog () { return access_logger_; }
108+
109+ private:
110+ utest::LogCaptureLogger access_logger_{logging::Format::kRaw };
111+ GrpcService service_{};
112+ };
113+
51114} // namespace
52115
53116using GrpcAccessLog = ServiceWithAccessLogFixture<UnitTestService>;
117+ using GrpcAccessLogWithTags = ServiceWithAccessLogTagsFixture<UnitTestService>;
54118
55119UTEST_F (GrpcAccessLog, Test) {
56120 auto client = MakeClient<sample::ugrpc::UnitTestServiceClient>();
@@ -60,20 +124,20 @@ UTEST_F(GrpcAccessLog, Test) {
60124
61125 GetServer ().StopServing ();
62126
63- constexpr std::string_view kExpectedPattern = R"( tskv\t )"
64- R"( timestamp=\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\t )"
65- R"( timezone=[-+]\d{2}\d{2}\t )"
66- R"( user_agent=.+\t )"
67- R"( ip=[.0-9a-f:\[\]]+\:[0-9]+\t )"
68- R"( x_real_ip=[.0-9a-f:\[\]]+\:[0-9]+\t )"
69- R"( request=[a-zA-Z./0-9]+\t )"
70- R"( request_time=\d+\.\d+\t )"
71- R"( upstream_response_time=\d+\.\d+\t )"
72- R"( grpc_status=\d+\t )"
73- R"( grpc_status_code=[A-Z_]+\n )" ;
127+ const auto logs = GetSingleLog ( AccessLog (). GetAll ());
128+ EXPECT_TRUE ( utils::regex_match (logs. GetLogRaw (), kBaseTskvRegex )) << logs;
129+ }
130+
131+ UTEST_F (GrpcAccessLogWithTags, TestWithCustomTags) {
132+ auto client = MakeClient<sample::ugrpc::UnitTestServiceClient>();
133+ sample::ugrpc::GreetingRequest out;
134+ out. set_name ( " userver " );
135+ auto response = client. SayHello (out);
136+
137+ GetServer (). StopServing () ;
74138
75139 const auto logs = GetSingleLog (AccessLog ().GetAll ());
76- EXPECT_TRUE (utils::regex_match (logs.GetLogRaw (), utils::regex ( kExpectedPattern ) )) << logs;
140+ EXPECT_TRUE (utils::regex_match (logs.GetLogRaw (), kTskvWithTagsRegex )) << logs;
77141}
78142
79143USERVER_NAMESPACE_END
0 commit comments