From 7f5252f2373df4b2d545061540e113a571d9db80 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Wed, 28 Apr 2021 12:19:53 -0700 Subject: [PATCH] Populate InstrumentationLibrary to exporters (#700) --- .../opentelemetry/exporters/otlp/recordable.h | 4 ++++ exporters/otlp/src/recordable.cc | 8 ++++++++ .../opentelemetry/exporters/zipkin/recordable.h | 4 ++++ exporters/zipkin/src/recordable.cc | 8 ++++++++ exporters/zipkin/test/zipkin_recordable_test.cc | 15 +++++++++++++++ .../ext/zpages/threadsafe_span_data.h | 13 ++++++++++++- sdk/include/opentelemetry/sdk/trace/recordable.h | 11 +++++++++++ sdk/include/opentelemetry/sdk/trace/span_data.h | 7 +++++++ sdk/src/trace/span.cc | 1 + 9 files changed, 70 insertions(+), 1 deletion(-) diff --git a/exporters/otlp/include/opentelemetry/exporters/otlp/recordable.h b/exporters/otlp/include/opentelemetry/exporters/otlp/recordable.h index 1f2d0391a9..b58e1ae6e1 100644 --- a/exporters/otlp/include/opentelemetry/exporters/otlp/recordable.h +++ b/exporters/otlp/include/opentelemetry/exporters/otlp/recordable.h @@ -42,6 +42,10 @@ class Recordable final : public sdk::trace::Recordable void SetDuration(std::chrono::nanoseconds duration) noexcept override; + void SetInstrumentationLibrary( + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library) noexcept override; + private: proto::trace::v1::Span span_; }; diff --git a/exporters/otlp/src/recordable.cc b/exporters/otlp/src/recordable.cc index 3e1c2443de..ccec6eaf17 100644 --- a/exporters/otlp/src/recordable.cc +++ b/exporters/otlp/src/recordable.cc @@ -223,6 +223,14 @@ 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); } + +void Recordable::SetInstrumentationLibrary( + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library) noexcept +{ + // TODO: add instrumentation library to OTLP exporter. +} + } // namespace otlp } // namespace exporter OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/zipkin/include/opentelemetry/exporters/zipkin/recordable.h b/exporters/zipkin/include/opentelemetry/exporters/zipkin/recordable.h index 3e4fbfa715..f597a45400 100644 --- a/exporters/zipkin/include/opentelemetry/exporters/zipkin/recordable.h +++ b/exporters/zipkin/include/opentelemetry/exporters/zipkin/recordable.h @@ -61,6 +61,10 @@ class Recordable final : public sdk::trace::Recordable void SetDuration(std::chrono::nanoseconds duration) noexcept override; + void SetInstrumentationLibrary( + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library) noexcept override; + private: ZipkinSpan span_; }; diff --git a/exporters/zipkin/src/recordable.cc b/exporters/zipkin/src/recordable.cc index ae2e450012..283e02ed45 100644 --- a/exporters/zipkin/src/recordable.cc +++ b/exporters/zipkin/src/recordable.cc @@ -215,6 +215,14 @@ void Recordable::SetDuration(std::chrono::nanoseconds duration) noexcept span_["duration"] = duration.count(); } +void Recordable::SetInstrumentationLibrary( + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library) noexcept +{ + span_["tags"]["otel.library.name"] = instrumentation_library.GetName(); + span_["tags"]["otel.library.version"] = instrumentation_library.GetVersion(); +} + void Recordable::SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept {} } // namespace zipkin } // namespace exporter diff --git a/exporters/zipkin/test/zipkin_recordable_test.cc b/exporters/zipkin/test/zipkin_recordable_test.cc index be48e3fd4d..f189d04d9c 100644 --- a/exporters/zipkin/test/zipkin_recordable_test.cc +++ b/exporters/zipkin/test/zipkin_recordable_test.cc @@ -95,6 +95,21 @@ TEST(ZipkinSpanRecordable, SetDuration) EXPECT_EQ(rec.span(), j_span); } +TEST(ZipkinSpanRecordable, SetInstrumentationLibrary) +{ + using InstrumentationLibrary = opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary; + + const char *library_name = "otel-cpp"; + const char *library_version = "0.5.0"; + json j_span = { + {"tags", {{"otel.library.name", library_name}, {"otel.library.version", library_version}}}}; + opentelemetry::exporter::zipkin::Recordable rec; + + rec.SetInstrumentationLibrary(*InstrumentationLibrary::create(library_name, library_version)); + + EXPECT_EQ(rec.span(), j_span); +} + TEST(ZipkinSpanRecordable, SetStatus) { std::string description = "Error description"; diff --git a/ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h b/ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h index c2b092bb1e..009719f94b 100644 --- a/ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h +++ b/ext/include/opentelemetry/ext/zpages/threadsafe_span_data.h @@ -172,6 +172,14 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable duration_ = duration; } + void SetInstrumentationLibrary( + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + &instrumentation_library) noexcept override + { + std::lock_guard lock(mutex_); + instrumentation_library_ = &instrumentation_library; + } + void AddLink(const opentelemetry::trace::SpanContext &span_context, const opentelemetry::common::KeyValueIterable &attributes = opentelemetry::common::KeyValueIterableView>( @@ -212,7 +220,8 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable status_desc_(threadsafe_span_data.status_desc_), attributes_(threadsafe_span_data.attributes_), events_(threadsafe_span_data.events_), - converter_(threadsafe_span_data.converter_) + converter_(threadsafe_span_data.converter_), + instrumentation_library_(threadsafe_span_data.instrumentation_library_) {} mutable std::mutex mutex_; @@ -227,6 +236,8 @@ class ThreadsafeSpanData final : public opentelemetry::sdk::trace::Recordable std::unordered_map attributes_; std::vector events_; opentelemetry::sdk::common::AttributeConverter converter_; + const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary + *instrumentation_library_; }; } // namespace zpages } // namespace ext diff --git a/sdk/include/opentelemetry/sdk/trace/recordable.h b/sdk/include/opentelemetry/sdk/trace/recordable.h index 24bca38e3f..3132c0d350 100644 --- a/sdk/include/opentelemetry/sdk/trace/recordable.h +++ b/sdk/include/opentelemetry/sdk/trace/recordable.h @@ -5,6 +5,7 @@ #include "opentelemetry/common/timestamp.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/empty_attributes.h" +#include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" #include "opentelemetry/trace/canonical_code.h" #include "opentelemetry/trace/span.h" #include "opentelemetry/trace/span_context.h" @@ -21,6 +22,9 @@ namespace sdk { namespace trace { + +using namespace opentelemetry::sdk::instrumentationlibrary; + /** * Maintains a representation of a span in a format that can be processed by a recorder. * @@ -125,6 +129,13 @@ class Recordable * @param duration the duration to set */ virtual void SetDuration(std::chrono::nanoseconds duration) noexcept = 0; + + /** + * Set the instrumentation library of the span. + * @param instrumentation_library the instrumentation library to set + */ + virtual void SetInstrumentationLibrary( + const InstrumentationLibrary &instrumentation_library) noexcept = 0; }; } // namespace trace } // namespace sdk diff --git a/sdk/include/opentelemetry/sdk/trace/span_data.h b/sdk/include/opentelemetry/sdk/trace/span_data.h index 4bc7ab8dda..d92fb7a049 100644 --- a/sdk/include/opentelemetry/sdk/trace/span_data.h +++ b/sdk/include/opentelemetry/sdk/trace/span_data.h @@ -232,6 +232,12 @@ class SpanData final : public Recordable void SetDuration(std::chrono::nanoseconds duration) noexcept override { duration_ = duration; } + void SetInstrumentationLibrary( + const InstrumentationLibrary &instrumentation_library) noexcept override + { + instrumentation_library_ = &instrumentation_library; + } + private: opentelemetry::trace::SpanContext span_context_{false, false}; opentelemetry::trace::SpanId parent_span_id_; @@ -244,6 +250,7 @@ class SpanData final : public Recordable std::vector events_; std::vector links_; opentelemetry::trace::SpanKind span_kind_{opentelemetry::trace::SpanKind::kInternal}; + const InstrumentationLibrary *instrumentation_library_; }; } // namespace trace } // namespace sdk diff --git a/sdk/src/trace/span.cc b/sdk/src/trace/span.cc index f890f9b9cd..deb9487066 100644 --- a/sdk/src/trace/span.cc +++ b/sdk/src/trace/span.cc @@ -59,6 +59,7 @@ Span::Span(std::shared_ptr &&tracer, return; } recordable_->SetName(name); + recordable_->SetInstrumentationLibrary(tracer_->GetInstrumentationLibrary()); trace_api::TraceId trace_id; trace_api::SpanId span_id = tracer_->GetIdGenerator().GenerateSpanId();