Skip to content
Closed
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 @@ -17,6 +17,7 @@
#include "opentelemetry/trace/trace_id.h"
#include "opentelemetry/trace/tracer_provider.h"

#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/recordable.h"
#include "opentelemetry/sdk/trace/span_data.h"
Expand Down Expand Up @@ -498,6 +499,14 @@ class ETWSpanData final : public sdk::trace::Recordable
span_kind_ = span_kind;
}

void SetResourceAttribute(
std::string key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept override
{
UNREFERENCED_PARAMETER(key);
UNREFERENCED_PARAMETER(value);
}

void SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept override
{
start_time_ = start_time;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include "opentelemetry/proto/resource/v1/resource.pb.h"
#include "opentelemetry/proto/trace/v1/trace.pb.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/recordable.h"
#include "opentelemetry/version.h"

Expand Down Expand Up @@ -34,12 +36,19 @@ class Recordable final : public sdk::trace::Recordable

void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override;

void SetResourceAttribute(
std::string key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept override;

void SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept override;

void SetDuration(std::chrono::nanoseconds duration) noexcept override;

proto::resource::v1::Resource &GetResources() noexcept;

private:
proto::trace::v1::Span span_;
proto::resource::v1::Resource resource_;
};
} // namespace otlp
} // namespace exporter
Expand Down
13 changes: 13 additions & 0 deletions exporters/otlp/src/otlp_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,23 @@ void PopulateRequest(const nostd::span<std::unique_ptr<sdk::trace::Recordable>>
auto resource_span = request->add_resource_spans();
auto instrumentation_lib = resource_span->add_instrumentation_library_spans();

// TBD -> Group using Resources and Instrumentation Lib ( version and name)
bool resource_added = false;
for (auto &recordable : spans)
{
auto rec = std::unique_ptr<Recordable>(static_cast<Recordable *>(recordable.release()));
*instrumentation_lib->add_spans() = std::move(rec->span());

if (!resource_added)
{
auto resource = rec->GetResources();
if (resource.attributes_size())
{
// TODO
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ready for review?

// resource_span->set_allocated_resource(&resource);
resource_added = true;
}
}
}
}

Expand Down
108 changes: 108 additions & 0 deletions exporters/otlp/src/recordable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,101 @@ void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
}
}

void PopulateResourceAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
const std::string &key,
const opentelemetry::sdk::common::OwnedAttributeValue &value)
{

attribute->set_key(key.data(), key.size());

if (nostd::holds_alternative<bool>(value))
{
attribute->mutable_value()->set_bool_value(nostd::get<bool>(value));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove blank line.

else if (nostd::holds_alternative<int>(value))
{
attribute->mutable_value()->set_int_value(nostd::get<int>(value));
}
else if (nostd::holds_alternative<int64_t>(value))
{
attribute->mutable_value()->set_int_value(nostd::get<int64_t>(value));
}
else if (nostd::holds_alternative<unsigned int>(value))
{
attribute->mutable_value()->set_int_value(nostd::get<unsigned int>(value));
}
else if (nostd::holds_alternative<uint64_t>(value))
{
attribute->mutable_value()->set_int_value(nostd::get<uint64_t>(value));
}
else if (nostd::holds_alternative<double>(value))
{
attribute->mutable_value()->set_double_value(nostd::get<double>(value));
}
else if (nostd::holds_alternative<std::string>(value))
{
attribute->mutable_value()->set_string_value(nostd::get<std::string>(value).data(),
nostd::get<std::string>(value).size());
}
#ifdef HAVE_SPAN_BYTE
else if (nostd::holds_alternative<uint8_t>(value))
{
attribute->mutable_value()->set_string_value(nostd::get<uint8_t>(value));
}
#endif
else if (nostd::holds_alternative<std::vector<bool>>(value))
{
for (const auto &val : nostd::get<std::vector<bool>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_bool_value(val);
}
}
else if (nostd::holds_alternative<std::vector<int32_t>>(value))
{
for (const auto &val : nostd::get<std::vector<int32_t>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
}
}
else if (nostd::holds_alternative<std::vector<uint32_t>>(value))
{
for (const auto &val : nostd::get<std::vector<uint32_t>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
}
}
else if (nostd::holds_alternative<std::vector<int64_t>>(value))
{
for (const auto &val : nostd::get<std::vector<int64_t>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
}
}
else if (nostd::holds_alternative<std::vector<uint64_t>>(value))
{
for (const auto &val : nostd::get<std::vector<uint64_t>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
}
}
else if (nostd::holds_alternative<std::vector<double>>(value))
{
for (const auto &val : nostd::get<std::vector<double>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_double_value(val);
}
}
else if (nostd::holds_alternative<std::vector<std::string>>(value))
{
for (const auto &val : nostd::get<std::vector<std::string>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_string_value(val.data(),
val.size());
}
}
}

void Recordable::SetAttribute(nostd::string_view key,
const opentelemetry::common::AttributeValue &value) noexcept
{
Expand Down Expand Up @@ -207,6 +302,14 @@ void Recordable::SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept
span_.set_kind(proto_span_kind);
}

void Recordable::SetResourceAttribute(
std::string key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept
{
auto attribute = resource_.add_attributes();
PopulateResourceAttribute(attribute, key, value);
}

void Recordable::SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept
{
span_.set_start_time_unix_nano(start_time.time_since_epoch().count());
Expand All @@ -217,6 +320,11 @@ void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept
const uint64_t unix_end_time = span_.start_time_unix_nano() + duration.count();
span_.set_end_time_unix_nano(unix_end_time);
}

proto::resource::v1::Resource &Recordable::GetResources() noexcept
{
return resource_;
}
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
5 changes: 3 additions & 2 deletions exporters/otlp/test/otlp_exporter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ TEST_F(OtlpExporterTestPeer, ExportIntegrationTest)

auto processor = std::shared_ptr<sdk::trace::SpanProcessor>(
new sdk::trace::SimpleSpanProcessor(std::move(exporter)));
auto provider =
nostd::shared_ptr<trace::TracerProvider>(new sdk::trace::TracerProvider(processor));
auto resource = opentelemetry::sdk::resource::Resource::Create({});
auto provider = nostd::shared_ptr<trace::TracerProvider>(
new sdk::trace::TracerProvider(processor, std::move(resource)));
auto tracer = provider->GetTracer("test");

EXPECT_CALL(*mock_stub, Export(_, _, _))
Expand Down
21 changes: 21 additions & 0 deletions ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
return span_id_;
}

/**
* Get the resources for this span
* @return the resources for this span
*/
const std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue>
GetResources()
{
std::lock_guard<std::mutex> lock(mutex_);
return resources_;
}

/**
* Get the parent span id for this span
* @return the span id for this span's parent
Expand Down Expand Up @@ -152,6 +163,15 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
span_kind_ = span_kind;
}

void SetResourceAttribute(
std::string key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept override
{
std::lock_guard<std::mutex> lock(mutex_);
attributes_.insert(
std::pair<std::string, opentelemetry::sdk::common::OwnedAttributeValue>(key, value));
}

void SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept override
{
std::lock_guard<std::mutex> lock(mutex_);
Expand Down Expand Up @@ -221,6 +241,7 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable
std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> attributes_;
std::vector<opentelemetry::sdk::trace::SpanDataEvent> events_;
opentelemetry::sdk::common::AttributeConverter converter_;
std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> resources_;
};
} // namespace zpages
} // namespace ext
Expand Down
5 changes: 3 additions & 2 deletions ext/test/zpages/tracez_data_aggregator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ class TracezDataAggregatorTest : public ::testing::Test
void SetUp() override
{
std::shared_ptr<TracezSpanProcessor> processor(new TracezSpanProcessor());
auto resource = opentelemetry::sdk::resource::Resource::Create({});
tracer = std::shared_ptr<opentelemetry::trace::Tracer>(new Tracer(processor, resource));
tracer = std::shared_ptr<opentelemetry::trace::Tracer>(new Tracer(processor, resource));
tracez_data_aggregator = std::unique_ptr<TracezDataAggregator>(
new TracezDataAggregator(processor, milliseconds(10)));
}

opentelemetry::sdk::resource::Resource resource =
opentelemetry::sdk::resource::Resource::Create({});
std::unique_ptr<TracezDataAggregator> tracez_data_aggregator;
std::shared_ptr<opentelemetry::trace::Tracer> tracer;
};
Expand Down
7 changes: 4 additions & 3 deletions ext/test/zpages/tracez_processor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ class TracezProcessor : public ::testing::Test
protected:
void SetUp() override
{
processor = std::shared_ptr<TracezSpanProcessor>(new TracezSpanProcessor());
auto resource = opentelemetry::sdk::resource::Resource::Create({});

processor = std::shared_ptr<TracezSpanProcessor>(new TracezSpanProcessor());
tracer = std::shared_ptr<opentelemetry::trace::Tracer>(new Tracer(processor, resource));
auto spans = processor->GetSpanSnapshot();
running = spans.running;
Expand All @@ -187,6 +185,9 @@ class TracezProcessor : public ::testing::Test
}

std::shared_ptr<TracezSpanProcessor> processor;
opentelemetry::sdk::resource::Resource resource =
opentelemetry::sdk::resource::Resource::Create({});

std::shared_ptr<opentelemetry::trace::Tracer> tracer;

std::vector<std::string> span_names;
Expand Down
12 changes: 12 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/recordable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "opentelemetry/common/key_value_iterable.h"
#include "opentelemetry/core/timestamp.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/sdk/common/empty_attributes.h"
#include "opentelemetry/trace/canonical_code.h"
#include "opentelemetry/trace/span.h"
Expand All @@ -13,6 +14,7 @@
#include "opentelemetry/version.h"

#include <map>
#include <string>

// TODO: Create generic short pattern for opentelemetry::common and opentelemetry::trace

Expand Down Expand Up @@ -115,6 +117,16 @@ class Recordable
* @param span_kind the spankind to set
*/
virtual void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept = 0;

/**
* Set Resource attribute of the span.
* @param key attribute key
* @param value attribute value
*/
virtual void SetResourceAttribute(
std::string key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept = 0;

/**
* Set the start time of the span.
* @param start_time the start time to set
Expand Down
19 changes: 19 additions & 0 deletions sdk/include/opentelemetry/sdk/trace/span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ class SpanData final : public Recordable
*/
opentelemetry::trace::SpanKind GetSpanKind() const noexcept { return span_kind_; }

/**
* Get Resources.
* @return the resource of this span
*/
const std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue>
&GetResources() const noexcept
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't this return a Resource object?

{
return resources_;
}

/**
* Get the status for this span
* @return the status for this span
Expand Down Expand Up @@ -213,6 +223,14 @@ class SpanData final : public Recordable
span_kind_ = span_kind;
}

void SetResourceAttribute(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand what this method is intended to do and where it is in the spec.

Fro what I gather, this is basically copying all the resource-attributes onto the span. Can we instead just grab a reference to the resource?

std::string key,
const opentelemetry::sdk::common::OwnedAttributeValue &value) noexcept override
{
resources_.insert(std::pair<std::string, opentelemetry::sdk::common::OwnedAttributeValue>(
std::string(key.data()), value));
}

void SetStartTime(opentelemetry::core::SystemTimestamp start_time) noexcept override
{
start_time_ = start_time;
Expand All @@ -233,6 +251,7 @@ class SpanData final : public Recordable
std::vector<SpanDataEvent> events_;
std::vector<SpanDataLink> links_;
opentelemetry::trace::SpanKind span_kind_{opentelemetry::trace::SpanKind::kInternal};
std::unordered_map<std::string, opentelemetry::sdk::common::OwnedAttributeValue> resources_;
};
} // namespace trace
} // namespace sdk
Expand Down
6 changes: 4 additions & 2 deletions sdk/src/trace/span.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
return;
}
recordable_->SetName(name);

trace_api::TraceId trace_id;
trace_api::SpanId span_id = GenerateRandomSpanId();

Expand Down Expand Up @@ -109,7 +108,10 @@ Span::Span(std::shared_ptr<Tracer> &&tracer,
recordable_->SetSpanKind(options.kind);
recordable_->SetStartTime(NowOr(options.start_system_time));
start_steady_time = NowOr(options.start_steady_time);
// recordable_->SetResource(resource_); TODO
for (auto attribute : resource.GetAttributes())
{
recordable_->SetResourceAttribute(attribute.first, attribute.second);
}
processor_->OnStart(*recordable_, parent_span_context);
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/src/trace/tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace trace
Tracer::Tracer(std::shared_ptr<SpanProcessor> processor,
const opentelemetry::sdk::resource::Resource &resource,
std::shared_ptr<Sampler> sampler) noexcept
: processor_{processor}, sampler_{sampler}, resource_{resource}
: processor_{processor}, sampler_{sampler}, resource_(resource)
{}

void Tracer::SetProcessor(std::shared_ptr<SpanProcessor> processor) noexcept
Expand Down
Loading