1
#pragma once
2

            
3
#include "envoy/access_log/access_log.h"
4
#include "envoy/common/callback.h"
5
#include "envoy/config/accesslog/v3/accesslog.pb.h"
6
#include "envoy/config/accesslog/v3/accesslog.pb.validate.h"
7
#include "envoy/config/core/v3/health_check.pb.h"
8
#include "envoy/data/core/v3/health_check_event.pb.h"
9
#include "envoy/event/timer.h"
10
#include "envoy/runtime/runtime.h"
11
#include "envoy/server/factory_context.h"
12
#include "envoy/server/health_checker_config.h"
13
#include "envoy/stats/scope.h"
14
#include "envoy/type/matcher/string.pb.h"
15
#include "envoy/upstream/health_check_event_sink.h"
16
#include "envoy/upstream/health_checker.h"
17

            
18
#include "source/common/access_log/access_log_impl.h"
19
#include "source/common/common/logger.h"
20
#include "source/common/config/utility.h"
21
#include "source/common/protobuf/utility.h"
22

            
23
namespace Envoy {
24
namespace Upstream {
25

            
26
class HealthCheckEventLoggerImpl : public HealthCheckEventLogger {
27
public:
28
  static absl::StatusOr<std::unique_ptr<HealthCheckEventLoggerImpl>>
29
  create(const envoy::config::core::v3::HealthCheck& health_check_config,
30
2
         Server::Configuration::HealthCheckerFactoryContext& context) {
31
2
    AccessLog::AccessLogFileSharedPtr file;
32
2
    if (!health_check_config.event_log_path().empty() /* deprecated */) {
33
1
      auto file_or_error = context.serverFactoryContext().accessLogManager().createAccessLog(
34
1
          Filesystem::FilePathAndType{Filesystem::DestinationType::File,
35
1
                                      health_check_config.event_log_path()});
36
1
      RETURN_IF_NOT_OK_REF(file_or_error.status());
37
1
      file = file_or_error.value();
38
1
    }
39
2
    return std::unique_ptr<HealthCheckEventLoggerImpl>(
40
2
        new HealthCheckEventLoggerImpl(health_check_config, std::move(file), context));
41
2
  }
42

            
43
  void logEjectUnhealthy(envoy::data::core::v3::HealthCheckerType health_checker_type,
44
                         const HostDescriptionConstSharedPtr& host,
45
                         envoy::data::core::v3::HealthCheckFailureType failure_type) override;
46
  void logAddHealthy(envoy::data::core::v3::HealthCheckerType health_checker_type,
47
                     const HostDescriptionConstSharedPtr& host, bool first_check) override;
48
  void logSuccessfulHealthCheck(envoy::data::core::v3::HealthCheckerType health_checker_type,
49
                                const HostDescriptionConstSharedPtr& host) override;
50
  void logUnhealthy(envoy::data::core::v3::HealthCheckerType health_checker_type,
51
                    const HostDescriptionConstSharedPtr& host,
52
                    envoy::data::core::v3::HealthCheckFailureType failure_type,
53
                    bool first_check) override;
54
  void logDegraded(envoy::data::core::v3::HealthCheckerType health_checker_type,
55
                   const HostDescriptionConstSharedPtr& host) override;
56
  void logNoLongerDegraded(envoy::data::core::v3::HealthCheckerType health_checker_type,
57
                           const HostDescriptionConstSharedPtr& host) override;
58

            
59
protected:
60
  HealthCheckEventLoggerImpl(const envoy::config::core::v3::HealthCheck& health_check_config,
61
                             AccessLog::AccessLogFileSharedPtr&& file,
62
                             Server::Configuration::HealthCheckerFactoryContext& context)
63
2
      : time_source_(context.serverFactoryContext().mainThreadDispatcher().timeSource()),
64
2
        file_(std::move(file)) {
65
2
    for (const auto& config : health_check_config.event_logger()) {
66
1
      auto& factory = Config::Utility::getAndCheckFactory<HealthCheckEventSinkFactory>(config);
67
1
      event_sinks_.push_back(factory.createHealthCheckEventSink(config.typed_config(), context));
68
1
    }
69
2
  }
70

            
71
private:
72
  void createHealthCheckEvent(
73
      envoy::data::core::v3::HealthCheckerType health_checker_type, const HostDescription& host,
74
      std::function<void(envoy::data::core::v3::HealthCheckEvent&)> callback) const;
75
  TimeSource& time_source_;
76
  AccessLog::AccessLogFileSharedPtr file_;
77
  std::vector<HealthCheckEventSinkPtr> event_sinks_;
78
};
79

            
80
} // namespace Upstream
81
} // namespace Envoy