/proc/self/cwd/source/extensions/tracers/skywalking/tracer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <memory> |
4 | | |
5 | | #include "envoy/tracing/trace_driver.h" |
6 | | |
7 | | #include "source/common/tracing/common_values.h" |
8 | | #include "source/common/tracing/null_span_impl.h" |
9 | | #include "source/extensions/tracers/skywalking/trace_segment_reporter.h" |
10 | | |
11 | | #include "cpp2sky/tracing_context.h" |
12 | | #include "cpp2sky/well_known_names.h" |
13 | | |
14 | | namespace Envoy { |
15 | | namespace Extensions { |
16 | | namespace Tracers { |
17 | | namespace SkyWalking { |
18 | | |
19 | | using cpp2sky::TracingContextPtr; |
20 | | using cpp2sky::TracingSpanPtr; |
21 | | |
22 | | const Http::LowerCaseString& skywalkingPropagationHeaderKey(); |
23 | | |
24 | | class Tracer { |
25 | | public: |
26 | | Tracer(TraceSegmentReporterPtr reporter); |
27 | | |
28 | | /* |
29 | | * Report trace segment data to backend tracing service. |
30 | | * |
31 | | * @param segment_context The segment context. |
32 | | */ |
33 | | void sendSegment(TracingContextPtr tracing_context); |
34 | | |
35 | | /* |
36 | | * Create a new span based on the segment context and parent span. |
37 | | * |
38 | | * @param name Operation name of span. |
39 | | * @param tracing_context The SkyWalking tracing context. The newly created span belongs to this |
40 | | * context. |
41 | | * |
42 | | * @return The unique ptr to the newly created span. |
43 | | */ |
44 | | Tracing::SpanPtr startSpan(absl::string_view name, absl::string_view protocol, |
45 | | TracingContextPtr tracing_context); |
46 | | |
47 | | private: |
48 | | TraceSegmentReporterPtr reporter_; |
49 | | }; |
50 | | |
51 | | using TracerPtr = std::unique_ptr<Tracer>; |
52 | | |
53 | | class Span : public Tracing::Span { |
54 | | public: |
55 | | Span(absl::string_view name, absl::string_view protocol, TracingContextPtr tracing_context, |
56 | | Tracer& parent_tracer) |
57 | | : parent_tracer_(parent_tracer), tracing_context_(tracing_context), |
58 | 0 | span_entity_(tracing_context_->createEntrySpan()) { |
59 | 0 | span_entity_->startSpan({name.data(), name.size()}); |
60 | 0 | skywalking::v3::SpanLayer layer; |
61 | 0 | if (absl::StrContains(protocol, "HTTP")) { |
62 | | // TraceContext.protocol of http is parsed from http message, which value could be HTTP/1.1, |
63 | | // etc. |
64 | 0 | layer = skywalking::v3::SpanLayer::Http; |
65 | 0 | } else if (!skywalking::v3::SpanLayer_Parse(std::string(protocol), &layer)) { |
66 | 0 | layer = skywalking::v3::SpanLayer::Unknown; |
67 | 0 | } |
68 | 0 | span_entity_->setSpanLayer(layer); |
69 | 0 | } |
70 | | Span(absl::string_view name, skywalking::v3::SpanLayer span_layer, Span& parent_span, |
71 | | TracingContextPtr tracing_context, Tracer& parent_tracer) |
72 | | : parent_tracer_(parent_tracer), tracing_context_(tracing_context), |
73 | 0 | span_entity_(tracing_context_->createExitSpan(parent_span.spanEntity())) { |
74 | 0 | span_entity_->startSpan({name.data(), name.size()}); |
75 | 0 | span_entity_->setSpanLayer(span_layer); |
76 | 0 | } |
77 | | |
78 | | // Tracing::Span |
79 | 0 | void setOperation(absl::string_view) override {} |
80 | | void setTag(absl::string_view name, absl::string_view value) override; |
81 | | void log(SystemTime timestamp, const std::string& event) override; |
82 | | void finishSpan() override; |
83 | | void injectContext(Tracing::TraceContext& trace_context, |
84 | | const Upstream::HostDescriptionConstSharedPtr& upstream) override; |
85 | | Tracing::SpanPtr spawnChild(const Tracing::Config& config, const std::string& name, |
86 | | SystemTime start_time) override; |
87 | | void setSampled(bool do_sample) override; |
88 | 0 | std::string getBaggage(absl::string_view) override { return EMPTY_STRING; } |
89 | 0 | void setBaggage(absl::string_view, absl::string_view) override {} |
90 | 0 | std::string getTraceIdAsHex() const override { return EMPTY_STRING; } |
91 | | |
92 | 0 | const TracingContextPtr tracingContext() { return tracing_context_; } |
93 | 0 | const TracingSpanPtr spanEntity() { return span_entity_; } |
94 | | |
95 | | private: |
96 | | Tracer& parent_tracer_; |
97 | | TracingContextPtr tracing_context_; |
98 | | TracingSpanPtr span_entity_; |
99 | | }; |
100 | | |
101 | | } // namespace SkyWalking |
102 | | } // namespace Tracers |
103 | | } // namespace Extensions |
104 | | } // namespace Envoy |