Line data Source code
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, bool split_spans_for_request) 65 : : service_name_(service_name), address_(address), reporter_(nullptr), 66 : random_generator_(random_generator), trace_id_128bit_(trace_id_128bit), 67 : shared_span_context_(shared_span_context), time_source_(time_source), 68 0 : split_spans_for_request_(split_spans_for_request) {} 69 : 70 : /** 71 : * Creates a "root" Zipkin span. 72 : * 73 : * @param config The tracing configuration 74 : * @param span_name Name of the new span. 75 : * @param start_time The time indicating the beginning of the span. 76 : * @return SpanPtr The root span. 77 : */ 78 : SpanPtr startSpan(const Tracing::Config&, const std::string& span_name, SystemTime timestamp); 79 : 80 : /** 81 : * Depending on the given context, creates either a "child" or a "shared-context" Zipkin span. 82 : * 83 : * @param config The tracing configuration 84 : * @param span_name Name of the new span. 85 : * @param start_time The time indicating the beginning of the span. 86 : * @param previous_context The context of the span preceding the one to be created. 87 : * @return SpanPtr The child span. 88 : */ 89 : SpanPtr startSpan(const Tracing::Config&, const std::string& span_name, SystemTime timestamp, 90 : const SpanContext& previous_context); 91 : 92 : /** 93 : * TracerInterface::reportSpan. 94 : * 95 : * @param span The span to be reported. 96 : */ 97 : void reportSpan(Span&& span) override; 98 : 99 : /** 100 : * Associates a Reporter object with this Tracer. 101 : * 102 : * @param The span reporter. 103 : */ 104 : void setReporter(ReporterPtr reporter); 105 : 106 : private: 107 : const std::string service_name_; 108 : Network::Address::InstanceConstSharedPtr address_; 109 : ReporterPtr reporter_; 110 : Random::RandomGenerator& random_generator_; 111 : const bool trace_id_128bit_; 112 : const bool shared_span_context_; 113 : TimeSource& time_source_; 114 : const bool split_spans_for_request_{}; 115 : }; 116 : 117 : using TracerPtr = std::unique_ptr<Tracer>; 118 : 119 : } // namespace Zipkin 120 : } // namespace Tracers 121 : } // namespace Extensions 122 : } // namespace Envoy