1
#pragma once
2

            
3
#include "envoy/event/dispatcher.h"
4
#include "envoy/event/timer.h"
5

            
6
#include "absl/container/flat_hash_set.h"
7
#include "datadog/event_scheduler.h"
8
#include "nlohmann/json.hpp"
9

            
10
namespace Envoy {
11
namespace Extensions {
12
namespace Tracers {
13
namespace Datadog {
14

            
15
/**
16
 * Registry of recurring events (timers). This class implements dd-trace-cpp's
17
 * \c datadog::tracing::EventScheduler interface in terms of an
18
 * \c Event::Dispatcher, which is what is used by Envoy. An instance of this class
19
 * is passed into dd-trace-cpp when tracing is configured, allowing dd-trace-cpp
20
 * to periodically send batches of traces to the Datadog Agent.
21
 */
22
class EventScheduler : public datadog::tracing::EventScheduler {
23
public:
24
  explicit EventScheduler(Event::Dispatcher& dispatcher);
25

            
26
  /**
27
   * Repeatedly execute the specified \p callback with approximately \p interval
28
   * between each invocation, starting after an initial \p interval. Return a
29
   * function that cancels future invocations. If the returned function is
30
   * invoked after this \c EventScheduler is destroyed, the behavior is
31
   * undefined.
32
   * @param interval how often the event will occur
33
   * @param callback the function invoked when the event occurs
34
   * @return a zero-parameter function that cancels the recurring event
35
   */
36
  Cancel schedule_recurring_event(std::chrono::steady_clock::duration interval,
37
                                  std::function<void()> callback) override;
38

            
39
  // Implementation of the required config() method from datadog::tracing::EventScheduler
40
  std::string config() const override;
41

            
42
  // Provides JSON configuration for debug logging.
43
  const nlohmann::json& config_json() const;
44

            
45
private:
46
  Event::Dispatcher& dispatcher_;
47
  absl::flat_hash_set<Event::TimerPtr> timers_;
48
};
49

            
50
} // namespace Datadog
51
} // namespace Tracers
52
} // namespace Extensions
53
} // namespace Envoy