diff --git a/exporters/ostream/include/opentelemetry/exporters/ostream/span_exporter.h b/exporters/ostream/include/opentelemetry/exporters/ostream/span_exporter.h index 2224e828a6..52849b5c18 100644 --- a/exporters/ostream/include/opentelemetry/exporters/ostream/span_exporter.h +++ b/exporters/ostream/include/opentelemetry/exporters/ostream/span_exporter.h @@ -119,6 +119,12 @@ class OStreamSpanExporter final : public sdktrace::SpanExporter void printEvents(const std::vector &events); void printLinks(const std::vector &links); + + void printResources(const opentelemetry::sdk::resource::Resource &resources); + + void printInstrumentationLibrary( + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library); }; } // namespace trace } // namespace exporter diff --git a/exporters/ostream/src/span_exporter.cc b/exporters/ostream/src/span_exporter.cc index 98eb86bdda..cbf3e7a5af 100644 --- a/exporters/ostream/src/span_exporter.cc +++ b/exporters/ostream/src/span_exporter.cc @@ -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"; } } @@ -135,6 +139,27 @@ void OStreamSpanExporter::printLinks(const std::vector & } } +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 diff --git a/exporters/ostream/test/ostream_span_test.cc b/exporters/ostream/test/ostream_span_test.cc index b4b387ed50..c72372adc7 100644 --- a/exporters/ostream/test/ostream_span_test.cc +++ b/exporters/ostream/test/ostream_span_test.cc @@ -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>; +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) { @@ -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 @@ -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()); @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/sdk/include/opentelemetry/sdk/trace/span_data.h b/sdk/include/opentelemetry/sdk/trace/span_data.h index 6a6d823e51..0fb09f3284 100644 --- a/sdk/include/opentelemetry/sdk/trace/span_data.h +++ b/sdk/include/opentelemetry/sdk/trace/span_data.h @@ -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 @@ -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 + instrumentation_library = + opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary::Create( + "unknown_service"); + return *instrumentation_library; + } + return *instrumentation_library_; + } /** * Get the start time for this span