From 5e55f192c52c8901f17ac41b8480ca3c372d5aaf Mon Sep 17 00:00:00 2001 From: Dhi Aurrahman Date: Sun, 12 Aug 2018 22:36:00 +0700 Subject: [PATCH] Add timestamp to HealthCheckEvent definition This PR adds timestamp field to the HealthCheckEvent message to allow having it rendered inside the JSON serialized log of a health check event. Signed-off-by: Dhi Aurrahman --- .../data/core/v2alpha/health_check_event.proto | 4 ++++ docs/root/intro/version_history.rst | 2 ++ source/common/upstream/health_checker_base_impl.cc | 4 ++++ source/common/upstream/health_checker_base_impl.h | 6 ++++-- source/common/upstream/health_checker_impl.cc | 4 ++-- test/common/upstream/health_checker_impl_test.cc | 14 ++++++++++---- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/api/envoy/data/core/v2alpha/health_check_event.proto b/api/envoy/data/core/v2alpha/health_check_event.proto index 5c9e28f6846dd..392dbcdc5a692 100644 --- a/api/envoy/data/core/v2alpha/health_check_event.proto +++ b/api/envoy/data/core/v2alpha/health_check_event.proto @@ -6,6 +6,7 @@ import "envoy/api/v2/core/address.proto"; import "envoy/api/v2/core/base.proto"; import "google/protobuf/duration.proto"; +import "google/protobuf/timestamp.proto"; import "google/protobuf/wrappers.proto"; import "validate/validate.proto"; @@ -30,6 +31,9 @@ message HealthCheckEvent { // Host addition. HealthCheckAddHealthy add_healthy_event = 5; } + + // Timestamp for event. + google.protobuf.Timestamp timestamp = 6 [(gogoproto.stdtime) = true]; } enum HealthCheckFailureType { diff --git a/docs/root/intro/version_history.rst b/docs/root/intro/version_history.rst index 4ac740f702bc5..1081f31620e3c 100644 --- a/docs/root/intro/version_history.rst +++ b/docs/root/intro/version_history.rst @@ -17,6 +17,8 @@ Version history * health check: added support for :ref:`custom health check `. * health check: added support for :ref:`specifying jitter as a percentage `. * health_check: added support for :ref:`health check event logging `. +* health_check: added :ref:`timestamp ` + to the :ref:`health check event ` definition. * health_check: added support for specifying :ref:`custom request headers ` to HTTP health checker requests. * http: added support for a per-stream idle timeout. This applies at both :ref:`connection manager diff --git a/source/common/upstream/health_checker_base_impl.cc b/source/common/upstream/health_checker_base_impl.cc index 4d779e328bc7b..81cc69291d8d8 100644 --- a/source/common/upstream/health_checker_base_impl.cc +++ b/source/common/upstream/health_checker_base_impl.cc @@ -286,6 +286,8 @@ void HealthCheckEventLoggerImpl::logEjectUnhealthy( *event.mutable_host() = std::move(address); event.set_cluster_name(host->cluster().name()); event.mutable_eject_unhealthy_event()->set_failure_type(failure_type); + TimestampUtil::systemClockToTimestamp(system_time_source_.currentTime(), + *event.mutable_timestamp()); // Make sure the type enums make it into the JSON const auto json = MessageUtil::getJsonStringFromMessage(event, /* pretty_print */ false, /* always_print_primitive_fields */ true); @@ -302,6 +304,8 @@ void HealthCheckEventLoggerImpl::logAddHealthy( *event.mutable_host() = std::move(address); event.set_cluster_name(host->cluster().name()); event.mutable_add_healthy_event()->set_first_check(first_check); + TimestampUtil::systemClockToTimestamp(system_time_source_.currentTime(), + *event.mutable_timestamp()); // Make sure the type enums make it into the JSON const auto json = MessageUtil::getJsonStringFromMessage(event, /* pretty_print */ false, /* always_print_primitive_fields */ true); diff --git a/source/common/upstream/health_checker_base_impl.h b/source/common/upstream/health_checker_base_impl.h index df3565ef6950b..933136a44757c 100644 --- a/source/common/upstream/health_checker_base_impl.h +++ b/source/common/upstream/health_checker_base_impl.h @@ -132,8 +132,9 @@ class HealthCheckerImplBase : public HealthChecker, class HealthCheckEventLoggerImpl : public HealthCheckEventLogger { public: - HealthCheckEventLoggerImpl(AccessLog::AccessLogManager& log_manager, const std::string& file_name) - : file_(log_manager.createAccessLog(file_name)) {} + HealthCheckEventLoggerImpl(AccessLog::AccessLogManager& log_manager, + SystemTimeSource& system_time_source, const std::string& file_name) + : system_time_source_(system_time_source), file_(log_manager.createAccessLog(file_name)) {} void logEjectUnhealthy(envoy::data::core::v2alpha::HealthCheckerType health_checker_type, const HostDescriptionConstSharedPtr& host, @@ -142,6 +143,7 @@ class HealthCheckEventLoggerImpl : public HealthCheckEventLogger { const HostDescriptionConstSharedPtr& host, bool first_check) override; private: + SystemTimeSource& system_time_source_; Filesystem::FileSharedPtr file_; }; diff --git a/source/common/upstream/health_checker_impl.cc b/source/common/upstream/health_checker_impl.cc index f5a39885034d3..0ca6b6794a3d5 100644 --- a/source/common/upstream/health_checker_impl.cc +++ b/source/common/upstream/health_checker_impl.cc @@ -49,8 +49,8 @@ HealthCheckerFactory::create(const envoy::api::v2::core::HealthCheck& hc_config, AccessLog::AccessLogManager& log_manager) { HealthCheckEventLoggerPtr event_logger; if (!hc_config.event_log_path().empty()) { - event_logger = - std::make_unique(log_manager, hc_config.event_log_path()); + event_logger = std::make_unique( + log_manager, ProdSystemTimeSource::instance_, hc_config.event_log_path()); } switch (hc_config.health_checker_case()) { case envoy::api::v2::core::HealthCheck::HealthCheckerCase::kHttpHealthCheck: diff --git a/test/common/upstream/health_checker_impl_test.cc b/test/common/upstream/health_checker_impl_test.cc index 3606f7a9fa2f0..111d2ac1f99b8 100644 --- a/test/common/upstream/health_checker_impl_test.cc +++ b/test/common/upstream/health_checker_impl_test.cc @@ -3164,13 +3164,19 @@ TEST(HealthCheckEventLoggerImplTest, All) { NiceMock cluster; ON_CALL(*host, cluster()).WillByDefault(ReturnRef(cluster)); - HealthCheckEventLoggerImpl event_logger(log_manager, "foo"); + NiceMock system_time_source; + EXPECT_CALL(system_time_source, currentTime()) + // This is rendered as "2009-02-13T23:31:31.234Z". + .WillRepeatedly(Return(SystemTime(std::chrono::milliseconds(1234567891234)))); + + HealthCheckEventLoggerImpl event_logger(log_manager, system_time_source, "foo"); EXPECT_CALL(*file, write(absl::string_view{ "{\"health_checker_type\":\"HTTP\",\"host\":{\"socket_address\":{" "\"protocol\":\"TCP\",\"address\":\"10.0.0.1\",\"resolver_name\":\"\"," "\"ipv4_compat\":false,\"port_value\":443}},\"cluster_name\":\"fake_" - "cluster\",\"eject_unhealthy_event\":{\"failure_type\":\"ACTIVE\"}}\n"})); + "cluster\",\"eject_unhealthy_event\":{\"failure_type\":\"ACTIVE\"}," + "\"timestamp\":\"2009-02-13T23:31:31.234Z\"}\n"})); event_logger.logEjectUnhealthy(envoy::data::core::v2alpha::HealthCheckerType::HTTP, host, envoy::data::core::v2alpha::HealthCheckFailureType::ACTIVE); @@ -3178,8 +3184,8 @@ TEST(HealthCheckEventLoggerImplTest, All) { "{\"health_checker_type\":\"HTTP\",\"host\":{\"socket_address\":{" "\"protocol\":\"TCP\",\"address\":\"10.0.0.1\",\"resolver_name\":\"\"," "\"ipv4_compat\":false,\"port_value\":443}},\"cluster_name\":\"fake_" - "cluster\",\"add_healthy_event\":{\"first_check\":false}}\n"})); - + "cluster\",\"add_healthy_event\":{\"first_check\":false},\"timestamp\":" + "\"2009-02-13T23:31:31.234Z\"}\n"})); event_logger.logAddHealthy(envoy::data::core::v2alpha::HealthCheckerType::HTTP, host, false); }