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

Add resource and instrumentation library support for OStreamSpanExporter #906

Merged
merged 2 commits into from
Jul 19, 2021
Merged
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 @@ -119,6 +119,12 @@ class OStreamSpanExporter final : public sdktrace::SpanExporter
void printEvents(const std::vector<sdktrace::SpanDataEvent> &events);

void printLinks(const std::vector<sdktrace::SpanDataLink> &links);

void printResources(const opentelemetry::sdk::resource::Resource &resources);

void printInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library);
};
} // namespace trace
} // namespace exporter
Expand Down
25 changes: 25 additions & 0 deletions exporters/ostream/src/span_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ sdk::common::ExportResult OStreamSpanExporter::Export(
printEvents(span->GetEvents());
sout_ << "\n links : ";
printLinks(span->GetLinks());
sout_ << "\n resources : ";
printResources(span->GetResource());
sout_ << "\n instr-lib : ";
printInstrumentationLibrary(span->GetInstrumentationLibrary());
sout_ << "\n}\n";
}
}
Expand Down Expand Up @@ -135,6 +139,27 @@ void OStreamSpanExporter::printLinks(const std::vector<sdktrace::SpanDataLink> &
}
}

void OStreamSpanExporter::printResources(const opentelemetry::sdk::resource::Resource &resources)
{
auto attributes = resources.GetAttributes();
if (attributes.size())
{
printAttributes(attributes, "\n\t");
}
}

void OStreamSpanExporter::printInstrumentationLibrary(
const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&instrumentation_library)
{
sout_ << instrumentation_library.GetName();
auto version = instrumentation_library.GetVersion();
if (version.size())
{
sout_ << "-" << version;
}
}

} // namespace trace
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
25 changes: 25 additions & 0 deletions exporters/ostream/test/ostream_span_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ namespace trace = opentelemetry::trace;
namespace common = opentelemetry::common;
namespace nostd = opentelemetry::nostd;
namespace sdktrace = opentelemetry::sdk::trace;
namespace resource = opentelemetry::sdk::resource;

using Attributes = std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>;

class TestResource : public resource::Resource
{
public:
TestResource(resource::ResourceAttributes attributes = resource::ResourceAttributes())
: resource::Resource(attributes)
{}
};

// Testing Shutdown functionality of OStreamSpanExporter, should expect no data to be sent to Stream
TEST(OStreamSpanExporter, Shutdown)
{
Expand Down Expand Up @@ -61,6 +70,8 @@ constexpr const char *kDefaultSpanPrinted =
" attributes : \n"
" events : \n"
" links : \n"
" resources : \n"
" instr-lib : unknown_service\n"
"}\n";

// Testing what a default span that is not changed will print out, either all 0's or empty values
Expand Down Expand Up @@ -109,6 +120,9 @@ TEST(OStreamSpanExporter, PrintSpanWithBasicFields)
recordable->SetStatus(opentelemetry::trace::StatusCode::kOk, "Test Description");
recordable->SetSpanKind(opentelemetry::trace::SpanKind::kClient);

TestResource resource1(opentelemetry::sdk::resource::ResourceAttributes({{"key1", "val1"}}));
recordable->SetResource(resource1);

processor->OnEnd(std::move(recordable));

std::string start = std::to_string(now.time_since_epoch().count());
Expand All @@ -130,6 +144,9 @@ TEST(OStreamSpanExporter, PrintSpanWithBasicFields)
" attributes : \n"
" events : \n"
" links : \n"
" resources : \n"
"\tkey1: val1\n"
" instr-lib : unknown_service\n"
"}\n";
EXPECT_EQ(output.str(), expectedOutput);
}
Expand Down Expand Up @@ -164,6 +181,8 @@ TEST(OStreamSpanExporter, PrintSpanWithAttribute)
"\tattr1: string\n"
" events : \n"
" links : \n"
" resources : \n"
" instr-lib : unknown_service\n"
"}\n";
EXPECT_EQ(output.str(), expectedOutput);
}
Expand Down Expand Up @@ -200,6 +219,8 @@ TEST(OStreamSpanExporter, PrintSpanWithArrayAttribute)
"\tarray1: [1,2,3]\n"
" events : \n"
" links : \n"
" resources : \n"
" instr-lib : unknown_service\n"
"}\n";
EXPECT_EQ(output.str(), expectedOutput);
}
Expand Down Expand Up @@ -256,6 +277,8 @@ TEST(OStreamSpanExporter, PrintSpanWithEvents)
"\t\tattr1: string\n"
"\t}\n"
" links : \n"
" resources : \n"
" instr-lib : unknown_service\n"
"}\n";
EXPECT_EQ(output.str(), expectedOutput);
}
Expand Down Expand Up @@ -327,6 +350,8 @@ TEST(OStreamSpanExporter, PrintSpanWithLinks)
"\t attributes : \n"
"\t\tattr1: string\n"
"\t}\n"
" resources : \n"
" instr-lib : unknown_service\n"
"}\n";
EXPECT_EQ(output.str(), expectedOutput);
}
Expand Down
33 changes: 32 additions & 1 deletion sdk/include/opentelemetry/sdk/trace/span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class SpanDataLink
class SpanData final : public Recordable
{
public:
SpanData() : resource_{nullptr}, instrumentation_library_{nullptr} {}
/**
* Get the trace id for this span
* @return the trace id for this span
Expand Down Expand Up @@ -152,7 +153,37 @@ class SpanData final : public Recordable
* @returns the attributes associated with the resource configured for TracerProvider
*/

const opentelemetry::sdk::resource::Resource &GetResource() const noexcept { return *resource_; }
const opentelemetry::sdk::resource::Resource &GetResource() const noexcept
{
if (resource_ == nullptr)
{
// this shouldn't happen as TraceProvider provides default resources
Copy link
Contributor

Choose a reason for hiding this comment

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

Add assert here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's one of the options. I just felt it's better to avoid asserting application if we can handle it gracefully. That also let us create unit-tests for ostream exporter ( which uses span-data) without depending on TracerProvider to pass the resources.

static opentelemetry::sdk::resource::Resource resource =
opentelemetry::sdk::resource::Resource::GetEmpty();
return resource;
}
return *resource_;
}

/**
* Get the attributes associated with the resource
* @returns the attributes associated with the resource configured for TracerProvider
*/

const opentelemetry::sdk::trace::InstrumentationLibrary &GetInstrumentationLibrary()
const noexcept
{
if (instrumentation_library_ == nullptr)
{
// this shouldn't happen as Tracer ensures there is valid default instrumentation library.
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto.

static std::unique_ptr<opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary>
instrumentation_library =
opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create(
"unknown_service");
return *instrumentation_library;
}
return *instrumentation_library_;
}

/**
* Get the start time for this span
Expand Down