From b999750268da780b9548537a44e4744c4ef1ad4a Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 4 Dec 2023 19:36:31 +0000 Subject: [PATCH 1/6] initial commit --- sdk/src/trace/tracer.cc | 18 ++++++++++++++---- sdk/test/trace/tracer_test.cc | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 6ca7b37df5..2e47c379f5 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -43,6 +43,17 @@ nostd::shared_ptr Tracer::StartSpan( { parent_context = span_context; } + else + { + context::ContextValue is_root_span = context.GetValue("is_root_span"); + if (nostd::holds_alternative(is_root_span)) + { + if (nostd::get(is_root_span)) + { + parent_context = opentelemetry::trace::SpanContext{false, false}; + } + } + } } opentelemetry::trace::TraceId trace_id; @@ -69,10 +80,9 @@ nostd::shared_ptr Tracer::StartSpan( auto span_context = std::unique_ptr(new opentelemetry::trace::SpanContext( trace_id, span_id, trace_flags, false, - sampling_result.trace_state - ? sampling_result.trace_state - : is_parent_span_valid ? parent_context.trace_state() - : opentelemetry::trace::TraceState::GetDefault())); + sampling_result.trace_state ? sampling_result.trace_state + : is_parent_span_valid ? parent_context.trace_state() + : opentelemetry::trace::TraceState::GetDefault())); if (!sampling_result.IsRecording()) { diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index 36cc135a1d..fcc674aa42 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -1008,6 +1008,26 @@ TEST(Tracer, WithActiveSpan) spans = span_data->GetSpans(); ASSERT_EQ(1, spans.size()); EXPECT_EQ("span 2", spans.at(0)->GetName()); + EXPECT_EQ(spans.at(0).get()->GetParentSpanId(), span_first->GetContext().span_id()); + EXPECT_EQ(spans.at(0).get()->GetTraceId(), span_first->GetContext().trace_id()); + + { + trace_api::StartSpanOptions options; + opentelemetry::context::Context c1; + c1 = c1.SetValue("is_root_span", true); + options.parent = c1; + auto root_span = tracer->StartSpan("span root", options); + + spans = span_data->GetSpans(); + ASSERT_EQ(0, spans.size()); + + root_span->End(); + } + spans = span_data->GetSpans(); + ASSERT_EQ(1, spans.size()); + EXPECT_EQ("span root", spans.at(0)->GetName()); + EXPECT_EQ(spans.at(0).get()->GetParentSpanId(), opentelemetry::trace::SpanId()); + EXPECT_NE(spans.at(0).get()->GetTraceId(), span_first->GetContext().trace_id()); span_first->End(); } From 14da5e697d255a43898362ce2f1f3e04170b2e5d Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 4 Dec 2023 11:37:48 -0800 Subject: [PATCH 2/6] format --- sdk/src/trace/tracer.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 2e47c379f5..772aab671b 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -80,9 +80,10 @@ nostd::shared_ptr Tracer::StartSpan( auto span_context = std::unique_ptr(new opentelemetry::trace::SpanContext( trace_id, span_id, trace_flags, false, - sampling_result.trace_state ? sampling_result.trace_state - : is_parent_span_valid ? parent_context.trace_state() - : opentelemetry::trace::TraceState::GetDefault())); + sampling_result.trace_state + ? sampling_result.trace_state + : is_parent_span_valid ? parent_context.trace_state() + : opentelemetry::trace::TraceState::GetDefault())); if (!sampling_result.IsRecording()) { From 94369dd7c3a9adfa83ced71752d2cad00372839e Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Dec 2023 18:33:21 +0000 Subject: [PATCH 3/6] add comment, and reorganize --- api/include/opentelemetry/trace/context.h | 10 ++++++++++ .../opentelemetry/trace/span_metadata.h | 1 + .../opentelemetry/trace/span_startoptions.h | 19 +++++++++++++++++-- sdk/src/trace/tracer.cc | 7 +------ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/api/include/opentelemetry/trace/context.h b/api/include/opentelemetry/trace/context.h index b68fe9e95a..cd8395d768 100644 --- a/api/include/opentelemetry/trace/context.h +++ b/api/include/opentelemetry/trace/context.h @@ -23,6 +23,16 @@ inline nostd::shared_ptr GetSpan(const context::Context &context) noexcept return nostd::shared_ptr(new DefaultSpan(SpanContext::GetInvalid())); } +inline bool IsRootSpan(const context::Context &context) noexcept +{ + context::ContextValue is_root_span = context.GetValue(kIsRootSpanKey); + if (nostd::holds_alternative(is_root_span)) + { + return nostd::get(is_root_span); + } + return false; +} + // Set Span into explicit context inline context::Context SetSpan(context::Context &context, nostd::shared_ptr span) noexcept { diff --git a/api/include/opentelemetry/trace/span_metadata.h b/api/include/opentelemetry/trace/span_metadata.h index 5e615ea537..8eb3e37255 100644 --- a/api/include/opentelemetry/trace/span_metadata.h +++ b/api/include/opentelemetry/trace/span_metadata.h @@ -21,6 +21,7 @@ enum class SpanKind // The key identifies the active span in the current context. constexpr char kSpanKey[] = "active_span"; +constexpr char kIsRootSpanKey[] = "is_root_span"; // StatusCode - Represents the canonical set of status codes of a finished Span. enum class StatusCode diff --git a/api/include/opentelemetry/trace/span_startoptions.h b/api/include/opentelemetry/trace/span_startoptions.h index 2180394d72..5152cc9cbd 100644 --- a/api/include/opentelemetry/trace/span_startoptions.h +++ b/api/include/opentelemetry/trace/span_startoptions.h @@ -34,8 +34,23 @@ struct StartSpanOptions // Explicitly set the parent of a Span. // - // This defaults to an invalid span context. In this case, the Span is - // automatically parented to the currently active span. + // The `parent` field in `StartSpanOptions` struct is designed to establish + // parent-child relationships in tracing spans. It can be set to either a + // `SpanContext` or a `context::Context` object. + // + // - When set to valid `SpanContext`, it directly assigns a specific Span as the parent + // of the newly created Span. + // + // - Alternatively, setting the `parent` field to a `context::Context` allows for + // more nuanced parent identification: + // 1. If the `Context` contains a Span object, this Span is treated as the parent. + // 2. If the `Context` contains the boolean flag `is_root_span` set to `true`, + // it indicates that the new Span should be treated as a root Span, i.e., it + // does not have a parent Span. + // + // - If the `parent` field is not set, the newly created Span will inherit the + // parent of the currently active Span (if any) in the current context. + // nostd::variant parent = SpanContext::GetInvalid(); // TODO: diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 772aab671b..05b054b2f6 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -45,14 +45,9 @@ nostd::shared_ptr Tracer::StartSpan( } else { - context::ContextValue is_root_span = context.GetValue("is_root_span"); - if (nostd::holds_alternative(is_root_span)) - { - if (nostd::get(is_root_span)) - { + if (opentelemetry::trace::IsRootSpan(context)) { parent_context = opentelemetry::trace::SpanContext{false, false}; } - } } } From 8a47e2e3ffd59bdd3f9452c08e2c516b62c1b3f9 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Dec 2023 18:39:14 +0000 Subject: [PATCH 4/6] format --- api/include/opentelemetry/trace/span_metadata.h | 2 +- api/include/opentelemetry/trace/span_startoptions.h | 10 +++++----- sdk/src/trace/tracer.cc | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/api/include/opentelemetry/trace/span_metadata.h b/api/include/opentelemetry/trace/span_metadata.h index 8eb3e37255..d2d6a9f85d 100644 --- a/api/include/opentelemetry/trace/span_metadata.h +++ b/api/include/opentelemetry/trace/span_metadata.h @@ -20,7 +20,7 @@ enum class SpanKind }; // The key identifies the active span in the current context. -constexpr char kSpanKey[] = "active_span"; +constexpr char kSpanKey[] = "active_span"; constexpr char kIsRootSpanKey[] = "is_root_span"; // StatusCode - Represents the canonical set of status codes of a finished Span. diff --git a/api/include/opentelemetry/trace/span_startoptions.h b/api/include/opentelemetry/trace/span_startoptions.h index 5152cc9cbd..c566a0af73 100644 --- a/api/include/opentelemetry/trace/span_startoptions.h +++ b/api/include/opentelemetry/trace/span_startoptions.h @@ -35,19 +35,19 @@ struct StartSpanOptions // Explicitly set the parent of a Span. // // The `parent` field in `StartSpanOptions` struct is designed to establish - // parent-child relationships in tracing spans. It can be set to either a + // parent-child relationships in tracing spans. It can be set to either a // `SpanContext` or a `context::Context` object. // - // - When set to valid `SpanContext`, it directly assigns a specific Span as the parent + // - When set to valid `SpanContext`, it directly assigns a specific Span as the parent // of the newly created Span. // - // - Alternatively, setting the `parent` field to a `context::Context` allows for + // - Alternatively, setting the `parent` field to a `context::Context` allows for // more nuanced parent identification: // 1. If the `Context` contains a Span object, this Span is treated as the parent. // 2. If the `Context` contains the boolean flag `is_root_span` set to `true`, - // it indicates that the new Span should be treated as a root Span, i.e., it + // it indicates that the new Span should be treated as a root Span, i.e., it // does not have a parent Span. - // + // // - If the `parent` field is not set, the newly created Span will inherit the // parent of the currently active Span (if any) in the current context. // diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 05b054b2f6..c722d60326 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -45,9 +45,10 @@ nostd::shared_ptr Tracer::StartSpan( } else { - if (opentelemetry::trace::IsRootSpan(context)) { - parent_context = opentelemetry::trace::SpanContext{false, false}; - } + if (opentelemetry::trace::IsRootSpan(context)) + { + parent_context = opentelemetry::trace::SpanContext{false, false}; + } } } From 046645a7def3f72f9bc4bcad3763aa388e68ded1 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Dec 2023 19:46:26 +0000 Subject: [PATCH 5/6] nit header doc --- api/include/opentelemetry/trace/span_startoptions.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/include/opentelemetry/trace/span_startoptions.h b/api/include/opentelemetry/trace/span_startoptions.h index c566a0af73..847470f845 100644 --- a/api/include/opentelemetry/trace/span_startoptions.h +++ b/api/include/opentelemetry/trace/span_startoptions.h @@ -34,9 +34,9 @@ struct StartSpanOptions // Explicitly set the parent of a Span. // - // The `parent` field in `StartSpanOptions` struct is designed to establish - // parent-child relationships in tracing spans. It can be set to either a - // `SpanContext` or a `context::Context` object. + // The `parent` field is designed to establish parent-child relationships + // in tracing spans. It can be set to either a `SpanContext` or a + // `context::Context` object. // // - When set to valid `SpanContext`, it directly assigns a specific Span as the parent // of the newly created Span. From 204c170d82d2073dc42d2ceaad1f21a6827221e9 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 5 Dec 2023 21:32:33 +0000 Subject: [PATCH 6/6] add code snippt --- api/include/opentelemetry/trace/span_startoptions.h | 8 ++++++++ sdk/test/trace/tracer_test.cc | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/api/include/opentelemetry/trace/span_startoptions.h b/api/include/opentelemetry/trace/span_startoptions.h index 847470f845..8a2165b0f0 100644 --- a/api/include/opentelemetry/trace/span_startoptions.h +++ b/api/include/opentelemetry/trace/span_startoptions.h @@ -47,6 +47,14 @@ struct StartSpanOptions // 2. If the `Context` contains the boolean flag `is_root_span` set to `true`, // it indicates that the new Span should be treated as a root Span, i.e., it // does not have a parent Span. + // Example Usage: + // ```cpp + // trace_api::StartSpanOptions options; + // opentelemetry::context::Context root; + // root = root.SetValue(kIsRootSpanKey, true); + // options.parent = root; + // auto root_span = tracer->StartSpan("span root", options); + // ``` // // - If the `parent` field is not set, the newly created Span will inherit the // parent of the currently active Span (if any) in the current context. diff --git a/sdk/test/trace/tracer_test.cc b/sdk/test/trace/tracer_test.cc index fcc674aa42..61ad24be1d 100644 --- a/sdk/test/trace/tracer_test.cc +++ b/sdk/test/trace/tracer_test.cc @@ -1014,7 +1014,7 @@ TEST(Tracer, WithActiveSpan) { trace_api::StartSpanOptions options; opentelemetry::context::Context c1; - c1 = c1.SetValue("is_root_span", true); + c1 = c1.SetValue(opentelemetry::trace::kIsRootSpanKey, true); options.parent = c1; auto root_span = tracer->StartSpan("span root", options);