Skip to content

Commit

Permalink
Add single link
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Oct 22, 2023
1 parent 76ce23b commit 5356600
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 53 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Important changes:

* [API] Add a new AddLink() operation to Span
[#2380](https://github.com/open-telemetry/opentelemetry-cpp/pull/2380)
* new `API` Span::AddLink() adds links to a span.
* New `API` Span::AddLink() adds a single link to a span.
* New `API` Span::AddLinks() adds multiple links to a span.
* Because this is an `ABI` breaking change, the fix is only available
with the `CMake` option `WITH_ABI_VERSION_2=ON`.
* When building with `CMake` option `WITH_ABI_VERSION_1=ON` (by default)
Expand Down
10 changes: 8 additions & 2 deletions api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ class Span final : public trace::Span
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const trace::SpanContextKeyValueIterable *links) noexcept override
void AddLink(const trace::SpanContext &target,
const common::KeyValueIterable &attrs) noexcept override
{
span_->AddLink(links);
span_->AddLink(target, attrs);
}

void AddLinks(const trace::SpanContextKeyValueIterable &links) noexcept override
{
span_->AddLinks(links);
}
#endif

Expand Down
5 changes: 4 additions & 1 deletion api/include/opentelemetry/trace/default_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ class DefaultSpan : public Span
{}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const SpanContextKeyValueIterable * /* links */) noexcept override {}
void AddLink(const SpanContext & /* target */,
const common::KeyValueIterable & /* attrs */) noexcept override
{}
void AddLinks(const SpanContextKeyValueIterable & /* links */) noexcept override {}
#endif

void SetStatus(StatusCode /* status */, nostd::string_view /* description */) noexcept override {}
Expand Down
6 changes: 5 additions & 1 deletion api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ class OPENTELEMETRY_EXPORT NoopSpan final : public Span
{}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const SpanContextKeyValueIterable * /* links */) noexcept override {}
void AddLink(const SpanContext & /* target */,
const common::KeyValueIterable & /* attrs */) noexcept override
{}

void AddLinks(const SpanContextKeyValueIterable & /* links */) noexcept override {}
#endif

void SetStatus(StatusCode /*code*/, nostd::string_view /*description*/) noexcept override {}
Expand Down
55 changes: 48 additions & 7 deletions api/include/opentelemetry/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,72 @@ class Span

#if OPENTELEMETRY_ABI_VERSION_NO >= 2

/**
* Add link (ABI).
*
* @since ABI_VERSION 2
*/
virtual void AddLink(const SpanContext &target,
const common::KeyValueIterable &attrs) noexcept = 0;

/**
* Add links (ABI).
*
* @since ABI_VERSION 2
*/
virtual void AddLink(const SpanContextKeyValueIterable *links) noexcept = 0;
virtual void AddLinks(const SpanContextKeyValueIterable &links) noexcept = 0;

/**
* Add link (API helper).
*
* @since ABI_VERSION 2
*/
template <class U,
nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
void AddLink(const SpanContext &target, const U &attrs)
{
common::KeyValueIterableView<U> view(attrs);
this->AddLink(target, view);
}

/**
* Add link (API helper).
*
* @since ABI_VERSION 2
*/
void AddLink(const SpanContext &target,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attrs)
{
/* Build a container from std::initializer_list. */
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> container{
attrs.begin(), attrs.end()};

/* Build a view on the container. */
common::KeyValueIterableView<
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>>
view(container);

return this->AddLink(target, view);
}

/**
* Add links (API helper).
*
* @since ABI_VERSION 2
*/
template <class U, nostd::enable_if_t<detail::is_span_context_kv_iterable<U>::value> * = nullptr>
void AddLink(const U &links)
void AddLinks(const U &links)
{
SpanContextKeyValueIterableView<U> view(links);
this->AddLink(&view);
this->AddLinks(view);
}

/**
* Add links (API helper).
*
* @since ABI_VERSION 2
*/
void AddLink(
void AddLinks(
std::initializer_list<
std::pair<SpanContext,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>>>
Expand All @@ -136,14 +177,14 @@ class Span
/* Build a container from std::initializer_list. */
nostd::span<const std::pair<
SpanContext, std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>>>
links_span{links.begin(), links.end()};
container{links.begin(), links.end()};

/* Build a view on the container. */
SpanContextKeyValueIterableView<nostd::span<const std::pair<
SpanContext, std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>>>>
view(links_span);
view(container);

return this->AddLink(&view);
return this->AddLinks(view);
}

#endif /* OPENTELEMETRY_ABI_VERSION_NO */
Expand Down
6 changes: 5 additions & 1 deletion examples/plugin/plugin/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ class Span final : public trace::Span
{}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const trace::SpanContextKeyValueIterable * /* links */) noexcept override {}
void AddLink(const trace::SpanContext &target,
const common::KeyValueIterable &attrs) noexcept override
{}

void AddLinks(const trace::SpanContextKeyValueIterable & /* links */) noexcept override {}
#endif

void SetStatus(trace::StatusCode /*code*/, nostd::string_view /*description*/) noexcept override
Expand Down
18 changes: 15 additions & 3 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,28 @@ void Span::AddEvent(nostd::string_view name,
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void Span::AddLink(const opentelemetry::trace::SpanContextKeyValueIterable *links) noexcept
void Span::AddLink(const opentelemetry::trace::SpanContext &target,
const opentelemetry::common::KeyValueIterable &attrs) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}

links->ForEachKeyValue([&](opentelemetry::trace::SpanContext span_context,
const common::KeyValueIterable &attributes) {
recordable_->AddLink(target, attrs);
}

void Span::AddLinks(const opentelemetry::trace::SpanContextKeyValueIterable &links) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
if (recordable_ == nullptr)
{
return;
}

links.ForEachKeyValue([&](opentelemetry::trace::SpanContext span_context,
const common::KeyValueIterable &attributes) {
recordable_->AddLink(span_context, attributes);
return true;
});
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class Span final : public opentelemetry::trace::Span
nostd::string_view description) noexcept override;

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const opentelemetry::trace::SpanContextKeyValueIterable *links) noexcept override;
void AddLink(const opentelemetry::trace::SpanContext &target,
const opentelemetry::common::KeyValueIterable &attrs) noexcept override;

void AddLinks(const opentelemetry::trace::SpanContextKeyValueIterable &links) noexcept override;
#endif

void UpdateName(nostd::string_view name) noexcept override;
Expand Down
Loading

0 comments on commit 5356600

Please sign in to comment.