Line data Source code
1 : #include "source/extensions/tracers/skywalking/tracer.h" 2 : 3 : #include <string> 4 : 5 : namespace Envoy { 6 : namespace Extensions { 7 : namespace Tracers { 8 : namespace SkyWalking { 9 : 10 : namespace { 11 : static constexpr absl::string_view StatusCodeTag = "status_code"; 12 : static constexpr absl::string_view UrlTag = "url"; 13 : } // namespace 14 : 15 0 : const Tracing::TraceContextHandler& skywalkingPropagationHeaderKey() { 16 0 : CONSTRUCT_ON_FIRST_USE(Tracing::TraceContextHandler, "sw8"); 17 0 : } 18 : 19 0 : void Span::setTag(absl::string_view name, absl::string_view value) { 20 0 : if (name == Tracing::Tags::get().HttpUrl) { 21 0 : span_entity_->addTag(UrlTag.data(), std::string(value)); 22 0 : } else if (name == Tracing::Tags::get().HttpStatusCode) { 23 0 : span_entity_->addTag(StatusCodeTag.data(), std::string(value)); 24 0 : } else if (name == Tracing::Tags::get().Error) { 25 0 : span_entity_->setErrorStatus(); 26 0 : span_entity_->addTag(std::string(name), std::string(value)); 27 0 : } else if (name == Tracing::Tags::get().PeerAddress) { 28 0 : span_entity_->setPeer(std::string(value)); 29 0 : } else { 30 0 : span_entity_->addTag(std::string(name), std::string(value)); 31 0 : } 32 0 : } 33 : 34 0 : void Span::setSampled(bool do_sample) { 35 : // Sampling status is always true on SkyWalking. But with disabling skip_analysis, 36 : // this span can't be analyzed. 37 0 : if (!do_sample) { 38 0 : span_entity_->setSkipAnalysis(); 39 0 : } 40 0 : } 41 : 42 0 : void Span::log(SystemTime, const std::string& event) { span_entity_->addLog(EMPTY_STRING, event); } 43 : 44 0 : void Span::finishSpan() { 45 0 : span_entity_->endSpan(); 46 0 : parent_tracer_.sendSegment(tracing_context_); 47 0 : } 48 : 49 : void Span::injectContext(Tracing::TraceContext& trace_context, 50 0 : const Upstream::HostDescriptionConstSharedPtr& upstream) { 51 0 : absl::string_view remote_address = 52 0 : upstream != nullptr ? upstream->address()->asStringView() : trace_context.host(); 53 : 54 0 : auto sw8_header = 55 0 : tracing_context_->createSW8HeaderValue({remote_address.data(), remote_address.size()}); 56 0 : if (sw8_header.has_value()) { 57 0 : skywalkingPropagationHeaderKey().setRefKey(trace_context, sw8_header.value()); 58 : 59 : // Rewrite operation name with latest upstream request path for the EXIT span. 60 0 : absl::string_view upstream_request_path = trace_context.path(); 61 0 : span_entity_->setOperationName({upstream_request_path.data(), upstream_request_path.size()}); 62 0 : } 63 0 : } 64 : 65 0 : Tracing::SpanPtr Span::spawnChild(const Tracing::Config&, const std::string&, SystemTime) { 66 : // Reuse operation name of parent span by default. 67 0 : return std::make_unique<Span>(span_entity_->operationName(), span_entity_->spanLayer(), *this, 68 0 : tracing_context_, parent_tracer_); 69 0 : } 70 : 71 0 : Tracer::Tracer(TraceSegmentReporterPtr reporter) : reporter_(std::move(reporter)) {} 72 : 73 0 : void Tracer::sendSegment(TracingContextPtr segment_context) { 74 0 : ASSERT(reporter_); 75 0 : if (segment_context->readyToSend()) { 76 0 : reporter_->report(std::move(segment_context)); 77 0 : } 78 0 : } 79 : 80 : Tracing::SpanPtr Tracer::startSpan(absl::string_view name, absl::string_view protocol, 81 0 : TracingContextPtr tracing_context) { 82 0 : return std::make_unique<Span>(name, protocol, tracing_context, *this); 83 0 : } 84 : } // namespace SkyWalking 85 : } // namespace Tracers 86 : } // namespace Extensions 87 : } // namespace Envoy