1
#pragma once
2

            
3
#include "envoy/common/random_generator.h"
4
#include "envoy/common/time.h"
5
#include "envoy/config/trace/v3/zipkin.pb.h"
6

            
7
#include "source/extensions/tracers/zipkin/span_context.h"
8
#include "source/extensions/tracers/zipkin/zipkin_core_types.h"
9

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

            
15
/**
16
 * This class implements the Zipkin tracer. It has methods to create the appropriate Zipkin span
17
 * type, i.e., root span, child span, or shared-context span.
18
 *
19
 * This class allows its users to supply a concrete Reporter class whose reportSpan method
20
 * is called by its own reportSpan method. By doing so, we have cleanly separated the logic
21
 * of dealing with finished spans from the span-creation and tracing logic.
22
 */
23
class Tracer : public TracerInterface {
24
public:
25
  /**
26
   * Constructor.
27
   *
28
   * @param service_name The name of the service where the Tracer is running. This name is
29
   * used in all annotations' endpoints of the spans created by the Tracer.
30
   * @param address Pointer to a network-address object. The IP address and port are used
31
   * in all annotations' endpoints of the spans created by the Tracer.
32
   * @param random_generator Reference to the random-number generator to be used by the Tracer.
33
   * @param trace_id_128bit Whether 128bit ids should be used.
34
   * @param shared_span_context Whether shared span id should be used.
35
   * @param timestamp_trace_ids Whether to include timestamp in first 4 bytes of trace IDs.
36
   */
37
  Tracer(const std::string& service_name, Network::Address::InstanceConstSharedPtr address,
38
         Random::RandomGenerator& random_generator, const bool trace_id_128bit,
39
         const bool shared_span_context, TimeSource& time_source,
40
         bool split_spans_for_request = false, bool timestamp_trace_ids = false)
41
55
      : service_name_(service_name), address_(address), reporter_(nullptr),
42
55
        random_generator_(random_generator), trace_id_128bit_(trace_id_128bit),
43
55
        shared_span_context_(shared_span_context), time_source_(time_source),
44
55
        split_spans_for_request_(split_spans_for_request),
45
55
        timestamp_trace_ids_(timestamp_trace_ids) {}
46

            
47
  /**
48
   * Sets the trace context option for header injection behavior.
49
   * @param trace_context_option The trace context option from ZipkinConfig.
50
   */
51
43
  void setTraceContextOption(TraceContextOption trace_context_option) {
52
43
    trace_context_option_ = trace_context_option;
53
43
  }
54

            
55
  /**
56
   * Gets the current trace context option.
57
   * @return The current trace context option.
58
   */
59
10
  TraceContextOption traceContextOption() const override { return trace_context_option_; }
60

            
61
  // TracerInterface
62
  SpanPtr startSpan(const Tracing::Config&, const std::string& span_name,
63
                    SystemTime timestamp) override;
64
  SpanPtr startSpan(const Tracing::Config&, const std::string& span_name, SystemTime timestamp,
65
                    const SpanContext& previous_context) override;
66
  void reportSpan(Span&& span) override;
67

            
68
  /**
69
   * Associates a Reporter object with this Tracer.
70
   *
71
   * @param The span reporter.
72
   */
73
  void setReporter(ReporterPtr reporter);
74

            
75
private:
76
  /**
77
   * Generates a 64-bit value with a timestamp in the first 4 bytes if enabled.
78
   * Format when enabled: [32-bit epoch seconds][32-bit random]
79
   * Otherwise: fully random 64-bit.
80
   *
81
   * @return uint64_t value with optional timestamp prefix
82
   */
83
  uint64_t generateTraceId();
84

            
85
  const std::string service_name_;
86
  Network::Address::InstanceConstSharedPtr address_;
87
  ReporterPtr reporter_;
88
  Random::RandomGenerator& random_generator_;
89
  const bool trace_id_128bit_;
90
  const bool shared_span_context_;
91
  TimeSource& time_source_;
92
  const bool split_spans_for_request_{};
93
  TraceContextOption trace_context_option_{envoy::config::trace::v3::ZipkinConfig::USE_B3};
94
  const bool timestamp_trace_ids_{};
95
};
96

            
97
using TracerPtr = std::unique_ptr<Tracer>;
98

            
99
} // namespace Zipkin
100
} // namespace Tracers
101
} // namespace Extensions
102
} // namespace Envoy