From 528dd1f24d9cd2808c2774dd3d2505b49472dd09 Mon Sep 17 00:00:00 2001 From: yuhelu Date: Mon, 27 Jun 2022 11:27:40 +0800 Subject: [PATCH 1/2] Fix trace kIsSampled flag set incorrect --- sdk/include/opentelemetry/sdk/trace/sampler.h | 6 ++++++ sdk/src/trace/tracer.cc | 18 +++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/trace/sampler.h b/sdk/include/opentelemetry/sdk/trace/sampler.h index 452ba924b4..799d490e8b 100644 --- a/sdk/include/opentelemetry/sdk/trace/sampler.h +++ b/sdk/include/opentelemetry/sdk/trace/sampler.h @@ -45,6 +45,12 @@ struct SamplingResult std::unique_ptr> attributes; // The tracestate used by the span. nostd::shared_ptr trace_state; + + inline bool IsRecording() + { + return decision == Decision::RECORD_ONLY || decision == Decision::RECORD_AND_SAMPLE; + } + inline bool IsSampled() { return decision == Decision::RECORD_AND_SAMPLE; } }; /** diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index be5e5f7d96..1a75adbde0 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -64,17 +64,17 @@ nostd::shared_ptr Tracer::StartSpan( auto sampling_result = context_->GetSampler().ShouldSample(parent_context, trace_id, name, options.kind, attributes, links); - auto trace_flags = sampling_result.decision == Decision::DROP - ? trace_api::TraceFlags{} - : trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled}; + auto trace_flags = sampling_result.IsSampled() + ? trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled} + : trace_api::TraceFlags{}; - auto span_context = std::unique_ptr(new trace_api::SpanContext( - trace_id, span_id, trace_flags, false, - sampling_result.trace_state ? sampling_result.trace_state - : is_parent_span_valid ? parent_context.trace_state() - : trace_api::TraceState::GetDefault())); + auto span_context = std::unique_ptr( + new trace_api::SpanContext(trace_id, span_id, trace_flags, false, + sampling_result.trace_state ? sampling_result.trace_state + : is_parent_span_valid ? parent_context.trace_state() + : trace_api::TraceState::GetDefault())); - if (sampling_result.decision == Decision::DROP) + if (!sampling_result.IsRecording()) { // create no-op span with valid span-context. From f1a05cf054cd63d6efdaa0f6176d43d04294c9f5 Mon Sep 17 00:00:00 2001 From: yuhelu Date: Tue, 28 Jun 2022 14:11:43 +0800 Subject: [PATCH 2/2] Fix format --- sdk/src/trace/tracer.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sdk/src/trace/tracer.cc b/sdk/src/trace/tracer.cc index 1a75adbde0..370eda49cb 100644 --- a/sdk/src/trace/tracer.cc +++ b/sdk/src/trace/tracer.cc @@ -65,14 +65,14 @@ nostd::shared_ptr Tracer::StartSpan( auto sampling_result = context_->GetSampler().ShouldSample(parent_context, trace_id, name, options.kind, attributes, links); auto trace_flags = sampling_result.IsSampled() - ? trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled} - : trace_api::TraceFlags{}; - - auto span_context = std::unique_ptr( - new trace_api::SpanContext(trace_id, span_id, trace_flags, false, - sampling_result.trace_state ? sampling_result.trace_state - : is_parent_span_valid ? parent_context.trace_state() - : trace_api::TraceState::GetDefault())); + ? trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled} + : trace_api::TraceFlags{}; + + auto span_context = std::unique_ptr(new trace_api::SpanContext( + trace_id, span_id, trace_flags, false, + sampling_result.trace_state ? sampling_result.trace_state + : is_parent_span_valid ? parent_context.trace_state() + : trace_api::TraceState::GetDefault())); if (!sampling_result.IsRecording()) {