Skip to content

Commit

Permalink
Additional Recordable::SetIdentity users
Browse files Browse the repository at this point in the history
- Added SetIdentity to otlp::Recordable (with tracestate)
- Added SetIdentity to Zipkin (without tracestate support)
- Fixed minor formatting issue
  • Loading branch information
eyjohn committed Apr 10, 2021
1 parent ca5887c commit 7126da9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ class Recordable final : public sdk::trace::Recordable
public:
const proto::trace::v1::Span &span() const noexcept { return span_; }

void SetIds(trace::TraceId trace_id,
trace::SpanId span_id,
trace::SpanId parent_span_id) noexcept override;
void SetIdentity(const opentelemetry::trace::SpanContext &span_context,
opentelemetry::trace::SpanId parent_span_id) noexcept override;

void SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept override;
Expand Down
12 changes: 7 additions & 5 deletions exporters/otlp/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ const int kAttributeValueSize = 15;
const int kAttributeValueSize = 14;
#endif

void Recordable::SetIds(trace::TraceId trace_id,
trace::SpanId span_id,
trace::SpanId parent_span_id) noexcept
void Recordable::SetIdentity(const opentelemetry::trace::SpanContext &span_context,
opentelemetry::trace::SpanId parent_span_id) noexcept
{
span_.set_trace_id(reinterpret_cast<const char *>(trace_id.Id().data()), trace::TraceId::kSize);
span_.set_span_id(reinterpret_cast<const char *>(span_id.Id().data()), trace::SpanId::kSize);
span_.set_trace_id(reinterpret_cast<const char *>(span_context.trace_id().Id().data()),
trace::TraceId::kSize);
span_.set_span_id(reinterpret_cast<const char *>(span_context.span_id().Id().data()),
trace::SpanId::kSize);
span_.set_parent_span_id(reinterpret_cast<const char *>(parent_span_id.Id().data()),
trace::SpanId::kSize);
span_.set_trace_state(span_context.trace_state()->ToHeader());
}

void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
Expand Down
9 changes: 7 additions & 2 deletions exporters/otlp/test/otlp_exporter_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const trace::SpanId kSpanId(std::array<const uint8_t, trace::SpanId::kSize>({0,
2}));
const trace::SpanId kParentSpanId(std::array<const uint8_t, trace::SpanId::kSize>({0, 0, 0, 0, 0, 0,
0, 3}));
const auto kTraceState = opentelemetry::trace::TraceState::GetDefault() -> Set("key1", "value");
const opentelemetry::trace::SpanContext kSpanContext{
kTraceId, kSpanId,
opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, true,
kTraceState};

// ----------------------- Helper classes and functions ------------------------

Expand Down Expand Up @@ -80,7 +85,7 @@ void CreateSparseSpans(std::array<std::unique_ptr<sdk::trace::Recordable>, kBatc
{
auto recordable = std::unique_ptr<sdk::trace::Recordable>(new Recordable);

recordable->SetIds(kTraceId, kSpanId, kParentSpanId);
recordable->SetIdentity(kSpanContext, kParentSpanId);
recordable->SetName("TestSpan");
recordable->SetStartTime(core::SystemTimestamp(std::chrono::system_clock::now()));
recordable->SetDuration(std::chrono::nanoseconds(10));
Expand All @@ -96,7 +101,7 @@ void CreateDenseSpans(std::array<std::unique_ptr<sdk::trace::Recordable>, kBatch
{
auto recordable = std::unique_ptr<sdk::trace::Recordable>(new Recordable);

recordable->SetIds(kTraceId, kSpanId, kParentSpanId);
recordable->SetIdentity(kSpanContext, kParentSpanId);
recordable->SetName("TestSpan");
recordable->SetStartTime(core::SystemTimestamp(std::chrono::system_clock::now()));
recordable->SetDuration(std::chrono::nanoseconds(10));
Expand Down
26 changes: 15 additions & 11 deletions exporters/otlp/test/recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ namespace exporter
{
namespace otlp
{
TEST(Recordable, SetIds)
{
const trace::TraceId trace_id(std::array<const uint8_t, trace::TraceId::kSize>(
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}));

const trace::SpanId span_id(
std::array<const uint8_t, trace::SpanId::kSize>({0, 0, 0, 0, 0, 0, 0, 2}));

const trace::SpanId parent_span_id(
std::array<const uint8_t, trace::SpanId::kSize>({0, 0, 0, 0, 0, 0, 0, 3}));
TEST(Recordable, SetIdentity)
{
constexpr uint8_t trace_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
constexpr uint8_t span_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
constexpr uint8_t parent_span_id_buf[] = {8, 7, 6, 5, 4, 3, 2, 1};
opentelemetry::trace::TraceId trace_id{trace_id_buf};
opentelemetry::trace::SpanId span_id{span_id_buf};
opentelemetry::trace::SpanId parent_span_id{parent_span_id_buf};
const auto trace_state = opentelemetry::trace::TraceState::GetDefault()->Set("key1", "value");
const opentelemetry::trace::SpanContext span_context{
trace_id, span_id,
opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, true,
trace_state};

Recordable rec;

rec.SetIds(trace_id, span_id, parent_span_id);
rec.SetIdentity(span_context, parent_span_id);

EXPECT_EQ(rec.span().trace_id(), std::string(reinterpret_cast<const char *>(trace_id.Id().data()),
trace::TraceId::kSize));
EXPECT_EQ(rec.span().span_id(),
std::string(reinterpret_cast<const char *>(span_id.Id().data()), trace::SpanId::kSize));
EXPECT_EQ(rec.span().trace_state(), "key1=value");
EXPECT_EQ(rec.span().parent_span_id(),
std::string(reinterpret_cast<const char *>(parent_span_id.Id().data()),
trace::SpanId::kSize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class Recordable final : public sdk::trace::Recordable
public:
const ZipkinSpan &span() const noexcept { return span_; }

void SetIds(trace::TraceId trace_id,
trace::SpanId span_id,
trace::SpanId parent_span_id) noexcept override;
void SetIdentity(const opentelemetry::trace::SpanContext &span_context,
opentelemetry::trace::SpanId parent_span_id) noexcept override;

void SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept override;
Expand Down
9 changes: 4 additions & 5 deletions exporters/zipkin/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ const int kAttributeValueSize = 15;
const int kAttributeValueSize = 14;
#endif

void Recordable::SetIds(trace::TraceId trace_id,
trace::SpanId span_id,
trace::SpanId parent_span_id) noexcept
void Recordable::SetIdentity(const opentelemetry::trace::SpanContext &span_context,
opentelemetry::trace::SpanId parent_span_id) noexcept
{
char trace_id_lower_base16[trace::TraceId::kSize * 2] = {0};
trace_id.ToLowerBase16(trace_id_lower_base16);
span_context.trace_id().ToLowerBase16(trace_id_lower_base16);
char span_id_lower_base16[trace::SpanId::kSize * 2] = {0};
span_id.ToLowerBase16(span_id_lower_base16);
span_context.span_id().ToLowerBase16(span_id_lower_base16);
char parent_span_id_lower_base16[trace::SpanId::kSize * 2] = {0};
parent_span_id.ToLowerBase16(parent_span_id_lower_base16);
span_["id"] = std::string(span_id_lower_base16, 16);
Expand Down
8 changes: 6 additions & 2 deletions exporters/zipkin/test/zipkin_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace sdktrace = opentelemetry::sdk::trace;
using json = nlohmann::json;

// Testing Shutdown functionality of OStreamSpanExporter, should expect no data to be sent to Stream
TEST(ZipkinSpanRecordable, SetIds)
TEST(ZipkinSpanRecordable, SetIdentity)
{
json j_span = {{"id", "0000000000000002"},
{"parentId", "0000000000000003"},
Expand All @@ -50,7 +50,11 @@ TEST(ZipkinSpanRecordable, SetIds)
const trace::SpanId parent_span_id(
std::array<const uint8_t, trace::SpanId::kSize>({0, 0, 0, 0, 0, 0, 0, 3}));

rec.SetIds(trace_id, span_id, parent_span_id);
const opentelemetry::trace::SpanContext span_context{
trace_id, span_id,
opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, true};

rec.SetIdentity(span_context, parent_span_id);
EXPECT_EQ(rec.span(), j_span);
}

Expand Down
6 changes: 3 additions & 3 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
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()));
trace_state ? trace_state
: is_parent_span_valid ? parent_span_context.trace_state()
: trace_api::TraceState::GetDefault()));

recordable_->SetIdentity(*span_context_, parent_span_id);

Expand Down

0 comments on commit 7126da9

Please sign in to comment.