1
#pragma once
2

            
3
#include <optional>
4

            
5
#include "envoy/common/time.h"
6
#include "envoy/tracing/trace_driver.h"
7

            
8
#include "datadog/span.h"
9

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

            
15
/**
16
 * Tracing::Span implementation for use in Datadog tracing. This class contains
17
 * an optional<datadog::tracing::Span> and forwards its member functions to the
18
 * corresponding member functions of datadog::tracing::Span.
19
 *
20
 * datadog::tracing::Span is the span type used in Datadog's core tracing
21
 * library, dd-trace-cpp. It's wrapped in an optional<> because the lifetime
22
 * of a datadog::tracing::Span is tied to the scope of the object itself,
23
 * whereas the Tracing::Span implemented here has a finishSpan() member
24
 * function that allows the span's lifetime to end without destroying the
25
 * object.
26
 *
27
 * For the same reason, this class has two states: one when the
28
 * optional<datadog::tracing::Span> is not empty and member functions are
29
 * forwarded to it, and another state when the optional<datadog::tracing::Span>
30
 * is empty and member functions have no effect.
31
 */
32
class Span : public Tracing::Span {
33
public:
34
  explicit Span(datadog::tracing::Span&& span, bool use_local_decision = false);
35

            
36
  const datadog::tracing::Optional<datadog::tracing::Span>& impl() const;
37

            
38
  // Envoy::Tracing::Span
39
  void setOperation(absl::string_view operation) override;
40
  void setTag(absl::string_view name, absl::string_view value) override;
41
  void log(SystemTime, const std::string&) override;
42
  void finishSpan() override;
43
  void injectContext(Tracing::TraceContext& trace_context,
44
                     const Tracing::UpstreamContext& upstream) override;
45
  Tracing::SpanPtr spawnChild(const Tracing::Config& config, const std::string& name,
46
                              SystemTime start_time) override;
47
  void setSampled(bool sampled) override;
48
5
  bool useLocalDecision() const override { return use_local_decision_; }
49
  std::string getBaggage(absl::string_view key) override;
50
  void setBaggage(absl::string_view key, absl::string_view value) override;
51
  std::string getTraceId() const override;
52
  std::string getSpanId() const override;
53

            
54
private:
55
  datadog::tracing::Optional<datadog::tracing::Span> span_;
56
  const bool use_local_decision_{false};
57
};
58

            
59
} // namespace Datadog
60
} // namespace Tracers
61
} // namespace Extensions
62
} // namespace Envoy