Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/extensions/tracers/zipkin/tracer.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include "envoy/common/pure.h"
4
#include "envoy/common/random_generator.h"
5
#include "envoy/common/time.h"
6
#include "envoy/tracing/tracer.h"
7
8
#include "source/extensions/tracers/zipkin/span_context.h"
9
#include "source/extensions/tracers/zipkin/tracer_interface.h"
10
#include "source/extensions/tracers/zipkin/zipkin_core_constants.h"
11
#include "source/extensions/tracers/zipkin/zipkin_core_types.h"
12
13
namespace Envoy {
14
namespace Extensions {
15
namespace Tracers {
16
namespace Zipkin {
17
18
/**
19
 * Abstract class that delegates to users of the Tracer class the responsibility
20
 * of "reporting" a Zipkin span that has ended its life cycle. "Reporting" can mean that the
21
 * span will be sent to out to Zipkin, or buffered so that it can be sent out later.
22
 */
23
class Reporter {
24
public:
25
  /**
26
   * Destructor.
27
   */
28
0
  virtual ~Reporter() = default;
29
30
  /**
31
   * Method that a concrete Reporter class must implement to handle finished spans.
32
   * For example, a span-buffer management policy could be implemented.
33
   *
34
   * @param span The span that needs action.
35
   */
36
  virtual void reportSpan(Span&& span) PURE;
37
};
38
39
using ReporterPtr = std::unique_ptr<Reporter>;
40
41
/**
42
 * This class implements the Zipkin tracer. It has methods to create the appropriate Zipkin span
43
 * type, i.e., root span, child span, or shared-context span.
44
 *
45
 * This class allows its users to supply a concrete Reporter class whose reportSpan method
46
 * is called by its own reportSpan method. By doing so, we have cleanly separated the logic
47
 * of dealing with finished spans from the span-creation and tracing logic.
48
 */
49
class Tracer : public TracerInterface {
50
public:
51
  /**
52
   * Constructor.
53
   *
54
   * @param service_name The name of the service where the Tracer is running. This name is
55
   * used in all annotations' endpoints of the spans created by the Tracer.
56
   * @param address Pointer to a network-address object. The IP address and port are used
57
   * in all annotations' endpoints of the spans created by the Tracer.
58
   * @param random_generator Reference to the random-number generator to be used by the Tracer.
59
   * @param trace_id_128bit Whether 128bit ids should be used.
60
   * @param shared_span_context Whether shared span id should be used.
61
   */
62
  Tracer(const std::string& service_name, Network::Address::InstanceConstSharedPtr address,
63
         Random::RandomGenerator& random_generator, const bool trace_id_128bit,
64
         const bool shared_span_context, TimeSource& time_source,
65
         bool split_spans_for_request = false)
66
      : service_name_(service_name), address_(address), reporter_(nullptr),
67
        random_generator_(random_generator), trace_id_128bit_(trace_id_128bit),
68
        shared_span_context_(shared_span_context), time_source_(time_source),
69
0
        split_spans_for_request_(split_spans_for_request) {}
70
71
  /**
72
   * Creates a "root" Zipkin span.
73
   *
74
   * @param config The tracing configuration
75
   * @param span_name Name of the new span.
76
   * @param start_time The time indicating the beginning of the span.
77
   * @return SpanPtr The root span.
78
   */
79
  SpanPtr startSpan(const Tracing::Config&, const std::string& span_name, SystemTime timestamp);
80
81
  /**
82
   * Depending on the given context, creates either a "child" or a "shared-context" Zipkin span.
83
   *
84
   * @param config The tracing configuration
85
   * @param span_name Name of the new span.
86
   * @param start_time The time indicating the beginning of the span.
87
   * @param previous_context The context of the span preceding the one to be created.
88
   * @return SpanPtr The child span.
89
   */
90
  SpanPtr startSpan(const Tracing::Config&, const std::string& span_name, SystemTime timestamp,
91
                    const SpanContext& previous_context);
92
93
  /**
94
   * TracerInterface::reportSpan.
95
   *
96
   * @param span The span to be reported.
97
   */
98
  void reportSpan(Span&& span) override;
99
100
  /**
101
   * Associates a Reporter object with this Tracer.
102
   *
103
   * @param The span reporter.
104
   */
105
  void setReporter(ReporterPtr reporter);
106
107
private:
108
  const std::string service_name_;
109
  Network::Address::InstanceConstSharedPtr address_;
110
  ReporterPtr reporter_;
111
  Random::RandomGenerator& random_generator_;
112
  const bool trace_id_128bit_;
113
  const bool shared_span_context_;
114
  TimeSource& time_source_;
115
  const bool split_spans_for_request_{};
116
};
117
118
using TracerPtr = std::unique_ptr<Tracer>;
119
120
} // namespace Zipkin
121
} // namespace Tracers
122
} // namespace Extensions
123
} // namespace Envoy