Skip to content

Commit

Permalink
Support for attributes on spans (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Tax authored Jun 23, 2020
1 parent 1a54e5d commit 9b624b9
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 25 deletions.
9 changes: 5 additions & 4 deletions api/include/opentelemetry/plugin/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class Factory final
* @param error_message on failure this will contain an error message.
* @return a Tracer on success or nullptr on failure.
*/
std::shared_ptr<Tracer> MakeTracer(nostd::string_view tracer_config,
std::string &error_message) const noexcept
std::shared_ptr<opentelemetry::trace::Tracer> MakeTracer(nostd::string_view tracer_config,
std::string &error_message) const
noexcept
{
nostd::unique_ptr<char[]> plugin_error_message;
auto tracer_handle = factory_impl_->MakeTracerHandle(tracer_config, plugin_error_message);
Expand All @@ -46,8 +47,8 @@ class Factory final
detail::CopyErrorMessage(plugin_error_message.get(), error_message);
return nullptr;
}
return std::shared_ptr<Tracer>{new (std::nothrow)
Tracer{library_handle_, std::move(tracer_handle)}};
return std::shared_ptr<opentelemetry::trace::Tracer>{
new (std::nothrow) Tracer{library_handle_, std::move(tracer_handle)}};
}

private:
Expand Down
8 changes: 7 additions & 1 deletion api/include/opentelemetry/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Span final : public trace::Span
{}

// trace::Span
void SetAttribute(nostd::string_view name, const common::AttributeValue &&value) noexcept override
{
span_->SetAttribute(name, std::move(value));
}

void AddEvent(nostd::string_view name) noexcept override { span_->AddEvent(name); }

void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override
Expand Down Expand Up @@ -61,9 +66,10 @@ class Tracer final : public trace::Tracer, public std::enable_shared_from_this<T
// trace::Tracer
nostd::unique_ptr<trace::Span> StartSpan(
nostd::string_view name,
const trace::KeyValueIterable &attributes,
const trace::StartSpanOptions &options = {}) noexcept override
{
auto span = tracer_handle_->tracer().StartSpan(name, options);
auto span = tracer_handle_->tracer().StartSpan(name, attributes, options);
if (span == nullptr)
{
return nullptr;
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 @@ -24,6 +24,10 @@ class NoopSpan final : public Span
public:
explicit NoopSpan(const std::shared_ptr<Tracer> &tracer) noexcept : tracer_{tracer} {}

void SetAttribute(nostd::string_view /*key*/,
const common::AttributeValue && /*value*/) noexcept override
{}

void AddEvent(nostd::string_view /*name*/) noexcept override {}

void AddEvent(nostd::string_view /*name*/, core::SystemTimestamp /*timestamp*/) noexcept override
Expand Down Expand Up @@ -56,6 +60,7 @@ class NoopTracer final : public Tracer, public std::enable_shared_from_this<Noop
public:
// Tracer
nostd::unique_ptr<Span> StartSpan(nostd::string_view /*name*/,
const KeyValueIterable & /*attributes*/,
const StartSpanOptions & /*options*/) noexcept override
{
return nostd::unique_ptr<Span>{new (std::nothrow) NoopSpan{this->shared_from_this()}};
Expand Down
9 changes: 3 additions & 6 deletions api/include/opentelemetry/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/core/timestamp.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/nostd/string_view.h"
Expand Down Expand Up @@ -40,7 +41,6 @@ struct StartSpanOptions
// Span(Context?) parent;
// SpanContext remote_parent;
// Links
// Attributes
SpanKind kind = SpanKind::kInternal;
};
/**
Expand Down Expand Up @@ -74,13 +74,10 @@ class Span
Span &operator=(const Span &) = delete;
Span &operator=(Span &&) = delete;

// TODO
// Sets an attribute on the Span. If the Span previously contained a mapping for
// the key, the old value is replaced.
//
// If an empty string is used as the value, the attribute will be silently
// dropped. Note: this behavior could change in the future.
// virtual void SetAttribute(nostd::string_view key, AttributeValue&& value) = 0;
virtual void SetAttribute(nostd::string_view key,
const common::AttributeValue &&value) noexcept = 0;

// Adds an event to the Span.
virtual void AddEvent(nostd::string_view name) noexcept = 0;
Expand Down
31 changes: 31 additions & 0 deletions api/include/opentelemetry/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,41 @@ class Tracer
virtual ~Tracer() = default;
/**
* Starts a span.
*
* Optionally sets attributes at Span creation from the given key/value pairs.
*
* Attributes will be processed in order, previous attributes with the same
* key will be overwritten.
*/
virtual nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
const KeyValueIterable &attributes,
const StartSpanOptions &options = {}) noexcept = 0;

nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
const StartSpanOptions &options = {}) noexcept
{
return this->StartSpan(name, {}, options);
}

template <class T, nostd::enable_if_t<detail::is_key_value_iterable<T>::value> * = nullptr>
nostd::unique_ptr<Span> StartSpan(nostd::string_view name,
const T &attributes,
const StartSpanOptions &options = {}) noexcept
{
return this->StartSpan(name, KeyValueIterableView<T>(attributes), options);
}

nostd::unique_ptr<Span> StartSpan(
nostd::string_view name,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes,
const StartSpanOptions &options = {}) noexcept
{
return this->StartSpan(name,
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
attributes.begin(), attributes.end()},
options);
}

/**
* Force any buffered spans to flush.
* @param timeout to complete the flush
Expand Down
20 changes: 14 additions & 6 deletions examples/plugin/plugin/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include <iostream>

namespace nostd = opentelemetry::nostd;
namespace core = opentelemetry::core;
namespace trace = opentelemetry::trace;
namespace nostd = opentelemetry::nostd;
namespace common = opentelemetry::common;
namespace core = opentelemetry::core;
namespace trace = opentelemetry::trace;

namespace
{
Expand All @@ -13,6 +14,7 @@ class Span final : public trace::Span
public:
Span(std::shared_ptr<Tracer> &&tracer,
nostd::string_view name,
const opentelemetry::trace::KeyValueIterable & /*attributes*/,
const trace::StartSpanOptions & /*options*/) noexcept
: tracer_{std::move(tracer)}, name_{name}
{
Expand All @@ -22,6 +24,10 @@ class Span final : public trace::Span
~Span() { std::cout << "~Span\n"; }

// opentelemetry::trace::Span
void SetAttribute(nostd::string_view /*name*/,
const common::AttributeValue && /*value*/) noexcept override
{}

void AddEvent(nostd::string_view /*name*/) noexcept override {}

void AddEvent(nostd::string_view /*name*/, core::SystemTimestamp /*timestamp*/) noexcept override
Expand Down Expand Up @@ -52,9 +58,11 @@ class Span final : public trace::Span

Tracer::Tracer(nostd::string_view /*output*/) {}

nostd::unique_ptr<trace::Span> Tracer::StartSpan(nostd::string_view name,
const trace::StartSpanOptions &options) noexcept
nostd::unique_ptr<trace::Span> Tracer::StartSpan(
nostd::string_view name,
const opentelemetry::trace::KeyValueIterable &attributes,
const trace::StartSpanOptions &options) noexcept
{
return nostd::unique_ptr<opentelemetry::trace::Span>{
new (std::nothrow) Span{this->shared_from_this(), name, options}};
new (std::nothrow) Span{this->shared_from_this(), name, attributes, options}};
}
3 changes: 2 additions & 1 deletion examples/plugin/plugin/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Tracer final : public opentelemetry::trace::Tracer,
// opentelemetry::trace::Tracer
opentelemetry::nostd::unique_ptr<opentelemetry::trace::Span> StartSpan(
opentelemetry::nostd::string_view name,
const opentelemetry::trace::StartSpanOptions &options) noexcept override;
const opentelemetry::trace::KeyValueIterable & /*attributes*/,
const opentelemetry::trace::StartSpanOptions & /*options */) noexcept override;

void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}

Expand Down
9 changes: 9 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/recordable.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/core/timestamp.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/trace/canonical_code.h"
Expand Down Expand Up @@ -33,6 +34,14 @@ class Recordable
opentelemetry::trace::SpanId span_id,
opentelemetry::trace::SpanId parent_span_id) noexcept = 0;

/**
* Set an attribute of a span.
* @param name the name of the attribute
* @param value the attribute value
*/
virtual void SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &&value) noexcept = 0;

/**
* Add an event to a span.
* @param name the name of the event
Expand Down
16 changes: 16 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/span_data.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <chrono>
#include <unordered_map>
#include "opentelemetry/core/timestamp.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/trace/recordable.h"
Expand Down Expand Up @@ -67,6 +68,15 @@ class SpanData final : public Recordable
*/
std::chrono::nanoseconds GetDuration() const noexcept { return duration_; }

/**
* Get the attributes for this span
* @return the attributes for this span
*/
const std::unordered_map<std::string, common::AttributeValue> &GetAttributes() const noexcept
{
return attributes_;
}

void SetIds(opentelemetry::trace::TraceId trace_id,
opentelemetry::trace::SpanId span_id,
opentelemetry::trace::SpanId parent_span_id) noexcept override
Expand All @@ -76,6 +86,11 @@ class SpanData final : public Recordable
parent_span_id_ = parent_span_id;
}

void SetAttribute(nostd::string_view key, const common::AttributeValue &&value) noexcept override
{
attributes_[std::string(key)] = value;
}

void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override
{
(void)name;
Expand Down Expand Up @@ -106,6 +121,7 @@ class SpanData final : public Recordable
std::string name_;
opentelemetry::trace::CanonicalCode status_code_{opentelemetry::trace::CanonicalCode::OK};
std::string status_desc_;
std::unordered_map<std::string, common::AttributeValue> attributes_;
};
} // namespace trace
} // namespace sdk
Expand Down
1 change: 1 addition & 0 deletions sdk/include/opentelemetry/sdk/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th

nostd::unique_ptr<trace_api::Span> StartSpan(
nostd::string_view name,
const trace_api::KeyValueIterable &attributes,
const trace_api::StartSpanOptions &options = {}) noexcept override;

void ForceFlushWithMicroseconds(uint64_t timeout) noexcept override;
Expand Down
13 changes: 13 additions & 0 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ SteadyTimestamp NowOr(const SteadyTimestamp &steady)
Span::Span(std::shared_ptr<Tracer> &&tracer,
std::shared_ptr<SpanProcessor> processor,
nostd::string_view name,
const trace_api::KeyValueIterable &attributes,
const trace_api::StartSpanOptions &options) noexcept
: tracer_{std::move(tracer)},
processor_{processor},
Expand All @@ -55,6 +56,11 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
processor_->OnStart(*recordable_);
recordable_->SetName(name);

attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept {
recordable_->SetAttribute(key, std::move(value));
return true;
});

recordable_->SetStartTime(NowOr(options.start_system_time));
start_steady_time = NowOr(options.start_steady_time);
}
Expand All @@ -64,6 +70,13 @@ Span::~Span()
End();
}

void Span::SetAttribute(nostd::string_view key, const common::AttributeValue &&value) noexcept
{
std::lock_guard<std::mutex> lock_guard{mu_};

recordable_->SetAttribute(key, std::move(value));
}

void Span::AddEvent(nostd::string_view name) noexcept
{
(void)name;
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/trace/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class Span final : public trace_api::Span
explicit Span(std::shared_ptr<Tracer> &&tracer,
std::shared_ptr<SpanProcessor> processor,
nostd::string_view name,
const trace_api::KeyValueIterable &attributes,
const trace_api::StartSpanOptions &options) noexcept;

~Span() override;

// trace_api::Span
void SetAttribute(nostd::string_view key, const common::AttributeValue &&value) noexcept override;

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

void AddEvent(nostd::string_view name, core::SystemTimestamp timestamp) noexcept override;
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ std::shared_ptr<SpanProcessor> Tracer::GetProcessor() const noexcept

nostd::unique_ptr<trace_api::Span> Tracer::StartSpan(
nostd::string_view name,
const trace_api::KeyValueIterable &attributes,
const trace_api::StartSpanOptions &options) noexcept
{
return nostd::unique_ptr<trace_api::Span>{
new (std::nothrow) Span{this->shared_from_this(), processor_.load(), name, options}};
return nostd::unique_ptr<trace_api::Span>{new (std::nothrow) Span{
this->shared_from_this(), processor_.load(), name, attributes, options}};
}

void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept
Expand Down
4 changes: 4 additions & 0 deletions sdk/test/trace/span_data_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "opentelemetry/sdk/trace/span_data.h"
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/trace_id.h"

Expand All @@ -20,6 +21,7 @@ TEST(SpanData, DefaultValues)
ASSERT_EQ(data.GetDescription(), "");
ASSERT_EQ(data.GetStartTime().time_since_epoch(), std::chrono::nanoseconds(0));
ASSERT_EQ(data.GetDuration(), std::chrono::nanoseconds(0));
ASSERT_EQ(data.GetAttributes().size(), 0);
}

TEST(SpanData, Set)
Expand All @@ -35,6 +37,7 @@ TEST(SpanData, Set)
data.SetStatus(opentelemetry::trace::CanonicalCode::UNKNOWN, "description");
data.SetStartTime(now);
data.SetDuration(std::chrono::nanoseconds(1000000));
data.SetAttribute("attr1", 314159);
data.AddEvent("event1", now);

ASSERT_EQ(data.GetTraceId(), trace_id);
Expand All @@ -45,4 +48,5 @@ TEST(SpanData, Set)
ASSERT_EQ(data.GetDescription(), "description");
ASSERT_EQ(data.GetStartTime().time_since_epoch(), now.time_since_epoch());
ASSERT_EQ(data.GetDuration(), std::chrono::nanoseconds(1000000));
ASSERT_EQ(opentelemetry::nostd::get<int>(data.GetAttributes().at("attr1")), 314159);
}
Loading

0 comments on commit 9b624b9

Please sign in to comment.