Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE - PLEASE REVIEW DESIGN] Feed Resource to Exporter #575

Closed
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 @@ -71,6 +71,7 @@ class ETWTracerExporter final : public opentelemetry::sdk::trace::SpanExporter
* @return Returns the result of the operation
*/
sdk::trace::ExportResult Export(
const opentelemetry::sdk::resource::Resource &resource,
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &recordables) noexcept override
{
for (auto &recordable : recordables)
Expand Down
2 changes: 1 addition & 1 deletion exporters/etw/test/etw_tracer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ TEST(ETWTracer, ExportUnitTest)
recordable->SetName("MySpan");

nostd::span<std::unique_ptr<sdk::trace::Recordable>> batch(&recordable, 1);
auto result = exporter->Export(batch);
auto result = exporter->Export(sdk::resource::Resource::GetDefault(), batch);
EXPECT_EQ(sdk::trace::ExportResult::kSuccess, result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "opentelemetry/exporters/memory/in_memory_span_data.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down Expand Up @@ -37,6 +38,7 @@ class InMemorySpanExporter final : public opentelemetry::sdk::trace::SpanExporte
* @return Returns the result of the operation
*/
sdk::trace::ExportResult Export(
const opentelemetry::sdk::resource::Resource& resource,
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &recordables) noexcept override
{
for (auto &recordable : recordables)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "opentelemetry/nostd/type_traits.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/span_data.h"
#include "opentelemetry/version.h"
Expand Down Expand Up @@ -35,6 +36,7 @@ class OStreamSpanExporter final : public sdktrace::SpanExporter
std::unique_ptr<sdktrace::Recordable> MakeRecordable() noexcept override;

sdktrace::ExportResult Export(
const opentelemetry::sdk::resource::Resource &resource,
const nostd::span<std::unique_ptr<sdktrace::Recordable>> &spans) noexcept override;

bool Shutdown(
Expand Down
1 change: 1 addition & 0 deletions exporters/ostream/src/span_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ std::unique_ptr<sdktrace::Recordable> OStreamSpanExporter::MakeRecordable() noex
}

sdktrace::ExportResult OStreamSpanExporter::Export(
const opentelemetry::sdk::resource::Resource &resource,
const nostd::span<std::unique_ptr<sdktrace::Recordable>> &spans) noexcept
{
if (isShutdown_)
Expand Down
18 changes: 18 additions & 0 deletions exporters/otlp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,28 @@ cc_library(
],
strip_include_prefix = "include",
deps = [
":shared_utils",
"//sdk/src/trace",
"@com_github_opentelemetry_proto//:trace_proto_cc",
],
)

cc_library(
name = "shared_utils",
srcs = [
"src/shared_utils.cc",
],
hdrs = [
"include/opentelemetry/exporters/otlp/shared_utils.h",
],
strip_include_prefix = "include",
deps = [
"//sdk/src/trace",
"@com_github_opentelemetry_proto//:common_proto_cc",
],
)


cc_library(
name = "otlp_exporter",
srcs = [
Expand All @@ -42,6 +59,7 @@ cc_library(
strip_include_prefix = "include",
deps = [
":recordable",
":shared_utils",
"//sdk/src/trace",

# For gRPC
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "opentelemetry/proto/collector/trace/v1/trace_service.grpc.pb.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/sdk/trace/exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand Down Expand Up @@ -44,6 +45,7 @@ class OtlpExporter final : public opentelemetry::sdk::trace::SpanExporter
* @param spans a span of unique pointers to span recordables
*/
sdk::trace::ExportResult Export(
const sdk::resource::Resource& resource,
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans) noexcept override;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "opentelemetry/proto/common/v1/common.pb.h"
#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/sdk/resource/resource.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{
namespace internal
{

/** Maps from C++ attribute into OTLP proto attribute. */
void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::common::AttributeValue &value);

/** Maps from C++ attribute into OTLP proto attribute. */
void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const sdk::common::OwnedAttributeValue &value);

} // namespace internal
} // namespace otlp
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
19 changes: 16 additions & 3 deletions exporters/otlp/src/otlp_exporter.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "opentelemetry/exporters/otlp/otlp_exporter.h"
#include "opentelemetry/exporters/otlp/recordable.h"
#include "opentelemetry/exporters/otlp/shared_utils.h"

#include <grpcpp/grpcpp.h>
#include <iostream>
Expand All @@ -12,15 +13,27 @@ namespace otlp

// ----------------------------- Helper functions ------------------------------

void PopulateResource(const sdk::resource::Resource& resource,
proto::resource::v1::Resource* proto) {
// TODO - Fill this out.
for (const auto& kv : resource.GetAttributes()) {
internal::PopulateAttribute(proto->add_attributes(), kv.first, kv.second);
}

}

/**
* Add span protobufs contained in recordables to request.
* @param spans the spans to export
* @param request the current request
*/
void PopulateRequest(const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans,
void PopulateRequest(const sdk::resource::Resource& resource,
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans,
proto::collector::trace::v1::ExportTraceServiceRequest *request)
{
auto resource_span = request->add_resource_spans();
PopulateResource(resource, resource_span->mutable_resource());

auto instrumentation_lib = resource_span->add_instrumentation_library_spans();

for (auto &recordable : spans)
Expand Down Expand Up @@ -61,11 +74,11 @@ std::unique_ptr<sdk::trace::Recordable> OtlpExporter::MakeRecordable() noexcept
}

sdk::trace::ExportResult OtlpExporter::Export(
const opentelemetry::sdk::resource::Resource& resource,
const nostd::span<std::unique_ptr<sdk::trace::Recordable>> &spans) noexcept
{
proto::collector::trace::v1::ExportTraceServiceRequest request;

PopulateRequest(spans, &request);
PopulateRequest(resource, spans, &request);

grpc::ClientContext context;
proto::collector::trace::v1::ExportTraceServiceResponse response;
Expand Down
108 changes: 4 additions & 104 deletions exporters/otlp/src/recordable.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "opentelemetry/exporters/otlp/recordable.h"
#include "opentelemetry/exporters/otlp/shared_utils.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace otlp
{

const int kAttributeValueSize = 14;

void Recordable::SetIds(trace::TraceId trace_id,
trace::SpanId span_id,
trace::SpanId parent_span_id) noexcept
Expand All @@ -18,110 +17,11 @@ void Recordable::SetIds(trace::TraceId trace_id,
trace::SpanId::kSize);
}

void PopulateAttribute(opentelemetry::proto::common::v1::KeyValue *attribute,
nostd::string_view key,
const opentelemetry::common::AttributeValue &value)
{
// Assert size of variant to ensure that this method gets updated if the variant
// definition changes
static_assert(
nostd::variant_size<opentelemetry::common::AttributeValue>::value == kAttributeValueSize,
"AttributeValue contains unknown type");

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

if (nostd::holds_alternative<bool>(value))
{
attribute->mutable_value()->set_bool_value(nostd::get<bool>(value));
}
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<nostd::string_view>(value))
{
attribute->mutable_value()->set_string_value(nostd::get<nostd::string_view>(value).data(),
nostd::get<nostd::string_view>(value).size());
}
#ifdef HAVE_CSTRING_TYPE
else if (nostd::holds_alternative<const char *>(value))
{
attribute->mutable_value()->set_string_value(nostd::get<const char *>(value));
}
#endif
else if (nostd::holds_alternative<nostd::span<const bool>>(value))
{
for (const auto &val : nostd::get<nostd::span<const bool>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_bool_value(val);
}
}
else if (nostd::holds_alternative<nostd::span<const int>>(value))
{
for (const auto &val : nostd::get<nostd::span<const int>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
}
}
else if (nostd::holds_alternative<nostd::span<const int64_t>>(value))
{
for (const auto &val : nostd::get<nostd::span<const int64_t>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
}
}
else if (nostd::holds_alternative<nostd::span<const unsigned int>>(value))
{
for (const auto &val : nostd::get<nostd::span<const unsigned int>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
}
}
else if (nostd::holds_alternative<nostd::span<const uint64_t>>(value))
{
for (const auto &val : nostd::get<nostd::span<const uint64_t>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_int_value(val);
}
}
else if (nostd::holds_alternative<nostd::span<const double>>(value))
{
for (const auto &val : nostd::get<nostd::span<const double>>(value))
{
attribute->mutable_value()->mutable_array_value()->add_values()->set_double_value(val);
}
}
else if (nostd::holds_alternative<nostd::span<const nostd::string_view>>(value))
{
for (const auto &val : nostd::get<nostd::span<const nostd::string_view>>(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
{
auto *attribute = span_.add_attributes();
PopulateAttribute(attribute, key, value);
internal::PopulateAttribute(attribute, key, value);
}

void Recordable::AddEvent(nostd::string_view name,
Expand All @@ -133,7 +33,7 @@ void Recordable::AddEvent(nostd::string_view name,
event->set_time_unix_nano(timestamp.time_since_epoch().count());

attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept {
PopulateAttribute(event->add_attributes(), key, value);
internal::PopulateAttribute(event->add_attributes(), key, value);
return true;
});
}
Expand All @@ -147,7 +47,7 @@ void Recordable::AddLink(const opentelemetry::trace::SpanContext &span_context,
link->set_span_id(reinterpret_cast<const char *>(span_context.span_id().Id().data()),
trace::SpanId::kSize);
attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept {
PopulateAttribute(link->add_attributes(), key, value);
internal::PopulateAttribute(link->add_attributes(), key, value);
return true;
});

Expand Down
Loading