Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/source/extensions/tracers/skywalking/tracer.cc
Line
Count
Source (jump to first uncovered line)
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_->setSpanLayer(skywalking::v3::SpanLayer::Http);
46
0
  span_entity_->endSpan();
47
0
  parent_tracer_.sendSegment(tracing_context_);
48
0
}
49
50
void Span::injectContext(Tracing::TraceContext& trace_context,
51
0
                         const Tracing::UpstreamContext& upstream) {
52
0
  absl::string_view remote_address =
53
0
      upstream.host_.has_value() ? upstream.host_->address()->asStringView() : trace_context.host();
54
55
0
  auto sw8_header =
56
0
      tracing_context_->createSW8HeaderValue({remote_address.data(), remote_address.size()});
57
0
  if (sw8_header.has_value()) {
58
0
    skywalkingPropagationHeaderKey().setRefKey(trace_context, sw8_header.value());
59
60
    // Rewrite operation name with latest upstream request path for the EXIT span.
61
0
    absl::string_view upstream_request_path = trace_context.path();
62
0
    span_entity_->setOperationName({upstream_request_path.data(), upstream_request_path.size()});
63
0
  }
64
0
}
65
66
0
Tracing::SpanPtr Span::spawnChild(const Tracing::Config&, const std::string&, SystemTime) {
67
  // Reuse operation name of parent span by default.
68
0
  return std::make_unique<Span>(span_entity_->operationName(), span_entity_->spanLayer(), *this,
69
0
                                tracing_context_, parent_tracer_);
70
0
}
71
72
0
Tracer::Tracer(TraceSegmentReporterPtr reporter) : reporter_(std::move(reporter)) {}
73
74
0
void Tracer::sendSegment(TracingContextSharedPtr segment_context) {
75
0
  ASSERT(reporter_);
76
0
  if (segment_context->readyToSend()) {
77
0
    reporter_->report(std::move(segment_context));
78
0
  }
79
0
}
80
81
Tracing::SpanPtr Tracer::startSpan(absl::string_view name, absl::string_view protocol,
82
0
                                   TracingContextSharedPtr tracing_context) {
83
0
  return std::make_unique<Span>(name, protocol, tracing_context, *this);
84
0
}
85
} // namespace SkyWalking
86
} // namespace Tracers
87
} // namespace Extensions
88
} // namespace Envoy