Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate resource attributes into process tags in Jaeger #897

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Recordable final : public sdk::trace::Recordable

thrift::Span *Span() noexcept { return span_.release(); }
std::vector<thrift::Tag> Tags() noexcept { return std::move(tags_); }
std::vector<thrift::Tag> ResourceTags() noexcept { return std::move(resource_tags_); }
std::vector<thrift::Log> Logs() noexcept { return std::move(logs_); }
const std::string &ServiceName() const noexcept { return service_name_; }

Expand Down Expand Up @@ -99,9 +100,14 @@ class Recordable final : public sdk::trace::Recordable
const opentelemetry::common::AttributeValue &value,
std::vector<thrift::Tag> &tags);

void PopulateAttribute(nostd::string_view key,
const sdk::common::OwnedAttributeValue &value,
std::vector<thrift::Tag> &tags);

private:
std::unique_ptr<thrift::Span> span_;
std::vector<thrift::Tag> tags_;
std::vector<thrift::Tag> resource_tags_;
std::vector<thrift::Log> logs_;
std::string service_name_;
};
Expand Down
39 changes: 34 additions & 5 deletions exporters/jaeger/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ void Recordable::PopulateAttribute(nostd::string_view key,
// TODO: extend other AttributeType to the types supported by Jaeger.
}

void Recordable::PopulateAttribute(nostd::string_view key,
const sdk::common::OwnedAttributeValue &value,
std::vector<thrift::Tag> &tags)
{
if (nostd::holds_alternative<int64_t>(value))
{
AddTag(std::string{key}, nostd::get<int64_t>(value), tags);
}
else if (nostd::holds_alternative<bool>(value))
{
AddTag(std::string{key}, nostd::get<bool>(value), tags);
}
else if (nostd::holds_alternative<double>(value))
{
AddTag(std::string{key}, nostd::get<double>(value), tags);
}
else if (nostd::holds_alternative<std::string>(value))
{
AddTag(std::string{key}, std::string{nostd::get<std::string>(value)}, tags);
}
// TODO: extend other OwnedAttributeType to the types supported by Jaeger.
}

void Recordable::SetIdentity(const trace::SpanContext &span_context,
trace::SpanId parent_span_id) noexcept
{
Expand Down Expand Up @@ -77,7 +100,7 @@ void Recordable::AddEvent(nostd::string_view name,
const common::KeyValueIterable &attributes) noexcept
{
std::vector<thrift::Tag> tags;
PopulateAttribute("event", name.data(), tags);
PopulateAttribute("event", static_cast<common::AttributeValue>(name.data()), tags);
lalitb marked this conversation as resolved.
Show resolved Hide resolved

attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept {
PopulateAttribute(key, value, tags);
Expand Down Expand Up @@ -131,11 +154,17 @@ void Recordable::SetName(nostd::string_view name) noexcept

void Recordable::SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept
{
// only service.name attribute is supported by specs as of now.
auto attributes = resource.GetAttributes();
if (attributes.find(OTEL_CPP_GET_ATTR(AttrServiceName)) != attributes.end())
for (const auto &attribute_iter : resource.GetAttributes())
{
service_name_ = nostd::get<std::string>(attributes[OTEL_CPP_GET_ATTR(AttrServiceName)]);
if (attribute_iter.first != "service.name")
{
PopulateAttribute(nostd::string_view{attribute_iter.first}, attribute_iter.second,
resource_tags_);
}
else
{
service_name_ = nostd::get<std::string>(attribute_iter.second);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions exporters/jaeger/src/thrift_sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ int ThriftSender::Append(std::unique_ptr<Recordable> &&span) noexcept
if (process_.serviceName.empty())
{
process_.serviceName = span->ServiceName();
process_.__set_tags(span->ResourceTags());

process_bytes_size_ = CalcSizeOfSerializedThrift(process_);
max_span_bytes -= process_bytes_size_;
Expand Down
35 changes: 35 additions & 0 deletions exporters/jaeger/test/jaeger_recordable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,38 @@ TEST(JaegerSpanRecordable, SetInstrumentationLibrary)
EXPECT_EQ(tags[1].vType, thrift::TagType::STRING);
EXPECT_EQ(tags[1].vStr, library_version);
}

TEST(JaegerSpanRecordable, SetResource)
{
opentelemetry::exporter::jaeger::Recordable rec;

const std::string service_name_key = "service.name";
std::string service_name_value = "test-jaeger-service-name";
auto resource = opentelemetry::sdk::resource::Resource::Create(
{{service_name_key, service_name_value}, {"key1", "value1"}, {"key2", "value2"}});
rec.SetResource(resource);

auto service_name = rec.ServiceName();
auto resource_tags = rec.ResourceTags();

EXPECT_GE(resource_tags.size(), 2);
EXPECT_EQ(service_name, service_name_value);

bool found_key1 = false;
bool found_key2 = false;
for (const auto &tag : resource_tags)
{
if (tag.key == "key1")
{
found_key1 = true;
EXPECT_EQ(tag.vType, thrift::TagType::STRING);
EXPECT_EQ(tag.vStr, "value1");
}
else if (tag.key == "key2")
{
found_key2 = true;
EXPECT_EQ(tag.vType, thrift::TagType::STRING);
EXPECT_EQ(tag.vStr, "value2");
}
}
}