| #include "log_monitoring.h" // IWYU pragma: keep, Needed for template instantiation |
| |
| #include "gmock.h" |
| #include "gunit.h" |
| #include "mock-log.h" |
| #include "absl/base/log_severity.h" |
| #include "absl/strings/string_view.h" |
| #include "absl/time/time.h" |
| #include <source_location> |
| #include "monitoring.h" |
| |
| namespace { |
| |
| using ::testing::HasSubstr; |
| using ::testing::ScopedMockLog; |
| |
| TEST(LogMonitoringTest, RequestLatencyLogsAreGenerated) { |
| ScopedMockLog log(testing::kDoNotCaptureLogsYet); |
| milotic::metrics::Latency<absl::string_view, int> latency( |
| "test_latency", {"test_str", "test_int"}, |
| {.description = "Test latency"}); |
| |
| EXPECT_CALL( |
| log, Log(base_logging::INFO, std::source_location::current().file_name(), |
| HasSubstr("test_latency(test_str=abc, test_int=100): 1s"))); |
| log.StartCapturingLogs(); |
| latency.Set(absl::Seconds(1), {"abc", 100}); |
| } |
| |
| TEST(LogMonitorTest, EventLogsAreGenerated) { |
| ScopedMockLog log(testing::kDoNotCaptureLogsYet); |
| milotic::metrics::EventCounter<int, absl::string_view> counter( |
| "counter", {"test_int", "test_str"}, {.description = "Test counter"}); |
| |
| EXPECT_CALL( |
| log, Log(base_logging::INFO, std::source_location::current().file_name(), |
| HasSubstr("counter(test_int=100, test_str=str1): 1"))); |
| EXPECT_CALL( |
| log, Log(base_logging::INFO, std::source_location::current().file_name(), |
| HasSubstr("counter(test_int=100, test_str=str1): 2"))); |
| EXPECT_CALL( |
| log, Log(base_logging::INFO, std::source_location::current().file_name(), |
| HasSubstr("counter(test_int=100, test_str=str2): 1"))); |
| |
| log.StartCapturingLogs(); |
| counter.Increment({100, "str1"}); |
| counter.Increment({100, "str1"}); |
| counter.Increment({100, "str2"}); |
| } |
| |
| TEST(LogMonitorTest, StateLogsAreGeneratedOncePerStateChange) { |
| ScopedMockLog log(testing::kDoNotCaptureLogsYet); |
| |
| milotic::metrics::State<> state("state", {}, {.description = "Test state"}); |
| |
| EXPECT_CALL( |
| log, Log(base_logging::INFO, std::source_location::current().file_name(), |
| HasSubstr("state: first"))); |
| EXPECT_CALL( |
| log, Log(base_logging::INFO, std::source_location::current().file_name(), |
| HasSubstr("state: next"))); |
| |
| log.StartCapturingLogs(); |
| state.SetState("first", {}); |
| state.SetState("first", {}); |
| state.SetState("next", {}); |
| } |
| |
| } // namespace |