|
10 | 10 | #include <vector> |
11 | 11 |
|
12 | 12 | #include "opentelemetry/common/timestamp.h" |
| 13 | +#include "opentelemetry/context/context.h" |
13 | 14 | #include "opentelemetry/nostd/shared_ptr.h" |
14 | 15 | #include "opentelemetry/nostd/span.h" |
| 16 | +#include "opentelemetry/nostd/string_view.h" |
15 | 17 | #include "opentelemetry/nostd/unique_ptr.h" |
16 | 18 | #include "opentelemetry/nostd/utility.h" |
| 19 | +#include "opentelemetry/nostd/variant.h" |
| 20 | +#include "opentelemetry/trace/context.h" |
17 | 21 | #include "opentelemetry/trace/noop.h" |
| 22 | +#include "opentelemetry/trace/scope.h" |
18 | 23 | #include "opentelemetry/trace/span.h" |
19 | 24 | #include "opentelemetry/trace/span_context.h" |
20 | 25 | #include "opentelemetry/trace/span_id.h" |
21 | 26 | #include "opentelemetry/trace/span_metadata.h" |
| 27 | +#include "opentelemetry/trace/span_startoptions.h" |
22 | 28 | #include "opentelemetry/trace/trace_flags.h" |
23 | 29 | #include "opentelemetry/trace/trace_id.h" |
24 | 30 | #include "opentelemetry/trace/tracer.h" |
@@ -107,3 +113,69 @@ TEST(NoopTest, CreateSpanValidSpanContext) |
107 | 113 | s1->AddEvent("even1"); // noop |
108 | 114 | s1->End(); // noop |
109 | 115 | } |
| 116 | + |
| 117 | +TEST(NoopTest, StartSpanPreservesParentSpanContext) |
| 118 | +{ |
| 119 | + constexpr uint8_t buf_span[] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 120 | + constexpr uint8_t buf_trace[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; |
| 121 | + const trace_api::SpanContext parent{trace_api::TraceId{buf_trace}, trace_api::SpanId{buf_span}, |
| 122 | + trace_api::TraceFlags{true}, false}; |
| 123 | + std::shared_ptr<trace_api::Tracer> tracer{new trace_api::NoopTracer()}; |
| 124 | + |
| 125 | + trace_api::StartSpanOptions parent_options; |
| 126 | + parent_options.parent = parent; |
| 127 | + auto child = tracer->StartSpan("child", parent_options); |
| 128 | + EXPECT_FALSE(child->IsRecording()); |
| 129 | + EXPECT_EQ(child->GetContext(), parent); |
| 130 | + |
| 131 | + auto parent_span = nonstd::shared_ptr<trace_api::Span>(new trace_api::NoopSpan( |
| 132 | + nonstd::unique_ptr<trace_api::SpanContext>(new trace_api::SpanContext(parent)))); |
| 133 | + opentelemetry::context::Context context; |
| 134 | + context = trace_api::SetSpan(context, parent_span); |
| 135 | + |
| 136 | + trace_api::StartSpanOptions context_options; |
| 137 | + context_options.parent = context; |
| 138 | + auto child_from_context = tracer->StartSpan("child", context_options); |
| 139 | + EXPECT_FALSE(child_from_context->IsRecording()); |
| 140 | + EXPECT_EQ(child_from_context->GetContext(), parent); |
| 141 | + |
| 142 | + auto context_with_span_context = opentelemetry::context::Context{ |
| 143 | + trace_api::kSpanKey, |
| 144 | + nonstd::shared_ptr<trace_api::SpanContext>(new trace_api::SpanContext(parent))}; |
| 145 | + context_options.parent = context_with_span_context; |
| 146 | + auto child_from_stored_span_context = tracer->StartSpan("child", context_options); |
| 147 | + EXPECT_FALSE(child_from_stored_span_context->IsRecording()); |
| 148 | + EXPECT_EQ(child_from_stored_span_context->GetContext(), parent); |
| 149 | + |
| 150 | + { |
| 151 | + trace_api::Scope scope(parent_span); |
| 152 | + auto child_from_current_context = tracer->StartSpan("child"); |
| 153 | + EXPECT_FALSE(child_from_current_context->IsRecording()); |
| 154 | + EXPECT_EQ(child_from_current_context->GetContext(), parent); |
| 155 | + |
| 156 | + trace_api::StartSpanOptions invalid_span_context_options; |
| 157 | + invalid_span_context_options.parent = trace_api::SpanContext::GetInvalid(); |
| 158 | + auto child_from_invalid_span_context = tracer->StartSpan("child", invalid_span_context_options); |
| 159 | + EXPECT_FALSE(child_from_invalid_span_context->IsRecording()); |
| 160 | + EXPECT_EQ(child_from_invalid_span_context->GetContext(), parent); |
| 161 | + |
| 162 | + trace_api::StartSpanOptions empty_context_options; |
| 163 | + empty_context_options.parent = opentelemetry::context::Context{}; |
| 164 | + auto child_from_empty_context = tracer->StartSpan("child", empty_context_options); |
| 165 | + EXPECT_FALSE(child_from_empty_context->IsRecording()); |
| 166 | + EXPECT_EQ(child_from_empty_context->GetContext(), parent); |
| 167 | + |
| 168 | + trace_api::StartSpanOptions invalid_span_options; |
| 169 | + invalid_span_options.parent = opentelemetry::context::Context{ |
| 170 | + trace_api::kSpanKey, nonstd::shared_ptr<trace_api::Span>(new trace_api::NoopSpan())}; |
| 171 | + auto child_from_invalid_span = tracer->StartSpan("child", invalid_span_options); |
| 172 | + EXPECT_FALSE(child_from_invalid_span->IsRecording()); |
| 173 | + EXPECT_EQ(child_from_invalid_span->GetContext(), parent); |
| 174 | + |
| 175 | + trace_api::StartSpanOptions root_context_options; |
| 176 | + root_context_options.parent = opentelemetry::context::Context{trace_api::kIsRootSpanKey, true}; |
| 177 | + auto root_child = tracer->StartSpan("root", root_context_options); |
| 178 | + EXPECT_FALSE(root_child->IsRecording()); |
| 179 | + EXPECT_FALSE(root_child->GetContext().IsValid()); |
| 180 | + } |
| 181 | +} |
0 commit comments