/proc/self/cwd/source/extensions/tracers/opentelemetry/tracer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <cstdint> |
4 | | |
5 | | #include "envoy/api/api.h" |
6 | | #include "envoy/config/trace/v3/opentelemetry.pb.h" |
7 | | #include "envoy/runtime/runtime.h" |
8 | | #include "envoy/thread_local/thread_local.h" |
9 | | #include "envoy/tracing/trace_driver.h" |
10 | | |
11 | | #include "source/common/common/logger.h" |
12 | | #include "source/extensions/tracers/common/factory_base.h" |
13 | | #include "source/extensions/tracers/opentelemetry/grpc_trace_exporter.h" |
14 | | #include "source/extensions/tracers/opentelemetry/resource_detectors/resource_detector.h" |
15 | | #include "source/extensions/tracers/opentelemetry/samplers/sampler.h" |
16 | | #include "source/extensions/tracers/opentelemetry/span_context.h" |
17 | | |
18 | | #include "absl/strings/escaping.h" |
19 | | |
20 | | namespace Envoy { |
21 | | namespace Extensions { |
22 | | namespace Tracers { |
23 | | namespace OpenTelemetry { |
24 | | |
25 | | #define OPENTELEMETRY_TRACER_STATS(COUNTER) \ |
26 | | COUNTER(spans_sent) \ |
27 | | COUNTER(timer_flushed) |
28 | | |
29 | | struct OpenTelemetryTracerStats { |
30 | | OPENTELEMETRY_TRACER_STATS(GENERATE_COUNTER_STRUCT) |
31 | | }; |
32 | | |
33 | | /** |
34 | | * OpenTelemetry Tracer. It is stored in TLS and contains the exporter. |
35 | | */ |
36 | | class Tracer : Logger::Loggable<Logger::Id::tracing> { |
37 | | public: |
38 | | Tracer(OpenTelemetryTraceExporterPtr exporter, Envoy::TimeSource& time_source, |
39 | | Random::RandomGenerator& random, Runtime::Loader& runtime, Event::Dispatcher& dispatcher, |
40 | | OpenTelemetryTracerStats tracing_stats, const ResourceConstSharedPtr resource, |
41 | | SamplerSharedPtr sampler); |
42 | | |
43 | | void sendSpan(::opentelemetry::proto::trace::v1::Span& span); |
44 | | |
45 | | Tracing::SpanPtr startSpan(const Tracing::Config& config, const std::string& operation_name, |
46 | | SystemTime start_time, Tracing::Decision tracing_decision, |
47 | | bool downstream_span = true); |
48 | | |
49 | | Tracing::SpanPtr startSpan(const Tracing::Config& config, const std::string& operation_name, |
50 | | SystemTime start_time, const SpanContext& previous_span_context, |
51 | | bool downstream_span = true); |
52 | | |
53 | | private: |
54 | | /** |
55 | | * Enables the span-flushing timer. |
56 | | */ |
57 | | void enableTimer(); |
58 | | /* |
59 | | * Removes all spans from the span buffer and sends them to the collector. |
60 | | */ |
61 | | void flushSpans(); |
62 | | |
63 | | OpenTelemetryTraceExporterPtr exporter_; |
64 | | Envoy::TimeSource& time_source_; |
65 | | Random::RandomGenerator& random_; |
66 | | std::vector<::opentelemetry::proto::trace::v1::Span> span_buffer_; |
67 | | Runtime::Loader& runtime_; |
68 | | Event::TimerPtr flush_timer_; |
69 | | OpenTelemetryTracerStats tracing_stats_; |
70 | | const ResourceConstSharedPtr resource_; |
71 | | SamplerSharedPtr sampler_; |
72 | | }; |
73 | | |
74 | | /** |
75 | | * OpenTelemetry tracing implementation of the Envoy Span object. |
76 | | * Note that it has a pointer to its parent Tracer to access the shared Exporter. |
77 | | */ |
78 | | class Span : Logger::Loggable<Logger::Id::tracing>, public Tracing::Span { |
79 | | public: |
80 | | Span(const Tracing::Config& config, const std::string& name, SystemTime start_time, |
81 | | Envoy::TimeSource& time_source, Tracer& parent_tracer, bool downstream_span); |
82 | | |
83 | | // Tracing::Span functions |
84 | 0 | void setOperation(absl::string_view /*operation*/) override{}; |
85 | | void setTag(absl::string_view /*name*/, absl::string_view /*value*/) override; |
86 | 0 | void log(SystemTime /*timestamp*/, const std::string& /*event*/) override{}; |
87 | | void finishSpan() override; |
88 | | void injectContext(Envoy::Tracing::TraceContext& /*trace_context*/, |
89 | | const Upstream::HostDescriptionConstSharedPtr&) override; |
90 | | Tracing::SpanPtr spawnChild(const Tracing::Config& config, const std::string& name, |
91 | | SystemTime start_time) override; |
92 | | |
93 | | /** |
94 | | * Set the span's sampled flag. |
95 | | */ |
96 | 0 | void setSampled(bool sampled) override { sampled_ = sampled; }; |
97 | | |
98 | | /** |
99 | | * @return whether or not the sampled attribute is set |
100 | | */ |
101 | | |
102 | 0 | bool sampled() const { return sampled_; } |
103 | | |
104 | 0 | std::string getBaggage(absl::string_view /*key*/) override { return EMPTY_STRING; }; |
105 | 0 | void setBaggage(absl::string_view /*key*/, absl::string_view /*value*/) override{}; |
106 | | |
107 | | // Additional methods |
108 | | |
109 | | /** |
110 | | * Sets the span's trace id attribute. |
111 | | */ |
112 | 0 | void setTraceId(const absl::string_view& trace_id_hex) { |
113 | 0 | span_.set_trace_id(absl::HexStringToBytes(trace_id_hex)); |
114 | 0 | } |
115 | | |
116 | 0 | std::string getTraceIdAsHex() const override { return absl::BytesToHexString(span_.trace_id()); }; |
117 | | |
118 | 0 | ::opentelemetry::proto::trace::v1::Span::SpanKind spankind() const { return span_.kind(); } |
119 | | |
120 | | /** |
121 | | * Sets the span's id. |
122 | | */ |
123 | 0 | void setId(const absl::string_view& span_id_hex) { |
124 | 0 | span_.set_span_id(absl::HexStringToBytes(span_id_hex)); |
125 | 0 | } |
126 | | |
127 | 0 | std::string spanId() { return absl::BytesToHexString(span_.span_id()); } |
128 | | |
129 | | /** |
130 | | * Sets the span's parent id. |
131 | | */ |
132 | 0 | void setParentId(const absl::string_view& parent_span_id_hex) { |
133 | 0 | span_.set_parent_span_id(absl::HexStringToBytes(parent_span_id_hex)); |
134 | 0 | } |
135 | | |
136 | 0 | std::string tracestate() const { return span_.trace_state(); } |
137 | | |
138 | | /** |
139 | | * Sets the span's tracestate. |
140 | | */ |
141 | 0 | void setTracestate(const absl::string_view& tracestate) { |
142 | 0 | span_.set_trace_state(std::string{tracestate}); |
143 | 0 | } |
144 | | |
145 | | /** |
146 | | * Method to access the span for testing. |
147 | | */ |
148 | 0 | const ::opentelemetry::proto::trace::v1::Span& spanForTest() const { return span_; } |
149 | | |
150 | | private: |
151 | | ::opentelemetry::proto::trace::v1::Span span_; |
152 | | Tracer& parent_tracer_; |
153 | | Envoy::TimeSource& time_source_; |
154 | | bool sampled_; |
155 | | }; |
156 | | |
157 | | using TracerPtr = std::unique_ptr<Tracer>; |
158 | | |
159 | | } // namespace OpenTelemetry |
160 | | } // namespace Tracers |
161 | | } // namespace Extensions |
162 | | } // namespace Envoy |