Skip to content

Commit

Permalink
Add resource and instrumentation library support for OStreamSpanExpor…
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored Jul 19, 2021
1 parent 79c4c71 commit ecb9a27
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
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
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.
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

0 comments on commit ecb9a27

Please sign in to comment.