Skip to content

Commit

Permalink
Simplify code with ResourceSpan.mutable_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan committed May 16, 2021
1 parent 8766d4c commit b99dbbc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Recordable final : public sdk::trace::Recordable
const proto::trace::v1::Span &span() const noexcept { return span_; }

/** Dynamically converts the resource of this span into a proto. */
void PopulateProtoResource(proto::resource::v1::Resource *resource) const noexcept;
proto::resource::v1::Resource ProtoResource() const noexcept;

void SetIdentity(const opentelemetry::trace::SpanContext &span_context,
opentelemetry::trace::SpanId parent_span_id) noexcept override;
Expand Down
9 changes: 2 additions & 7 deletions exporters/otlp/src/otlp_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,8 @@ void PopulateRequest(const nostd::span<std::unique_ptr<sdk::trace::Recordable>>

if (!has_resource)
{
std::unique_ptr<proto::resource::v1::Resource> proto_resource{
new proto::resource::v1::Resource};

rec->PopulateProtoResource(proto_resource.get());

resource_span->set_allocated_resource(proto_resource.release());
has_resource = true;
*resource_span->mutable_resource() = rec->ProtoResource();
has_resource = true;
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions exporters/otlp/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,18 @@ void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
}
}

void Recordable::PopulateProtoResource(proto::resource::v1::Resource *proto) const noexcept
proto::resource::v1::Resource Recordable::ProtoResource() const noexcept
{
proto::resource::v1::Resource proto;
if (resource_)
{
for (const auto &kv : resource_->GetAttributes())
{
PopulateAttribute(proto->add_attributes(), kv.first, kv.second);
PopulateAttribute(proto.add_attributes(), kv.first, kv.second);
}
}

return proto;
}

void Recordable::SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept
Expand Down

0 comments on commit b99dbbc

Please sign in to comment.