Skip to content

Commit

Permalink
Merge branch 'main' into jaeger-propagator
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Mar 12, 2021
2 parents 1dd50c5 + b80cc91 commit 907bf23
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 26 deletions.
7 changes: 7 additions & 0 deletions docs/public/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"breathe",
]

breathe_projects = {
"OpenTelemetry C++ API": "../../api/docs/doxyoutput/xml"
}

breathe_default_project = "OpenTelemetry C++ API"

primary_domain = "cpp"

higlight_language = "cpp"
Expand Down
3 changes: 2 additions & 1 deletion exporters/zipkin/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ void Recordable::SetName(nostd::string_view name) noexcept

void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept
{
span_["timestamp"] = start_time.time_since_epoch().count();
span_["timestamp"] =
std::chrono::duration_cast<std::chrono::microseconds>(start_time.time_since_epoch()).count();
}

void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept
Expand Down
5 changes: 4 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_context_kv_iterable.h"
#include "opentelemetry/trace/trace_id.h"
#include "opentelemetry/trace/trace_state.h"
#include "opentelemetry/version.h"

#include <map>
Expand Down Expand Up @@ -41,6 +42,8 @@ struct SamplingResult
Decision decision;
// A set of span Attributes that will also be added to the Span. Can be nullptr.
std::unique_ptr<const std::map<std::string, opentelemetry::common::AttributeValue>> attributes;
// The tracestate used by the span.
nostd::shared_ptr<opentelemetry::trace::TraceState> trace_state;
};

/**
Expand All @@ -61,7 +64,7 @@ class Sampler
* @param name the name of the new Span.
* @param spanKind the trace_api::SpanKind of the Span.
* @param attributes list of AttributeValue with their keys.
* @param links TODO: Collection of links that will be associated with the Span to be created.
* @param links Collection of links that will be associated with the Span to be created.
* @return sampling result whether span should be sampled or not.
* @since 0.1.0
*/
Expand Down
11 changes: 9 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/samplers/always_off.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ class AlwaysOffSampler : public Sampler
* @return Returns DROP always
*/
SamplingResult ShouldSample(
const trace_api::SpanContext & /*parent_context*/,
const trace_api::SpanContext &parent_context,
trace_api::TraceId /*trace_id*/,
nostd::string_view /*name*/,
trace_api::SpanKind /*span_kind*/,
const opentelemetry::common::KeyValueIterable & /*attributes*/,
const trace_api::SpanContextKeyValueIterable & /*links*/) noexcept override
{
return {Decision::DROP, nullptr};
if (!parent_context.IsValid())
{
return {Decision::DROP, nullptr, opentelemetry::trace::TraceState::GetDefault()};
}
else
{
return {Decision::DROP, nullptr, parent_context.trace_state()};
}
}

/**
Expand Down
11 changes: 9 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/samplers/always_on.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ class AlwaysOnSampler : public Sampler
* @return Always return Decision RECORD_AND_SAMPLE
*/
inline SamplingResult ShouldSample(
const trace_api::SpanContext & /*parent_context*/,
const trace_api::SpanContext &parent_context,
trace_api::TraceId /*trace_id*/,
nostd::string_view /*name*/,
trace_api::SpanKind /*span_kind*/,
const opentelemetry::common::KeyValueIterable & /*attributes*/,
const trace_api::SpanContextKeyValueIterable & /*links*/) noexcept override
{
return {Decision::RECORD_AND_SAMPLE, nullptr};
if (!parent_context.IsValid())
{
return {Decision::RECORD_AND_SAMPLE, nullptr, opentelemetry::trace::TraceState::GetDefault()};
}
else
{
return {Decision::RECORD_AND_SAMPLE, nullptr, parent_context.trace_state()};
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/trace/samplers/parent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ SamplingResult ParentBasedSampler::ShouldSample(
// If parent exists:
if (parent_context.IsSampled())
{
return {Decision::RECORD_AND_SAMPLE, nullptr};
return {Decision::RECORD_AND_SAMPLE, nullptr, parent_context.trace_state()};
}

return {Decision::DROP, nullptr};
return {Decision::DROP, nullptr, parent_context.trace_state()};
}

nostd::string_view ParentBasedSampler::GetDescription() const noexcept
Expand Down
15 changes: 10 additions & 5 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
const trace_api::SpanContextKeyValueIterable &links,
const trace_api::StartSpanOptions &options,
const trace_api::SpanContext &parent_span_context,
const opentelemetry::sdk::resource::Resource &resource) noexcept
const opentelemetry::sdk::resource::Resource &resource,
const nostd::shared_ptr<opentelemetry::trace::TraceState> trace_state,
const bool sampled) noexcept
: tracer_{std::move(tracer)},
processor_{processor},
recordable_{processor_->MakeRecordable()},
Expand Down Expand Up @@ -93,10 +95,13 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
recordable_->SetIds(trace_id, span_id, trace_api::SpanId());
}

span_context_ = std::unique_ptr<trace_api::SpanContext>(
new trace_api::SpanContext(trace_id, span_id, trace_api::TraceFlags(), false,
is_parent_span_valid ? parent_span_context.trace_state()
: trace_api::TraceState::GetDefault()));
span_context_ = std::unique_ptr<trace_api::SpanContext>(new trace_api::SpanContext(
trace_id, span_id,
sampled ? trace_api::TraceFlags{trace_api::TraceFlags::kIsSampled} : trace_api::TraceFlags{},
false,
trace_state ? trace_state
: is_parent_span_valid ? parent_span_context.trace_state()
: trace_api::TraceState::GetDefault()));

attributes.ForEachKeyValue(
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
Expand Down
19 changes: 11 additions & 8 deletions sdk/src/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ namespace trace_api = opentelemetry::trace;
class Span final : public trace_api::Span
{
public:
explicit Span(std::shared_ptr<Tracer> &&tracer,
std::shared_ptr<SpanProcessor> processor,
nostd::string_view name,
const opentelemetry::common::KeyValueIterable &attributes,
const trace_api::SpanContextKeyValueIterable &links,
const trace_api::StartSpanOptions &options,
const trace_api::SpanContext &parent_span_context,
const opentelemetry::sdk::resource::Resource &resource) noexcept;
Span(std::shared_ptr<Tracer> &&tracer,
std::shared_ptr<SpanProcessor> processor,
nostd::string_view name,
const opentelemetry::common::KeyValueIterable &attributes,
const trace_api::SpanContextKeyValueIterable &links,
const trace_api::StartSpanOptions &options,
const trace_api::SpanContext &parent_span_context,
const opentelemetry::sdk::resource::Resource &resource,
const nostd::shared_ptr<opentelemetry::trace::TraceState> trace_state =
trace_api::TraceState::GetDefault(),
const bool sampled = false) noexcept;

~Span() override;

Expand Down
6 changes: 3 additions & 3 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ nostd::shared_ptr<trace_api::Span> Tracer::StartSpan(
}
else
{
auto span = nostd::shared_ptr<trace_api::Span>{
new (std::nothrow) Span{this->shared_from_this(), processor_.load(), name, attributes,
links, options, parent, resource_}};
auto span = nostd::shared_ptr<trace_api::Span>{new (std::nothrow) Span{
this->shared_from_this(), processor_.load(), name, attributes, links, options, parent,
resource_, sampling_result.trace_state, true}};

// if the attributes is not nullptr, add attributes to the span.
if (sampling_result.attributes)
Expand Down
1 change: 1 addition & 0 deletions sdk/test/trace/always_off_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ TEST(AlwaysOffSampler, ShouldSample)

ASSERT_EQ(Decision::DROP, sampling_result.decision);
ASSERT_EQ(nullptr, sampling_result.attributes);
ASSERT_EQ("", sampling_result.trace_state->ToHeader());
}

TEST(AlwaysOffSampler, GetDescription)
Expand Down
2 changes: 2 additions & 0 deletions sdk/test/trace/always_on_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ TEST(AlwaysOnSampler, ShouldSample)

ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision);
ASSERT_EQ(nullptr, sampling_result.attributes);
ASSERT_EQ("", sampling_result.trace_state->ToHeader());

// Test with a valid trace id and empty parent context
sampling_result = sampler.ShouldSample(
Expand All @@ -44,6 +45,7 @@ TEST(AlwaysOnSampler, ShouldSample)

ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result.decision);
ASSERT_EQ(nullptr, sampling_result.attributes);
ASSERT_EQ("", sampling_result.trace_state->ToHeader());
}

TEST(AlwaysOnSampler, GetDescription)
Expand Down
10 changes: 8 additions & 2 deletions sdk/test/trace/parent_sampler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ TEST(ParentBasedSampler, ShouldSample)

opentelemetry::common::KeyValueIterableView<M> view{m1};
trace_api::SpanContextKeyValueIterableView<L> links{l1};
trace_api::SpanContext parent_context_sampled(trace_id, span_id, trace_api::TraceFlags{1}, false);
auto trace_state = opentelemetry::trace::TraceState::FromHeader("congo=t61rcWkgMzE");
trace_api::SpanContext parent_context_sampled(trace_id, span_id, trace_api::TraceFlags{1}, false,
trace_state);
trace_api::SpanContext parent_context_nonsampled(trace_id, span_id, trace_api::TraceFlags{0},
false);
false, trace_state);

// Case 1: Parent doesn't exist. Return result of delegateSampler()
auto sampling_result = sampler_off.ShouldSample(trace_api::SpanContext::GetInvalid(), trace_id,
Expand All @@ -43,16 +45,20 @@ TEST(ParentBasedSampler, ShouldSample)

ASSERT_EQ(Decision::DROP, sampling_result.decision);
ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result2.decision);
ASSERT_EQ("", sampling_result.trace_state->ToHeader());
ASSERT_EQ("", sampling_result2.trace_state->ToHeader());

// Case 2: Parent exists and SampledFlag is true
auto sampling_result3 =
sampler_off.ShouldSample(parent_context_sampled, trace_id, "", span_kind, view, links);
ASSERT_EQ(Decision::RECORD_AND_SAMPLE, sampling_result3.decision);
ASSERT_EQ("congo=t61rcWkgMzE", sampling_result3.trace_state->ToHeader());

// Case 3: Parent exists and SampledFlag is false
auto sampling_result4 =
sampler_on.ShouldSample(parent_context_nonsampled, trace_id, "", span_kind, view, links);
ASSERT_EQ(Decision::DROP, sampling_result4.decision);
ASSERT_EQ("congo=t61rcWkgMzE", sampling_result4.trace_state->ToHeader());
}

TEST(ParentBasedSampler, GetDescription)
Expand Down
49 changes: 49 additions & 0 deletions sdk/test/trace/tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using opentelemetry::common::KeyValueIterableView;
using opentelemetry::exporter::memory::InMemorySpanData;
using opentelemetry::exporter::memory::InMemorySpanExporter;
using opentelemetry::trace::SpanContext;
using opentelemetry::trace::TraceFlags;

/**
* A mock sampler that returns non-empty sampling results attributes.
Expand Down Expand Up @@ -563,3 +564,51 @@ TEST(Tracer, ExpectParent)
EXPECT_EQ(spandata_first->GetSpanId(), spandata_second->GetParentSpanId());
EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId());
}

TEST(Tracer, ExpectParentWithValidSpanContext)
{
std::unique_ptr<InMemorySpanExporter> exporter(new InMemorySpanExporter());
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
auto tracer = initTracer(std::move(exporter));
auto spans = span_data.get()->GetSpans();

ASSERT_EQ(0, spans.size());

// produce valid SpanContext with pseudo span and trace Id.
uint8_t span_id_buf[trace_api::SpanId::kSize] = {
1,
};
trace_api::SpanId span_id{span_id_buf};
uint8_t trace_id_buf[trace_api::TraceId::kSize] = {
2,
};
trace_api::TraceId trace_id{trace_id_buf};

trace_api::StartSpanOptions options;
options.parent = SpanContext(trace_id, span_id, TraceFlags{TraceFlags::kIsSampled}, true);

auto span_first = tracer->StartSpan("span 1", options);

EXPECT_EQ(span_first->GetContext().trace_flags().IsSampled(), true);

options.parent = span_first->GetContext();
auto span_second = tracer->StartSpan("span 2", options);
EXPECT_EQ(span_second->GetContext().trace_flags().IsSampled(), true);

options.parent = span_second->GetContext();
auto span_third = tracer->StartSpan("span 3", options);
EXPECT_EQ(span_third->GetContext().trace_flags().IsSampled(), true);

span_third->End();
span_second->End();
span_first->End();

spans = span_data->GetSpans();
ASSERT_EQ(3, spans.size());
auto spandata_first = std::move(spans.at(2));
auto spandata_second = std::move(spans.at(1));
auto spandata_third = std::move(spans.at(0));

EXPECT_EQ(spandata_first->GetSpanId(), spandata_second->GetParentSpanId());
EXPECT_EQ(spandata_second->GetSpanId(), spandata_third->GetParentSpanId());
}

0 comments on commit 907bf23

Please sign in to comment.