Coverage Report

Created: 2024-09-19 09:45

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