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
2
    envoy::data::core::v3::HealthCheckFailureType failure_type) {
17
2
  createHealthCheckEvent(health_checker_type, *host, [&failure_type](auto& event) {
18
2
    event.mutable_eject_unhealthy_event()->set_failure_type(failure_type);
19
2
  });
20
2
}
21

            
22
void HealthCheckEventLoggerImpl::logUnhealthy(
23
    envoy::data::core::v3::HealthCheckerType health_checker_type,
24
    const HostDescriptionConstSharedPtr& host,
25
2
    envoy::data::core::v3::HealthCheckFailureType failure_type, bool first_check) {
26
2
  createHealthCheckEvent(health_checker_type, *host, [&first_check, &failure_type](auto& event) {
27
2
    event.mutable_health_check_failure_event()->set_failure_type(failure_type);
28
2
    event.mutable_health_check_failure_event()->set_first_check(first_check);
29
2
  });
30
2
}
31

            
32
void HealthCheckEventLoggerImpl::logAddHealthy(
33
    envoy::data::core::v3::HealthCheckerType health_checker_type,
34
2
    const HostDescriptionConstSharedPtr& host, bool first_check) {
35
2
  createHealthCheckEvent(health_checker_type, *host, [&first_check](auto& event) {
36
2
    event.mutable_add_healthy_event()->set_first_check(first_check);
37
2
  });
38
2
}
39

            
40
void HealthCheckEventLoggerImpl::logSuccessfulHealthCheck(
41
    envoy::data::core::v3::HealthCheckerType health_checker_type,
42
2
    const HostDescriptionConstSharedPtr& host) {
43
2
  createHealthCheckEvent(health_checker_type, *host,
44
2
                         [](auto& event) { event.mutable_successful_health_check_event(); });
45
2
}
46

            
47
void HealthCheckEventLoggerImpl::logDegraded(
48
    envoy::data::core::v3::HealthCheckerType health_checker_type,
49
2
    const HostDescriptionConstSharedPtr& host) {
50
2
  createHealthCheckEvent(health_checker_type, *host,
51
2
                         [](auto& event) { event.mutable_degraded_healthy_host(); });
52
2
}
53

            
54
void HealthCheckEventLoggerImpl::logNoLongerDegraded(
55
    envoy::data::core::v3::HealthCheckerType health_checker_type,
56
2
    const HostDescriptionConstSharedPtr& host) {
57
2
  createHealthCheckEvent(health_checker_type, *host,
58
2
                         [](auto& event) { event.mutable_no_longer_degraded_host(); });
59
2
}
60

            
61
void HealthCheckEventLoggerImpl::createHealthCheckEvent(
62
    envoy::data::core::v3::HealthCheckerType health_checker_type, const HostDescription& host,
63
12
    std::function<void(envoy::data::core::v3::HealthCheckEvent&)> callback) const {
64
12
  envoy::data::core::v3::HealthCheckEvent event;
65
12
  event.set_cluster_name(host.cluster().name());
66
12
  event.set_health_checker_type(health_checker_type);
67

            
68
12
  envoy::config::core::v3::Address address;
69
12
  Network::Utility::addressToProtobufAddress(*host.address(), address);
70
12
  *event.mutable_host() = std::move(address);
71
12
  if (host.metadata() != nullptr) {
72
12
    *event.mutable_metadata() = *host.metadata();
73
12
  }
74
12
  *event.mutable_locality() = host.locality();
75

            
76
12
  TimestampUtil::systemClockToTimestamp(time_source_.systemTime(), *event.mutable_timestamp());
77

            
78
12
  callback(event);
79
12
  for (const auto& event_sink : event_sinks_) {
80
6
    event_sink->log(event);
81
6
  }
82

            
83
12
#ifdef ENVOY_ENABLE_YAML
84
12
  if (file_ == nullptr) {
85
6
    return;
86
6
  }
87

            
88
  // Make sure the type enums make it into the JSON
89
6
  const auto json =
90
6
      MessageUtil::getJsonStringFromMessageOrError(event, /* pretty_print */ false,
91
6
                                                   /* always_print_primitive_fields */ true);
92
6
  file_->write(fmt::format("{}\n", json));
93

            
94
6
#endif
95
6
}
96
} // namespace Upstream
97
} // namespace Envoy