Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff committed Oct 19, 2023
1 parent 91dd15f commit b4ba4aa
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/plugin/detail/tracer_handle.h"
#include "opentelemetry/trace/span_context_kv_iterable.h"
#include "opentelemetry/trace/tracer.h"
#include "opentelemetry/version.h"

Expand Down Expand Up @@ -49,6 +50,13 @@ class Span final : public trace::Span
span_->AddEvent(name, timestamp, attributes);
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const trace::SpanContextKeyValueIterable *links) noexcept override
{
span_->AddLink(links);
}
#endif

void SetStatus(trace::StatusCode code, nostd::string_view description) noexcept override
{
span_->SetStatus(code, description);
Expand Down
4 changes: 4 additions & 0 deletions api/include/opentelemetry/trace/default_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class DefaultSpan : public Span
const common::KeyValueIterable & /* attributes */) noexcept override
{}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const SpanContextKeyValueIterable *links) noexcept override {}
#endif

void SetStatus(StatusCode /* status */, nostd::string_view /* description */) noexcept override {}

void UpdateName(nostd::string_view /* name */) noexcept override {}
Expand Down
5 changes: 5 additions & 0 deletions api/include/opentelemetry/trace/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_context_kv_iterable.h"
#include "opentelemetry/trace/tracer.h"
#include "opentelemetry/trace/tracer_provider.h"
#include "opentelemetry/version.h"
Expand Down Expand Up @@ -58,6 +59,10 @@ class OPENTELEMETRY_EXPORT NoopSpan final : public Span
const common::KeyValueIterable & /*attributes*/) noexcept override
{}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const SpanContextKeyValueIterable * /* links */) noexcept override {}
#endif

void SetStatus(StatusCode /*code*/, nostd::string_view /*description*/) noexcept override {}

void UpdateName(nostd::string_view /*name*/) noexcept override {}
Expand Down
49 changes: 49 additions & 0 deletions api/include/opentelemetry/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "opentelemetry/nostd/type_traits.h"
#include "opentelemetry/trace/canonical_code.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/trace/span_context_kv_iterable.h"
#include "opentelemetry/trace/span_context_kv_iterable_view.h"
#include "opentelemetry/trace/span_metadata.h"

#include "opentelemetry/version.h"
Expand Down Expand Up @@ -99,6 +101,53 @@ class Span
attributes.begin(), attributes.end()});
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2

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

/**
* 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)
{
SpanContextKeyValueIterableView<U> view(links);
this->AddLink(&view);
}

/**
* Add links (API helper).
*
* @since ABI_VERSION 2
*/
void AddLink(
std::initializer_list<
std::pair<SpanContext,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>>>
links)
{
/* 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()};

/* 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);

return this->AddLink(&view);
}

#endif /* OPENTELEMETRY_ABI_VERSION_NO */

// Sets the status of the span. The default status is Unset. Only the value of
// the last call will be
// recorded, and implementations are free to ignore previous calls.
Expand Down
1 change: 1 addition & 0 deletions api/include/opentelemetry/trace/span_context_kv_iterable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/common/key_value_iterable_view.h"
#include "opentelemetry/nostd/function_ref.h"
#include "opentelemetry/trace/span_context.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down
4 changes: 4 additions & 0 deletions examples/plugin/plugin/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Span final : public trace::Span
const common::KeyValueIterable & /*attributes*/) noexcept override
{}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const trace::SpanContextKeyValueIterable * /* links */) noexcept override {}
#endif

void SetStatus(trace::StatusCode /*code*/, nostd::string_view /*description*/) noexcept override
{}

Expand Down
17 changes: 17 additions & 0 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ void Span::AddEvent(nostd::string_view name,
recordable_->AddEvent(name, timestamp, attributes);
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void Span::AddLink(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;
});
}
#endif

void Span::SetStatus(opentelemetry::trace::StatusCode code, nostd::string_view description) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Span final : public opentelemetry::trace::Span
void SetStatus(opentelemetry::trace::StatusCode code,
nostd::string_view description) noexcept override;

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
void AddLink(const opentelemetry::trace::SpanContextKeyValueIterable *links) noexcept override;
#endif

void UpdateName(nostd::string_view name) noexcept override;

void End(const opentelemetry::trace::EndSpanOptions &options = {}) noexcept override;
Expand Down

0 comments on commit b4ba4aa

Please sign in to comment.