Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 1eee992

Browse files
author
Bogdan Drutu
authored
Change initialized list to absl::Span to accept vectors and arrays. (#53)
1 parent bee9be2 commit 1eee992

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

opencensus/trace/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ cc_library(
8787
"@com_google_absl//absl/strings",
8888
"@com_google_absl//absl/synchronization",
8989
"@com_google_absl//absl/time",
90+
"@com_google_absl//absl/types:span",
9091
"//opencensus/common/internal:random_lib",
9192
],
9293
)

opencensus/trace/internal/span_test.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ TEST(SpanTest, AddAttributesLastValueWins) {
9999
data.attributes().at("another_key").string_value());
100100
}
101101

102+
TEST(SpanTest, AddAttributesWithArrayAndVector) {
103+
auto span =
104+
Span::StartSpan("SpanName", /*parent=*/nullptr, {nullptr, kRecordEvents});
105+
std::array<std::pair<absl::string_view, AttributeValueRef>, 3>
106+
attributes_array = {
107+
{{"str_key", "value1"}, {"int_key", 123}, {"bool_key", true}}};
108+
std::vector<std::pair<absl::string_view, AttributeValueRef>>
109+
attributes_vector = {{"another_key", "another_value"}};
110+
span.AddAttributes(attributes_array);
111+
span.AddAttributes(attributes_vector);
112+
auto data = SpanTestPeer::ToSpanData(&span);
113+
span.End();
114+
EXPECT_EQ("value1", data.attributes().at("str_key").string_value());
115+
EXPECT_EQ(123, data.attributes().at("int_key").int_value());
116+
EXPECT_EQ(true, data.attributes().at("bool_key").bool_value());
117+
EXPECT_EQ("another_value",
118+
data.attributes().at("another_key").string_value());
119+
}
120+
102121
TEST(SpanTest, AddAnnotationLastAttributeWins) {
103122
auto span =
104123
Span::StartSpan("SpanName", /*parent=*/nullptr, {nullptr, kRecordEvents});

opencensus/trace/span.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <vector>
2121

2222
#include "absl/strings/string_view.h"
23+
#include "absl/types/span.h"
2324
#include "opencensus/trace/attribute_value_ref.h"
2425
#include "opencensus/trace/sampler.h"
2526
#include "opencensus/trace/span_context.h"
@@ -45,7 +46,7 @@ class SpanTestPeer;
4546
// Attributes to the tracing API. e.g.:
4647
// AddAttributes({{"key1", "value1"}, {"key2", 123}});
4748
using AttributesRef =
48-
std::initializer_list<std::pair<absl::string_view, AttributeValueRef>>;
49+
absl::Span<const std::pair<absl::string_view, AttributeValueRef>>;
4950

5051
// Options for Starting a Span.
5152
struct StartSpanOptions {

0 commit comments

Comments
 (0)