|
4 | 4 | #include <chrono> |
5 | 5 | #include <cstdint> |
6 | 6 | #include <mutex> |
7 | | -#include <new> |
8 | 7 | #include <utility> |
9 | 8 |
|
10 | | -#include "opentelemetry/common/key_value_iterable.h" |
11 | 9 | #include "opentelemetry/context/context.h" |
12 | 10 | #include "opentelemetry/context/runtime_context.h" |
13 | 11 | #include "opentelemetry/nostd/shared_ptr.h" |
|
22 | 20 | #include "opentelemetry/sdk/trace/tracer_context.h" |
23 | 21 | #include "opentelemetry/trace/context.h" |
24 | 22 | #include "opentelemetry/trace/default_span.h" |
25 | | -#include "opentelemetry/trace/span.h" |
26 | 23 | #include "opentelemetry/trace/span_context.h" |
27 | | -#include "opentelemetry/trace/span_context_kv_iterable.h" |
28 | 24 | #include "opentelemetry/trace/span_id.h" |
29 | 25 | #include "opentelemetry/trace/span_startoptions.h" |
30 | 26 | #include "opentelemetry/trace/trace_flags.h" |
|
34 | 30 |
|
35 | 31 | #include "src/trace/span.h" |
36 | 32 |
|
37 | | -OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk |
| 33 | +OPENTELEMETRY_BEGIN_NAMESPACE |
| 34 | +namespace sdk |
| 35 | +{ |
| 36 | +namespace trace |
38 | 37 | { |
39 | | - namespace trace |
40 | | - { |
41 | | - |
42 | | - namespace |
43 | | - { |
44 | 38 |
|
45 | | - nostd::shared_ptr<opentelemetry::trace::Span> MakeNonRecordingSpan( |
46 | | - opentelemetry::trace::SpanContext &&span_context) noexcept |
47 | | - { |
48 | | -#if OPENTELEMETRY_HAVE_EXCEPTIONS |
49 | | - try |
50 | | - { |
51 | | -#endif |
52 | | - return {std::make_shared<opentelemetry::trace::DefaultSpan>(std::move(span_context))}; |
| 39 | +Tracer::Tracer(std::shared_ptr<TracerContext> context, |
| 40 | + std::unique_ptr<InstrumentationScope> instrumentation_scope) |
| 41 | + : instrumentation_scope_{std::move(instrumentation_scope)}, |
| 42 | + context_{std::move(context)}, |
| 43 | + tracer_config_(context_->GetTracerConfigurator().ComputeConfig(*instrumentation_scope_)), |
| 44 | + noop_span_{std::make_shared<opentelemetry::trace::DefaultSpan>( |
| 45 | + opentelemetry::trace::SpanContext::GetInvalid())} |
| 46 | +{ |
| 47 | + UpdateEnabled(tracer_config_.IsEnabled()); |
| 48 | +} |
| 49 | + |
| 50 | +nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan( |
| 51 | + nostd::string_view name, |
| 52 | + const opentelemetry::common::KeyValueIterable &attributes, |
| 53 | + const opentelemetry::trace::SpanContextKeyValueIterable &links, |
| 54 | + const opentelemetry::trace::StartSpanOptions &options) noexcept |
| 55 | +{ |
53 | 56 | #if OPENTELEMETRY_HAVE_EXCEPTIONS |
54 | | - } |
55 | | - catch (const std::bad_alloc &) |
56 | | - { |
57 | | - return {}; |
58 | | - } |
59 | | -#else |
60 | | - return nostd::shared_ptr<opentelemetry::trace::Span>{ |
61 | | - new (std::nothrow) opentelemetry::trace::DefaultSpan(std::move(span_context))}; |
62 | | -#endif |
63 | | - } |
64 | | - |
65 | | - nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan( |
66 | | - std::shared_ptr<Tracer> &&tracer, |
67 | | - nostd::string_view name, |
68 | | - const opentelemetry::common::KeyValueIterable &attributes, |
69 | | - const opentelemetry::trace::SpanContextKeyValueIterable &links, |
70 | | - const opentelemetry::trace::StartSpanOptions &options, |
71 | | - const opentelemetry::sdk::trace::SamplingResult &sampling_result, |
72 | | - const opentelemetry::trace::SpanContext &parent_context, |
73 | | - opentelemetry::trace::SpanContext &&span_context) noexcept |
| 57 | + try |
74 | 58 | { |
75 | | -#if OPENTELEMETRY_HAVE_EXCEPTIONS |
76 | | - try |
77 | | - { |
78 | | -#endif |
79 | | - return {std::make_shared<Span>(std::move(tracer), name, attributes, links, options, |
80 | | - sampling_result, parent_context, std::move(span_context))}; |
81 | | -#if OPENTELEMETRY_HAVE_EXCEPTIONS |
82 | | - } |
83 | | - catch (const std::bad_alloc &) |
84 | | - { |
85 | | - return {}; |
86 | | - } |
87 | | -#else |
88 | | - return nostd::shared_ptr<opentelemetry::trace::Span>{ |
89 | | - new (std::nothrow) Span{std::move(tracer), name, attributes, links, options, |
90 | | - sampling_result, parent_context, std::move(span_context)}}; |
91 | 59 | #endif |
92 | | - } |
93 | | - |
94 | | - } // namespace |
95 | | - |
96 | | - Tracer::Tracer(std::shared_ptr<TracerContext> context, |
97 | | - std::unique_ptr<InstrumentationScope> instrumentation_scope) |
98 | | - : instrumentation_scope_{std::move(instrumentation_scope)}, |
99 | | - context_{std::move(context)}, |
100 | | - tracer_config_(context_->GetTracerConfigurator().ComputeConfig(*instrumentation_scope_)), |
101 | | - noop_span_{std::make_shared<opentelemetry::trace::DefaultSpan>( |
102 | | - opentelemetry::trace::SpanContext::GetInvalid())} |
103 | | - { |
104 | | - UpdateEnabled(tracer_config_.IsEnabled()); |
105 | | - } |
106 | | - |
107 | | - nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan( |
108 | | - nostd::string_view name, |
109 | | - const opentelemetry::common::KeyValueIterable &attributes, |
110 | | - const opentelemetry::trace::SpanContextKeyValueIterable &links, |
111 | | - const opentelemetry::trace::StartSpanOptions &options) noexcept |
112 | | - { |
113 | 60 | // Check if the tracer is enabled using the API Tracer::Enabled() accessor if available. |
114 | 61 | if (!Enabled()) |
115 | 62 | { |
@@ -200,56 +147,60 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk |
200 | 147 | opentelemetry::trace::SpanContext span_context(trace_id, span_id, trace_flags, false, |
201 | 148 | get_trace_state()); |
202 | 149 |
|
| 150 | + nostd::shared_ptr<opentelemetry::trace::Span> span; |
| 151 | + |
203 | 152 | if (!sampling_result.IsRecording()) |
204 | 153 | { |
205 | | - auto non_recording_span = MakeNonRecordingSpan(std::move(span_context)); |
206 | | - |
207 | | - if (!non_recording_span) |
208 | | - { |
209 | | - return noop_span_; |
210 | | - } |
211 | | - return non_recording_span; |
| 154 | + span = nostd::shared_ptr<opentelemetry::trace::Span>{ |
| 155 | + std::make_shared<opentelemetry::trace::DefaultSpan>(std::move(span_context))}; |
212 | 156 | } |
213 | | - |
214 | | - auto span = MakeSpan(shared_from_this(), name, attributes, links, options, sampling_result, |
215 | | - parent_context, std::move(span_context)); |
216 | | - if (!span) |
| 157 | + else |
217 | 158 | { |
218 | | - return noop_span_; |
| 159 | + span = nostd::shared_ptr<opentelemetry::trace::Span>{ |
| 160 | + std::make_shared<Span>(shared_from_this(), name, attributes, links, options, |
| 161 | + sampling_result, parent_context, std::move(span_context))}; |
219 | 162 | } |
| 163 | + |
220 | 164 | return span; |
| 165 | +#if OPENTELEMETRY_HAVE_EXCEPTIONS |
221 | 166 | } |
| 167 | + catch (...) |
| 168 | + { |
| 169 | + return noop_span_; |
| 170 | + } |
| 171 | +#endif |
| 172 | +} |
222 | 173 |
|
223 | | - void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept |
| 174 | +void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept |
| 175 | +{ |
| 176 | + if (context_) |
224 | 177 | { |
225 | | - if (context_) |
226 | | - { |
227 | | - context_->ForceFlush( |
228 | | - std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(timeout)}); |
229 | | - } |
| 178 | + context_->ForceFlush( |
| 179 | + std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(timeout)}); |
230 | 180 | } |
| 181 | +} |
231 | 182 |
|
232 | | - void Tracer::CloseWithMicroseconds(uint64_t timeout) noexcept |
| 183 | +void Tracer::CloseWithMicroseconds(uint64_t timeout) noexcept |
| 184 | +{ |
| 185 | + // Trace context is shared by many tracers.So we just call ForceFlush to flush all pending spans |
| 186 | + // and do not shutdown it. |
| 187 | + if (context_) |
233 | 188 | { |
234 | | - // Trace context is shared by many tracers.So we just call ForceFlush to flush all pending spans |
235 | | - // and do not shutdown it. |
236 | | - if (context_) |
237 | | - { |
238 | | - context_->ForceFlush( |
239 | | - std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(timeout)}); |
240 | | - } |
| 189 | + context_->ForceFlush( |
| 190 | + std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(timeout)}); |
241 | 191 | } |
| 192 | +} |
242 | 193 |
|
243 | | - void Tracer::UpdateTracerConfig(TracerConfig config) noexcept |
| 194 | +void Tracer::UpdateTracerConfig(TracerConfig config) noexcept |
| 195 | +{ |
| 196 | + const bool enabled = config.IsEnabled(); |
244 | 197 | { |
245 | | - const bool enabled = config.IsEnabled(); |
246 | | - { |
247 | | - std::lock_guard<std::mutex> lock(tracer_config_mutex_); |
248 | | - tracer_config_ = config; |
249 | | - } |
250 | | - UpdateEnabled(enabled); |
| 198 | + std::lock_guard<std::mutex> lock(tracer_config_mutex_); |
| 199 | + tracer_config_ = config; |
251 | 200 | } |
| 201 | + UpdateEnabled(enabled); |
| 202 | +} |
252 | 203 |
|
253 | | - } // namespace trace |
| 204 | +} // namespace trace |
254 | 205 | } // namespace sdk |
255 | 206 | OPENTELEMETRY_END_NAMESPACE |
0 commit comments