Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/config/trace/v3/zipkin.pb.h" 4 : 5 : #include "source/common/protobuf/protobuf.h" 6 : #include "source/extensions/tracers/zipkin/tracer_interface.h" 7 : #include "source/extensions/tracers/zipkin/zipkin_core_types.h" 8 : 9 : #include "zipkin.pb.h" 10 : 11 : namespace Envoy { 12 : namespace Extensions { 13 : namespace Tracers { 14 : namespace Zipkin { 15 : 16 : /** 17 : * This class implements a simple buffer to store Zipkin tracing spans 18 : * prior to flushing them. 19 : */ 20 : class SpanBuffer { 21 : public: 22 : /** 23 : * Constructor that creates an empty buffer. Space needs to be allocated by invoking 24 : * the method allocateBuffer(size). 25 : * 26 : * @param version The selected Zipkin collector version. @see 27 : * api/envoy/config/trace/v3/trace.proto. 28 : * @param shared_span_context To determine whether client and server spans will share the same 29 : * span context. 30 : */ 31 : SpanBuffer(const envoy::config::trace::v3::ZipkinConfig::CollectorEndpointVersion& version, 32 : bool shared_span_context); 33 : 34 : /** 35 : * Constructor that initializes a buffer with the given size. 36 : * 37 : * @param version The selected Zipkin collector version. @see 38 : * api/envoy/config/trace/v3/trace.proto. 39 : * @param shared_span_context To determine whether client and server spans will share the same 40 : * span context. 41 : * @param size The desired buffer size. 42 : */ 43 : SpanBuffer(const envoy::config::trace::v3::ZipkinConfig::CollectorEndpointVersion& version, 44 : bool shared_span_context, uint64_t size); 45 : 46 : /** 47 : * Allocates space for an empty buffer or resizes a previously-allocated one. 48 : * 49 : * @param size The desired buffer size. 50 : */ 51 0 : void allocateBuffer(uint64_t size) { span_buffer_.reserve(size); } 52 : 53 : /** 54 : * Adds the given Zipkin span to the buffer. 55 : * 56 : * @param span The span to be added to the buffer. 57 : * 58 : * @return true if the span was successfully added, or false if the buffer was full. 59 : */ 60 : bool addSpan(Span&& span); 61 : 62 : /** 63 : * Empties the buffer. This method is supposed to be called when all buffered spans 64 : * have been sent to the Zipkin service. 65 : */ 66 0 : void clear() { span_buffer_.clear(); } 67 : 68 : /** 69 : * @return the number of spans currently buffered. 70 : */ 71 0 : uint64_t pendingSpans() { return span_buffer_.size(); } 72 : 73 : /** 74 : * Serializes std::vector<Span> span_buffer_ to std::string as payload for the reporter when the 75 : * reporter does spans flushing. This function does only serialization and does not clear 76 : * span_buffer_. 77 : * 78 : * @return std::string the contents of the buffer, a collection of serialized pending Zipkin 79 : * spans. 80 : */ 81 0 : std::string serialize() const { return serializer_->serialize(span_buffer_); } 82 : 83 : private: 84 : SerializerPtr 85 : makeSerializer(const envoy::config::trace::v3::ZipkinConfig::CollectorEndpointVersion& version, 86 : bool shared_span_context); 87 : 88 : // We use a pre-allocated vector to improve performance 89 : std::vector<Span> span_buffer_; 90 : SerializerPtr serializer_; 91 : }; 92 : 93 : using SpanBufferPtr = std::unique_ptr<SpanBuffer>; 94 : 95 : /** 96 : * JsonV2Serializer implements Zipkin::Serializer that serializes list of Zipkin spans into JSON 97 : * Zipkin v2 array. 98 : */ 99 : class JsonV2Serializer : public Serializer { 100 : public: 101 : JsonV2Serializer(bool shared_span_context); 102 : 103 : /** 104 : * Serialize list of Zipkin spans into Zipkin v2 JSON array. 105 : * @return std::string serialized pending spans as Zipkin v2 JSON array. 106 : */ 107 : std::string serialize(const std::vector<Span>& pending_spans) override; 108 : 109 : private: 110 : const std::vector<ProtobufWkt::Struct> toListOfSpans(const Span& zipkin_span, 111 : Util::Replacements& replacements) const; 112 : const ProtobufWkt::Struct toProtoEndpoint(const Endpoint& zipkin_endpoint) const; 113 : 114 : const bool shared_span_context_; 115 : }; 116 : 117 : /** 118 : * ProtobufSerializer implements Zipkin::Serializer that serializes list of Zipkin spans into 119 : * stringified (SerializeToString) protobuf message. 120 : */ 121 : class ProtobufSerializer : public Serializer { 122 : public: 123 : ProtobufSerializer(bool shared_span_context); 124 : 125 : /** 126 : * Serialize list of Zipkin spans into Zipkin v2 zipkin::proto3::ListOfSpans. 127 : * @return std::string serialized pending spans as Zipkin zipkin::proto3::ListOfSpans. 128 : */ 129 : std::string serialize(const std::vector<Span>& pending_spans) override; 130 : 131 : private: 132 : const zipkin::proto3::ListOfSpans toListOfSpans(const Span& zipkin_span) const; 133 : const zipkin::proto3::Endpoint toProtoEndpoint(const Endpoint& zipkin_endpoint) const; 134 : 135 : const bool shared_span_context_; 136 : }; 137 : 138 : } // namespace Zipkin 139 : } // namespace Tracers 140 : } // namespace Extensions 141 : } // namespace Envoy