Line data Source code
1 : #include "source/common/upstream/health_checker_event_logger.h" 2 : 3 : #include "envoy/config/core/v3/address.pb.h" 4 : #include "envoy/config/core/v3/health_check.pb.h" 5 : #include "envoy/data/core/v3/health_check_event.pb.h" 6 : #include "envoy/stats/scope.h" 7 : 8 : #include "source/common/network/utility.h" 9 : 10 : namespace Envoy { 11 : namespace Upstream { 12 : 13 : void HealthCheckEventLoggerImpl::logEjectUnhealthy( 14 : envoy::data::core::v3::HealthCheckerType health_checker_type, 15 : const HostDescriptionConstSharedPtr& host, 16 0 : envoy::data::core::v3::HealthCheckFailureType failure_type) { 17 0 : createHealthCheckEvent(health_checker_type, *host, [&failure_type](auto& event) { 18 0 : event.mutable_eject_unhealthy_event()->set_failure_type(failure_type); 19 0 : }); 20 0 : } 21 : 22 : void HealthCheckEventLoggerImpl::logUnhealthy( 23 : envoy::data::core::v3::HealthCheckerType health_checker_type, 24 : const HostDescriptionConstSharedPtr& host, 25 0 : envoy::data::core::v3::HealthCheckFailureType failure_type, bool first_check) { 26 0 : createHealthCheckEvent(health_checker_type, *host, [&first_check, &failure_type](auto& event) { 27 0 : event.mutable_health_check_failure_event()->set_failure_type(failure_type); 28 0 : event.mutable_health_check_failure_event()->set_first_check(first_check); 29 0 : }); 30 0 : } 31 : 32 : void HealthCheckEventLoggerImpl::logAddHealthy( 33 : envoy::data::core::v3::HealthCheckerType health_checker_type, 34 0 : const HostDescriptionConstSharedPtr& host, bool first_check) { 35 0 : createHealthCheckEvent(health_checker_type, *host, [&first_check](auto& event) { 36 0 : event.mutable_add_healthy_event()->set_first_check(first_check); 37 0 : }); 38 0 : } 39 : 40 : void HealthCheckEventLoggerImpl::logDegraded( 41 : envoy::data::core::v3::HealthCheckerType health_checker_type, 42 0 : const HostDescriptionConstSharedPtr& host) { 43 0 : createHealthCheckEvent(health_checker_type, *host, 44 0 : [](auto& event) { event.mutable_degraded_healthy_host(); }); 45 0 : } 46 : 47 : void HealthCheckEventLoggerImpl::logNoLongerDegraded( 48 : envoy::data::core::v3::HealthCheckerType health_checker_type, 49 0 : const HostDescriptionConstSharedPtr& host) { 50 0 : createHealthCheckEvent(health_checker_type, *host, 51 0 : [](auto& event) { event.mutable_no_longer_degraded_host(); }); 52 0 : } 53 : 54 : void HealthCheckEventLoggerImpl::createHealthCheckEvent( 55 : envoy::data::core::v3::HealthCheckerType health_checker_type, const HostDescription& host, 56 0 : std::function<void(envoy::data::core::v3::HealthCheckEvent&)> callback) const { 57 0 : envoy::data::core::v3::HealthCheckEvent event; 58 0 : event.set_cluster_name(host.cluster().name()); 59 0 : event.set_health_checker_type(health_checker_type); 60 : 61 0 : envoy::config::core::v3::Address address; 62 0 : Network::Utility::addressToProtobufAddress(*host.address(), address); 63 0 : *event.mutable_host() = std::move(address); 64 0 : if (host.metadata() != nullptr) { 65 0 : *event.mutable_metadata() = *host.metadata(); 66 0 : } 67 0 : *event.mutable_locality() = host.locality(); 68 : 69 0 : TimestampUtil::systemClockToTimestamp(time_source_.systemTime(), *event.mutable_timestamp()); 70 : 71 0 : callback(event); 72 0 : for (const auto& event_sink : event_sinks_) { 73 0 : event_sink->log(event); 74 0 : } 75 : 76 0 : #ifdef ENVOY_ENABLE_YAML 77 0 : if (file_ == nullptr) { 78 0 : return; 79 0 : } 80 : 81 : // Make sure the type enums make it into the JSON 82 0 : const auto json = 83 0 : MessageUtil::getJsonStringFromMessageOrError(event, /* pretty_print */ false, 84 0 : /* always_print_primitive_fields */ true); 85 0 : file_->write(fmt::format("{}\n", json)); 86 : 87 0 : #endif 88 0 : } 89 : } // namespace Upstream 90 : } // namespace Envoy