Skip to content

Commit

Permalink
Add SpanContext (and TraceState) to Recordable (open-telemetry#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyjohn authored Apr 13, 2021
1 parent 3ce3e76 commit aeb8230
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 92 deletions.
4 changes: 3 additions & 1 deletion api/include/opentelemetry/common/kv_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "opentelemetry/common/key_value_iterable_view.h"
#include "opentelemetry/common/string_util.h"
#include "opentelemetry/nostd/function_ref.h"
Expand Down Expand Up @@ -267,4 +269,4 @@ class KeyValueProperties
size_t Size() const noexcept { return num_entries_; }
};
} // namespace common
OPENTELEMETRY_END_NAMESPACE
OPENTELEMETRY_END_NAMESPACE
1 change: 1 addition & 0 deletions exporters/ostream/src/span_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ sdk::common::ExportResult OStreamSpanExporter::Export(
<< "\n name : " << span->GetName()
<< "\n trace_id : " << std::string(trace_id, 32)
<< "\n span_id : " << std::string(span_id, 16)
<< "\n tracestate : " << span->GetSpanContext().trace_state()->ToHeader()
<< "\n parent_span_id: " << std::string(parent_span_id, 16)
<< "\n start : " << span->GetStartTime().time_since_epoch().count()
<< "\n duration : " << span->GetDuration().count()
Expand Down
29 changes: 20 additions & 9 deletions exporters/ostream/test/ostream_span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ constexpr const char *kDefaultSpanPrinted =
" name : \n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" tracestate : \n"
" parent_span_id: 0000000000000000\n"
" start : 0\n"
" duration : 0\n"
Expand Down Expand Up @@ -85,16 +86,21 @@ TEST(OStreamSpanExporter, PrintSpanWithBasicFields)

auto recordable = processor->MakeRecordable();

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("state1", "value");
const opentelemetry::trace::SpanContext span_context{
trace_id, span_id,
opentelemetry::trace::TraceFlags{opentelemetry::trace::TraceFlags::kIsSampled}, true,
trace_state};

recordable->SetIdentity(span_context, parent_span_id);
recordable->SetName("Test Span");
opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now());

constexpr uint8_t trace_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::TraceId t_id(trace_id_buf);
constexpr uint8_t span_id_buf[] = {1, 2, 3, 4, 5, 6, 7, 8};
opentelemetry::trace::SpanId s_id(span_id_buf);

recordable->SetIds(t_id, s_id, s_id);

recordable->SetStartTime(now);
recordable->SetDuration(std::chrono::nanoseconds(100));
recordable->SetStatus(opentelemetry::trace::StatusCode::kOk, "Test Description");
Expand All @@ -109,7 +115,8 @@ TEST(OStreamSpanExporter, PrintSpanWithBasicFields)
" name : Test Span\n"
" trace_id : 01020304050607080102030405060708\n"
" span_id : 0102030405060708\n"
" parent_span_id: 0102030405060708\n"
" tracestate : state1=value\n"
" parent_span_id: 0807060504030201\n"
" start : " +
start +
"\n"
Expand Down Expand Up @@ -143,6 +150,7 @@ TEST(OStreamSpanExporter, PrintSpanWithAttribute)
" name : \n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" tracestate : \n"
" parent_span_id: 0000000000000000\n"
" start : 0\n"
" duration : 0\n"
Expand Down Expand Up @@ -178,6 +186,7 @@ TEST(OStreamSpanExporter, PrintSpanWithArrayAttribute)
" name : \n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" tracestate : \n"
" parent_span_id: 0000000000000000\n"
" start : 0\n"
" duration : 0\n"
Expand Down Expand Up @@ -219,6 +228,7 @@ TEST(OStreamSpanExporter, PrintSpanWithEvents)
" name : \n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" tracestate : \n"
" parent_span_id: 0000000000000000\n"
" start : 0\n"
" duration : 0\n"
Expand Down Expand Up @@ -291,6 +301,7 @@ TEST(OStreamSpanExporter, PrintSpanWithLinks)
" name : \n"
" trace_id : 00000000000000000000000000000000\n"
" span_id : 0000000000000000\n"
" tracestate : \n"
" parent_span_id: 0000000000000000\n"
" start : 0\n"
" duration : 0\n"
Expand Down
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
32 changes: 19 additions & 13 deletions ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
opentelemetry::trace::TraceId GetTraceId() const noexcept
{
std::lock_guard<std::mutex> lock(mutex_);
return trace_id_;
return span_context_.trace_id();
}

/**
Expand All @@ -43,7 +43,17 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
opentelemetry::trace::SpanId GetSpanId() const noexcept
{
std::lock_guard<std::mutex> lock(mutex_);
return span_id_;
return span_context_.span_id();
}

/**
* Get the span context for this span
* @return the span context for this span
*/
const opentelemetry::trace::SpanContext &GetSpanContext() const noexcept
{
std::lock_guard<std::mutex> lock(mutex_);
return span_context_;
}

/**
Expand Down Expand Up @@ -110,20 +120,18 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
* Get the attributes for this span
* @return the attributes for this span
*/
const std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue>
GetAttributes() const noexcept
std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> GetAttributes()
const noexcept
{
std::lock_guard<std::mutex> lock(mutex_);
return attributes_;
}

void SetIds(opentelemetry::trace::TraceId trace_id,
opentelemetry::trace::SpanId span_id,
opentelemetry::trace::SpanId parent_span_id) noexcept override
void SetIdentity(const opentelemetry::trace::SpanContext &span_context,
opentelemetry::trace::SpanId parent_span_id) noexcept override
{
std::lock_guard<std::mutex> lock(mutex_);
trace_id_ = trace_id;
span_id_ = span_id;
span_context_ = span_context;
parent_span_id_ = parent_span_id;
}

Expand Down Expand Up @@ -195,8 +203,7 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
private:
ThreadsafeSpanData(const ThreadsafeSpanData &threadsafe_span_data,
const std::lock_guard<std::mutex> &)
: trace_id_(threadsafe_span_data.trace_id_),
span_id_(threadsafe_span_data.span_id_),
: span_context_(threadsafe_span_data.span_context_),
parent_span_id_(threadsafe_span_data.parent_span_id_),
start_time_(threadsafe_span_data.start_time_),
duration_(threadsafe_span_data.duration_),
Expand All @@ -209,8 +216,7 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
{}

mutable std::mutex mutex_;
opentelemetry::trace::TraceId trace_id_;
opentelemetry::trace::SpanId span_id_;
opentelemetry::trace::SpanContext span_context_{false, false};
opentelemetry::trace::SpanId parent_span_id_;
core::SystemTimestamp start_time_;
std::chrono::nanoseconds duration_{0};
Expand Down
32 changes: 23 additions & 9 deletions ext/test/zpages/threadsafe_span_data_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ using opentelemetry::sdk::common::OwnedAttributeValue;

TEST(ThreadsafeSpanData, DefaultValues)
{
opentelemetry::trace::TraceId zero_trace_id;
opentelemetry::trace::SpanContext empty_span_context{false, false};
opentelemetry::trace::SpanId zero_span_id;
ThreadsafeSpanData data;

ASSERT_EQ(data.GetTraceId(), zero_trace_id);
ASSERT_EQ(data.GetSpanId(), zero_span_id);
ASSERT_EQ(data.GetTraceId(), empty_span_context.trace_id());
ASSERT_EQ(data.GetSpanId(), empty_span_context.span_id());
ASSERT_EQ(data.GetSpanContext(), empty_span_context);
ASSERT_EQ(data.GetParentSpanId(), zero_span_id);
ASSERT_EQ(data.GetName(), "");
ASSERT_EQ(data.GetStatus(), opentelemetry::trace::StatusCode::kUnset);
Expand All @@ -29,27 +30,40 @@ TEST(ThreadsafeSpanData, DefaultValues)

TEST(ThreadsafeSpanData, Set)
{
opentelemetry::trace::TraceId trace_id;
opentelemetry::trace::SpanId span_id;
opentelemetry::trace::SpanId parent_span_id;
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};
opentelemetry::core::SystemTimestamp now(std::chrono::system_clock::now());

ThreadsafeSpanData data;
data.SetIds(trace_id, span_id, parent_span_id);
data.SetIdentity(span_context, parent_span_id);
data.SetName("span name");
data.SetSpanKind(opentelemetry::trace::SpanKind::kServer);
data.SetStatus(opentelemetry::trace::StatusCode::kOk, "description");
data.SetStartTime(now);
data.SetDuration(std::chrono::nanoseconds(1000000));
data.SetAttribute("attr1", 314159);
data.SetAttribute("attr1", (int64_t)314159);
data.AddEvent("event1", now);

ASSERT_EQ(data.GetTraceId(), trace_id);
ASSERT_EQ(data.GetSpanId(), span_id);
ASSERT_EQ(data.GetSpanContext(), span_context);
std::string trace_state_key1_value;
ASSERT_EQ(data.GetSpanContext().trace_state()->Get("key1", trace_state_key1_value), true);
ASSERT_EQ(trace_state_key1_value, "value");
ASSERT_EQ(data.GetParentSpanId(), parent_span_id);
ASSERT_EQ(data.GetName(), "span name");
ASSERT_EQ(data.GetStatus(), opentelemetry::trace::StatusCode::kOk);
ASSERT_EQ(data.GetDescription(), "description");
ASSERT_EQ(data.GetStartTime().time_since_epoch(), now.time_since_epoch());
ASSERT_EQ(data.GetDuration(), std::chrono::nanoseconds(1000000));
ASSERT_EQ(opentelemetry::nostd::get<int>(data.GetAttributes().at("attr1")), 314159);
ASSERT_EQ(opentelemetry::nostd::get<int64_t>(data.GetAttributes().at("attr1")), 314159);
}
Loading

0 comments on commit aeb8230

Please sign in to comment.