Line data Source code
1 : #pragma once 2 : 3 : #include <map> 4 : #include <memory> 5 : #include <string> 6 : #include <vector> 7 : 8 : #include "envoy/common/optref.h" 9 : #include "envoy/config/typed_config.h" 10 : #include "envoy/server/tracer_config.h" 11 : #include "envoy/tracing/trace_context.h" 12 : 13 : #include "absl/types/optional.h" 14 : #include "opentelemetry/proto/trace/v1/trace.pb.h" 15 : 16 : namespace Envoy { 17 : namespace Extensions { 18 : namespace Tracers { 19 : namespace OpenTelemetry { 20 : 21 : class SpanContext; 22 : 23 : enum class Decision { 24 : // IsRecording will be false, the Span will not be recorded and all events and attributes will be 25 : // dropped. 26 : DROP, 27 : // IsRecording will be true, but the Sampled flag MUST NOT be set. 28 : RECORD_ONLY, 29 : // IsRecording will be true and the Sampled flag MUST be set. 30 : RECORD_AND_SAMPLE 31 : }; 32 : 33 : /** 34 : * @brief The type of the span. 35 : * see 36 : * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#spankind 37 : */ 38 : using OTelSpanKind = ::opentelemetry::proto::trace::v1::Span::SpanKind; 39 : 40 : struct SamplingResult { 41 : /// @see Decision 42 : Decision decision; 43 : // A set of span Attributes that will also be added to the Span. Can be nullptr. 44 : std::unique_ptr<const std::map<std::string, std::string>> attributes; 45 : // A Tracestate that will be associated with the Span. If the sampler 46 : // returns an empty Tracestate here, the Tracestate will be cleared, so samplers SHOULD normally 47 : // return the passed-in Tracestate if they do not intend to change it 48 : std::string tracestate; 49 : 50 0 : inline bool isRecording() const { 51 0 : return decision == Decision::RECORD_ONLY || decision == Decision::RECORD_AND_SAMPLE; 52 0 : } 53 : 54 0 : inline bool isSampled() const { return decision == Decision::RECORD_AND_SAMPLE; } 55 : }; 56 : 57 : /** 58 : * @brief The base type for all samplers 59 : * see https://opentelemetry.io/docs/specs/otel/trace/sdk/#sampler 60 : * 61 : */ 62 : class Sampler { 63 : public: 64 0 : virtual ~Sampler() = default; 65 : 66 : /** 67 : * @brief Decides if a trace should be sampled. 68 : * 69 : * @param parent_context Span context describing the parent span. The Span's SpanContext may be 70 : * invalid to indicate a root span. 71 : * @param trace_id Trace id of the Span to be created. If the parent SpanContext contains a valid 72 : * TraceId, they MUST always match. 73 : * @param name Name of the Span to be created. 74 : * @param spankind Span kind of the Span to be created. 75 : * @param trace_context TraceContext containing potential initial span attributes 76 : * @param links Collection of links that will be associated with the Span to be created. 77 : * @return SamplingResult @see SamplingResult 78 : */ 79 : virtual SamplingResult shouldSample(const absl::optional<SpanContext> parent_context, 80 : const std::string& trace_id, const std::string& name, 81 : OTelSpanKind spankind, 82 : OptRef<const Tracing::TraceContext> trace_context, 83 : const std::vector<SpanContext>& links) PURE; 84 : 85 : /** 86 : * @brief Returns a sampler description or name. 87 : * 88 : * @return The sampler name or short description with the configuration. 89 : */ 90 : virtual std::string getDescription() const PURE; 91 : }; 92 : 93 : using SamplerSharedPtr = std::shared_ptr<Sampler>; 94 : 95 : /* 96 : * A factory for creating a sampler 97 : */ 98 : class SamplerFactory : public Envoy::Config::TypedFactory { 99 : public: 100 0 : ~SamplerFactory() override = default; 101 : 102 : /** 103 : * @brief Creates a sampler 104 : * @param config The sampler protobuf config. 105 : * @param context The TracerFactoryContext. 106 : * @return SamplerSharedPtr A sampler. 107 : */ 108 : virtual SamplerSharedPtr createSampler(const Protobuf::Message& config, 109 : Server::Configuration::TracerFactoryContext& context) PURE; 110 : 111 4 : std::string category() const override { return "envoy.tracers.opentelemetry.samplers"; } 112 : }; 113 : 114 : using SamplerFactoryPtr = std::unique_ptr<SamplerFactory>; 115 : 116 : } // namespace OpenTelemetry 117 : } // namespace Tracers 118 : } // namespace Extensions 119 : } // namespace Envoy