Line data Source code
1 : #pragma once 2 : 3 : #include <string> 4 : 5 : #include "envoy/local_info/local_info.h" 6 : #include "envoy/tracing/tracer.h" 7 : 8 : #include "source/common/tracing/common_values.h" 9 : #include "source/common/tracing/null_span_impl.h" 10 : 11 : namespace Envoy { 12 : namespace Tracing { 13 : 14 : /** 15 : * Protocol independent tracer utility functions. 16 : */ 17 : class TracerUtility { 18 : public: 19 : /** 20 : * Get string representation of the operation. 21 : * @param operation name to convert. 22 : * @return string representation of the operation. 23 : */ 24 : static const std::string& toString(OperationName operation_name); 25 : 26 : /** 27 : * Request might be traceable if the request ID is traceable or we do sampling tracing. 28 : * Note: there is a global switch which turns off tracing completely on server side. 29 : * 30 : * @return decision if request is traceable or not and Reason why. 31 : **/ 32 : static Decision shouldTraceRequest(const StreamInfo::StreamInfo& stream_info); 33 : 34 : /** 35 : * Finalize span and set protocol independent tags to the span. 36 : * @param span the downstream or upstream span. 37 : * @param context traceable stream context. 38 : * @param stream_info stream info. 39 : * @param config tracing configuration. 40 : * @param upstream_span true if the span is an upstream span. 41 : */ 42 : static void finalizeSpan(Span& span, const TraceContext& context, 43 : const StreamInfo::StreamInfo& stream_info, const Config& config, 44 : bool upstream_span); 45 : 46 : private: 47 : static const std::string IngressOperation; 48 : static const std::string EgressOperation; 49 : }; 50 : 51 : class EgressConfigImpl : public Config { 52 : public: 53 : // Tracing::Config 54 0 : Tracing::OperationName operationName() const override { return Tracing::OperationName::Egress; } 55 0 : const CustomTagMap* customTags() const override { return nullptr; } 56 0 : bool verbose() const override { return false; } 57 0 : uint32_t maxPathTagLength() const override { return Tracing::DefaultMaxPathTagLength; } 58 : // This EgressConfigImpl is only used for async client tracing. Return false here is OK. 59 68 : bool spawnUpstreamSpan() const override { return false; } 60 : }; 61 : 62 : using EgressConfig = ConstSingleton<EgressConfigImpl>; 63 : 64 : class NullTracer : public Tracer { 65 : public: 66 : // Tracing::Tracer 67 : SpanPtr startSpan(const Config&, TraceContext&, const StreamInfo::StreamInfo&, 68 0 : Tracing::Decision) override { 69 0 : return SpanPtr{new NullSpan()}; 70 0 : } 71 : }; 72 : 73 : class TracerImpl : public Tracer { 74 : public: 75 : TracerImpl(DriverSharedPtr driver, const LocalInfo::LocalInfo& local_info); 76 : 77 : // Tracing::Tracer 78 : SpanPtr startSpan(const Config& config, TraceContext& trace_context, 79 : const StreamInfo::StreamInfo& stream_info, 80 : Tracing::Decision tracing_decision) override; 81 : 82 0 : DriverSharedPtr driverForTest() const { return driver_; } 83 : 84 : private: 85 : DriverSharedPtr driver_; 86 : const LocalInfo::LocalInfo& local_info_; 87 : }; 88 : 89 : } // namespace Tracing 90 : } // namespace Envoy