From 9ea96728bfce9dc5dd22340908018bc83e30ca5e Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Wed, 22 Jul 2020 21:45:18 +0000 Subject: [PATCH 1/4] Add helper function to populate attributes --- exporters/otlp/src/recordable.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/exporters/otlp/src/recordable.cc b/exporters/otlp/src/recordable.cc index c721313ce6..ae8f51dd21 100644 --- a/exporters/otlp/src/recordable.cc +++ b/exporters/otlp/src/recordable.cc @@ -18,8 +18,9 @@ void Recordable::SetIds(trace::TraceId trace_id, trace::SpanId::kSize); } -void Recordable::SetAttribute(nostd::string_view key, - const opentelemetry::common::AttributeValue &value) noexcept +void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute, + nostd::string_view key, + const opentelemetry::common::AttributeValue &value) { // Assert size of variant to ensure that this method gets updated if the variant // definition changes @@ -27,7 +28,6 @@ void Recordable::SetAttribute(nostd::string_view key, nostd::variant_size::value == kAttributeValueSize, "AttributeValue contains unknown type"); - auto *attribute = span_.add_attributes(); attribute->set_key(key.data(), key.size()); if (nostd::holds_alternative(value)) @@ -111,6 +111,13 @@ void Recordable::SetAttribute(nostd::string_view key, } } +void Recordable::SetAttribute(nostd::string_view key, + const opentelemetry::common::AttributeValue &value) noexcept +{ + auto *attribute = span_.add_attributes(); + PopulateAttribute(attribute, key, value); +} + void Recordable::AddEvent(nostd::string_view name, core::SystemTimestamp timestamp, const trace::KeyValueIterable &attributes) noexcept From 5194e864e22838ffc70d19e763ea7d0f2134fcc6 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Thu, 23 Jul 2020 23:02:20 +0000 Subject: [PATCH 2/4] Populate attributes --- exporters/otlp/src/recordable.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/src/recordable.cc b/exporters/otlp/src/recordable.cc index ae8f51dd21..c86b3d2a77 100644 --- a/exporters/otlp/src/recordable.cc +++ b/exporters/otlp/src/recordable.cc @@ -125,7 +125,11 @@ void Recordable::AddEvent(nostd::string_view name, auto *event = span_.add_events(); event->set_name(name.data(), name.size()); event->set_time_unix_nano(timestamp.time_since_epoch().count()); - // TODO: handle attributes + + attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept { + PopulateAttribute(event->add_attributes(), key, value); + return true; + }); } void Recordable::AddLink(opentelemetry::trace::SpanContext span_context, From dd57708eb6078ddebcd703b8acc31813559ab3c6 Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 24 Jul 2020 16:30:20 +0000 Subject: [PATCH 3/4] Add links implementation --- exporters/otlp/src/recordable.cc | 8 ++++++-- exporters/otlp/test/recordable_test.cc | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/exporters/otlp/src/recordable.cc b/exporters/otlp/src/recordable.cc index c86b3d2a77..4d19e3ecd1 100644 --- a/exporters/otlp/src/recordable.cc +++ b/exporters/otlp/src/recordable.cc @@ -135,8 +135,12 @@ void Recordable::AddEvent(nostd::string_view name, void Recordable::AddLink(opentelemetry::trace::SpanContext span_context, const trace::KeyValueIterable &attributes) noexcept { - (void)span_context; - (void)attributes; + auto *link = span_.add_links(); + attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept { + PopulateAttribute(link->add_attributes(), key, value); + return true; + }); + // TODO: Populate trace_id, span_id and trace_state when these are supported by SpanContext } void Recordable::SetStatus(trace::CanonicalCode code, nostd::string_view description) noexcept diff --git a/exporters/otlp/test/recordable_test.cc b/exporters/otlp/test/recordable_test.cc index ec1662662d..a1e42c091c 100644 --- a/exporters/otlp/test/recordable_test.cc +++ b/exporters/otlp/test/recordable_test.cc @@ -78,7 +78,7 @@ TEST(Recordable, SetStatus) EXPECT_EQ(rec.span().status().message(), description); } -TEST(Recordable, AddEvents) +TEST(Recordable, AddEventsWithDefaults) { Recordable rec; nostd::string_view name = "Test Event"; @@ -95,6 +95,22 @@ TEST(Recordable, AddEvents) EXPECT_EQ(rec.span().events(0).time_unix_nano(), unix_event_time); } +TEST(Recordable, AddEventsWithAttributes) +{ + Recordable rec; + const int kNumAttributes = 2; + + std::map attributes = {{"attr1", 4}, {"attr2", 7}}; + + rec.AddEvent("Test Event", std::chrono::system_clock::now(), + trace::KeyValueIterableView>(attributes)); + + EXPECT_EQ(rec.span().events(0).attributes(0).key(), "attr1"); + EXPECT_EQ(rec.span().events(0).attributes(1).key(), "attr2"); + EXPECT_EQ(rec.span().events(0).attributes(0).value().int_value(), 4); + EXPECT_EQ(rec.span().events(0).attributes(1).value().int_value(), 7); +} + // Test non-int single types. Int single types are tested using templates (see IntAttributeTest) TEST(Recordable, SetSingleAtrribute) { From f54b686dda38efb8d12ad11346e2d78c584e0f6c Mon Sep 17 00:00:00 2001 From: Nadia Ciobanu Date: Fri, 24 Jul 2020 23:23:40 +0000 Subject: [PATCH 4/4] Add test for links --- exporters/otlp/test/recordable_test.cc | 41 ++++++++++++++++++++------ 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/exporters/otlp/test/recordable_test.cc b/exporters/otlp/test/recordable_test.cc index a1e42c091c..5a6eec8c3a 100644 --- a/exporters/otlp/test/recordable_test.cc +++ b/exporters/otlp/test/recordable_test.cc @@ -78,7 +78,7 @@ TEST(Recordable, SetStatus) EXPECT_EQ(rec.span().status().message(), description); } -TEST(Recordable, AddEventsWithDefaults) +TEST(Recordable, AddEventDefault) { Recordable rec; nostd::string_view name = "Test Event"; @@ -93,22 +93,45 @@ TEST(Recordable, AddEventsWithDefaults) EXPECT_EQ(rec.span().events(0).name(), name); EXPECT_EQ(rec.span().events(0).time_unix_nano(), unix_event_time); + EXPECT_EQ(rec.span().events(0).attributes().size(), 0); } -TEST(Recordable, AddEventsWithAttributes) +TEST(Recordable, AddEventWithAttributes) { Recordable rec; - const int kNumAttributes = 2; - - std::map attributes = {{"attr1", 4}, {"attr2", 7}}; + const int kNumAttributes = 3; + std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3"}; + int values[kNumAttributes] = {4, 7, 23}; + std::map attributes = { + {keys[0], values[0]}, {keys[1], values[1]}, {keys[2], values[2]}}; rec.AddEvent("Test Event", std::chrono::system_clock::now(), trace::KeyValueIterableView>(attributes)); - EXPECT_EQ(rec.span().events(0).attributes(0).key(), "attr1"); - EXPECT_EQ(rec.span().events(0).attributes(1).key(), "attr2"); - EXPECT_EQ(rec.span().events(0).attributes(0).value().int_value(), 4); - EXPECT_EQ(rec.span().events(0).attributes(1).value().int_value(), 7); + for (int i = 0; i < kNumAttributes; i++) + { + EXPECT_EQ(rec.span().events(0).attributes(i).key(), keys[i]); + EXPECT_EQ(rec.span().events(0).attributes(i).value().int_value(), values[i]); + } +} + +TEST(Recordable, AddLink) +{ + Recordable rec; + const int kNumAttributes = 3; + std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3"}; + int values[kNumAttributes] = {5, 12, 40}; + std::map attributes = { + {keys[0], values[0]}, {keys[1], values[1]}, {keys[2], values[2]}}; + + rec.AddLink(trace::SpanContext(false, false), + trace::KeyValueIterableView>(attributes)); + + for (int i = 0; i < kNumAttributes; i++) + { + EXPECT_EQ(rec.span().links(0).attributes(i).key(), keys[i]); + EXPECT_EQ(rec.span().links(0).attributes(i).value().int_value(), values[i]); + } } // Test non-int single types. Int single types are tested using templates (see IntAttributeTest)