diff --git a/opentracing-shim/include/span_context_shim.h b/opentracing-shim/include/span_context_shim.h index b2db5ed6c8..12d2f57acc 100644 --- a/opentracing-shim/include/span_context_shim.h +++ b/opentracing-shim/include/span_context_shim.h @@ -18,33 +18,20 @@ using BaggagePtr = nostd::shared_ptr; class SpanContextShim final : public opentracing::SpanContext { public: - - explicit SpanContextShim(const opentelemetry::trace::SpanContext& context, const BaggagePtr& baggage) : - context_(context), baggage_(baggage) {} - - inline SpanContextShim operator=(const SpanContextShim& other) - { - return SpanContextShim(other.context_, other.baggage_); - } - + explicit SpanContextShim(const opentelemetry::trace::SpanContext& context, const BaggagePtr& baggage) + : context_(context), baggage_(baggage) {} inline const opentelemetry::trace::SpanContext& context() const { return context_; } - inline const BaggagePtr& baggage() const { return baggage_; } - - SpanContextShim newWithKeyValue(const nostd::string_view &key, const nostd::string_view &value) const noexcept; - + SpanContextShim newWithKeyValue(nostd::string_view key, nostd::string_view value) const noexcept; bool BaggageItem(nostd::string_view key, std::string& value) const noexcept; - + // Overrides using VisitBaggageItem = std::function; void ForeachBaggageItem(VisitBaggageItem f) const override; - std::unique_ptr Clone() const noexcept override; private: - - const opentelemetry::trace::SpanContext context_; - const BaggagePtr baggage_; - + opentelemetry::trace::SpanContext context_; + BaggagePtr baggage_; }; } // namespace opentracingshim diff --git a/opentracing-shim/include/span_shim.h b/opentracing-shim/include/span_shim.h index a6bbf67c12..fe341debbf 100644 --- a/opentracing-shim/include/span_shim.h +++ b/opentracing-shim/include/span_shim.h @@ -24,12 +24,10 @@ using EventEntry = std::pair; class SpanShim : public opentracing::Span { public: - - explicit SpanShim(const TracerShim& tracer, const SpanPtr& span, const BaggagePtr& baggage) : - tracer_(tracer), span_(span), context_(span->GetContext(), baggage) {} - + explicit SpanShim(const TracerShim& tracer, const SpanPtr& span, const BaggagePtr& baggage) + : tracer_(tracer), span_(span), context_(span->GetContext(), baggage) {} void handleError(const opentracing::Value& value) noexcept; - + // Overrides void FinishWithOptions(const opentracing::FinishSpanOptions& finish_span_options) noexcept override; void SetOperationName(opentracing::string_view name) noexcept override; void SetTag(opentracing::string_view key, const opentracing::Value& value) noexcept override; @@ -42,14 +40,12 @@ class SpanShim : public opentracing::Span inline const opentracing::Tracer& tracer() const noexcept override { return tracer_; }; private: - void logImpl(opentracing::SystemTime timestamp, nostd::span fields) noexcept; const TracerShim& tracer_; SpanPtr span_; SpanContextShim context_; mutable opentelemetry::common::SpinLockMutex context_lock_; - }; } // namespace opentracingshim diff --git a/opentracing-shim/include/tracer_shim.h b/opentracing-shim/include/tracer_shim.h index aada00980e..e91b8a2b03 100644 --- a/opentracing-shim/include/tracer_shim.h +++ b/opentracing-shim/include/tracer_shim.h @@ -19,14 +19,13 @@ using TracerProviderPtr = nostd::shared_ptr; struct OpenTracingPropagators { - PropagatorPtr textMap; - PropagatorPtr httpHeaders; + PropagatorPtr text_map; + PropagatorPtr http_headers; }; class TracerShim : public opentracing::Tracer { public: - /** * Creates a {@code opentracing::Tracer} shim out of * {@code Provider::GetTracerProvider()} and @@ -66,7 +65,7 @@ class TracerShim : public opentracing::Tracer { return std::shared_ptr(new (std::nothrow) TracerShim(tracer, propagators)); } - + // Overrides std::unique_ptr StartSpanWithOptions(opentracing::string_view operation_name, const opentracing::StartSpanOptions& options) const noexcept override; opentracing::expected Inject(const opentracing::SpanContext& sc, @@ -81,19 +80,16 @@ class TracerShim : public opentracing::Tracer inline void Close() noexcept override { is_closed_ = true; }; private: - explicit TracerShim(const TracerPtr& tracer, const OpenTracingPropagators& propagators) : tracer_(tracer), propagators_(propagators) {} - template opentracing::expected injectImpl(const opentracing::SpanContext& sc, const T& writer, const PropagatorPtr& propagator) const; - template opentracing::expected> extractImpl(const T& reader, const PropagatorPtr& propagator) const; - + TracerPtr tracer_; OpenTracingPropagators propagators_; bool is_closed_ = false; diff --git a/opentracing-shim/src/span_context_shim.cc b/opentracing-shim/src/span_context_shim.cc index d220634701..9d1c60618c 100644 --- a/opentracing-shim/src/span_context_shim.cc +++ b/opentracing-shim/src/span_context_shim.cc @@ -6,10 +6,10 @@ #include "span_context_shim.h" OPENTELEMETRY_BEGIN_NAMESPACE -namespace opentracingshim +namespace opentracingshim { -SpanContextShim SpanContextShim::newWithKeyValue(const nostd::string_view &key, const nostd::string_view &value) const noexcept +SpanContextShim SpanContextShim::newWithKeyValue(nostd::string_view key, nostd::string_view value) const noexcept { return SpanContextShim{context_, baggage_->Set(key, value)}; } @@ -22,8 +22,8 @@ bool SpanContextShim::BaggageItem(nostd::string_view key, std::string& value) co void SpanContextShim::ForeachBaggageItem(VisitBaggageItem f) const { baggage_->GetAllEntries([&f](nostd::string_view key, nostd::string_view value) - { - return f(key.data(), value.data()); + { + return f(key.data(), value.data()); } ); } diff --git a/opentracing-shim/src/span_shim.cc b/opentracing-shim/src/span_shim.cc index 6600872494..7ba644215b 100644 --- a/opentracing-shim/src/span_shim.cc +++ b/opentracing-shim/src/span_shim.cc @@ -13,29 +13,35 @@ #include OPENTELEMETRY_BEGIN_NAMESPACE -namespace opentracingshim +namespace opentracingshim { void SpanShim::handleError(const opentracing::Value& value) noexcept { using opentelemetry::trace::StatusCode; - + // The error tag MUST be mapped to StatusCode: + // - true maps to Error. + // - false maps to Ok + // - no value being set maps to Unset. auto code = StatusCode::kUnset; const auto& str_value = shimutils::stringFromValue(value); - if (str_value == "true") + + if (str_value == "true") { code = StatusCode::kError; } - else if (str_value == "false") + else if (str_value == "false") { code = StatusCode::kOk; } span_->SetStatus(code); -} +} void SpanShim::FinishWithOptions(const opentracing::FinishSpanOptions& finish_span_options) noexcept { + // If an explicit timestamp is specified, a conversion MUST + // be done to match the OpenTracing and OpenTelemetry units. span_->End({{ finish_span_options.finish_steady_timestamp }}); } @@ -46,6 +52,7 @@ void SpanShim::SetOperationName(opentracing::string_view name) noexcept void SpanShim::SetTag(opentracing::string_view key, const opentracing::Value& value) noexcept { + // Calls Set Attribute on the underlying OpenTelemetry Span with the specified key/value pair. if (key == opentracing::ext::error) { handleError(value); @@ -58,12 +65,16 @@ void SpanShim::SetTag(opentracing::string_view key, const opentracing::Value& va void SpanShim::SetBaggageItem(opentracing::string_view restricted_key, opentracing::string_view value) noexcept { + // Creates a new SpanContext Shim with a new OpenTelemetry Baggage containing the specified + // Baggage key/value pair, and sets it as the current instance for this Span Shim. const std::lock_guard guard(context_lock_); - context_ = context_.newWithKeyValue(restricted_key.data(), value.data()); + context_ = context_.newWithKeyValue(restricted_key.data(), value.data());; } std::string SpanShim::BaggageItem(opentracing::string_view restricted_key) const noexcept { + // Returns the value for the specified key in the OpenTelemetry Baggage + // of the current SpanContext Shim, or null if none exists. const std::lock_guard guard(context_lock_); std::string value; return context_.BaggageItem(restricted_key.data(), value) ? value : ""; @@ -71,59 +82,72 @@ std::string SpanShim::BaggageItem(opentracing::string_view restricted_key) const void SpanShim::Log(std::initializer_list fields) noexcept { + // If an explicit timestamp is specified, a conversion MUST + // be done to match the OpenTracing and OpenTelemetry units. logImpl(opentracing::SystemTime::min(), fields); } void SpanShim::Log(opentracing::SystemTime timestamp, std::initializer_list fields) noexcept { + // If an explicit timestamp is specified, a conversion MUST + // be done to match the OpenTracing and OpenTelemetry units. logImpl(timestamp, fields); } void SpanShim::Log(opentracing::SystemTime timestamp, const std::vector& fields) noexcept { + // If an explicit timestamp is specified, a conversion MUST + // be done to match the OpenTracing and OpenTelemetry units. logImpl(timestamp, fields); } -void SpanShim::logImpl(opentracing::SystemTime timestamp, nostd::span fields) noexcept +void SpanShim::logImpl(opentracing::SystemTime timestamp, nostd::span fields) noexcept { - const auto event = std::find_if(fields.begin(), fields.end(), [](EventEntry item){ return item.first == "event"; }); + // The Add Event’s name parameter MUST be the value with the event key + // in the pair set, or else fallback to use the log literal string. + const auto event = std::find_if(fields.begin(), fields.end(), [](EventEntry item){ return item.first == "event"; }); auto name = (event != fields.end()) ? shimutils::stringFromValue(event->second) : std::string{"log"}; - + // If pair set contains an event=error entry, the values MUST be mapped to an Event + // with the conventions outlined in the Exception semantic conventions document: bool is_error = (name == opentracing::ext::error); + // A call to AddEvent is performed with name being set to exception if (is_error) name = "exception"; - - std::vector> attributes; + // Along the specified key/value pair set as additional event attributes... + std::vector> attributes; attributes.reserve(fields.size()); - - for (const auto& entry : fields) + + for (const auto& entry : fields) { auto key = entry.first; const auto& value = shimutils::attributeFromValue(entry.second); - - if (is_error) + // ... including mapping of the following key/value pairs: + // - error.kind maps to exception.type. + // - message maps to exception.message. + // - stack maps to exception.stacktrace. + if (is_error) { - if (key == "error.kind") + if (key == "error.kind") { key = opentelemetry::trace::SemanticConventions::kExceptionType; - } - else if (key == "message") + } + else if (key == "message") { key = opentelemetry::trace::SemanticConventions::kExceptionMessage; - } - else if (key == "stack") + } + else if (key == "stack") { key = opentelemetry::trace::SemanticConventions::kExceptionStacktrace; } } - + attributes.emplace_back(key, value); } - - if (timestamp != opentracing::SystemTime::min()) + // Calls Add Events on the underlying OpenTelemetry Span with the specified key/value pair set. + if (timestamp != opentracing::SystemTime::min()) { span_->AddEvent(name, timestamp, attributes); - } - else + } + else { span_->AddEvent(name, attributes); } diff --git a/opentracing-shim/src/tracer_shim.cc b/opentracing-shim/src/tracer_shim.cc index ac5cd31ef9..2b8ff88b8d 100644 --- a/opentracing-shim/src/tracer_shim.cc +++ b/opentracing-shim/src/tracer_shim.cc @@ -14,7 +14,7 @@ #include OPENTELEMETRY_BEGIN_NAMESPACE -namespace opentracingshim +namespace opentracingshim { namespace detail @@ -28,30 +28,30 @@ static opentelemetry::trace::StartSpanOptions makeOptionsShim(const opentracing: using opentracing::SpanReferenceType; opentelemetry::trace::StartSpanOptions options_shim; - // If an explicit start timestamp is specified, a conversion MUST + // If an explicit start timestamp is specified, a conversion MUST // be done to match the OpenTracing and OpenTelemetry units. options_shim.start_system_time = opentelemetry::common::SystemTimestamp{options.start_system_timestamp}; options_shim.start_steady_time = opentelemetry::common::SteadyTimestamp{options.start_steady_timestamp}; - + const auto& refs = options.references; // If a list of Span references is specified... if (!refs.empty()) { - auto first_child_of = std::find_if(refs.cbegin(), refs.cend(), - [](const std::pair& entry){ - return entry.first == SpanReferenceType::ChildOfRef; + auto first_child_of = std::find_if(refs.cbegin(), refs.cend(), + [](const std::pair& entry){ + return entry.first == SpanReferenceType::ChildOfRef; }); - // The first SpanContext with Child Of type in the entire list is used as parent, + // The first SpanContext with Child Of type in the entire list is used as parent, // else the first SpanContext is used as parent auto context = (first_child_of != refs.cend()) ? first_child_of->second : refs.cbegin()->second; - - if (auto context_shim = dynamic_cast(context)) + + if (auto context_shim = dynamic_cast(context)) { options_shim.parent = context_shim->context(); } } - + return options_shim; } @@ -59,17 +59,17 @@ static LinksList makeReferenceLinks(const opentracing::StartSpanOptions& options { using opentracing::SpanReferenceType; using namespace opentelemetry::trace::SemanticConventions; - + LinksList links; links.reserve(options.references.size()); // All values in the list MUST be added as Links with the reference type value // as a Link attribute, i.e. opentracing.ref_type set to follows_from or child_of - for (const auto& entry : options.references) + for (const auto& entry : options.references) { auto context_shim = dynamic_cast(entry.second); nostd::string_view span_kind; - + if (entry.first == SpanReferenceType::ChildOfRef) { span_kind = OpentracingRefTypeValues::kChildOf; @@ -79,7 +79,7 @@ static LinksList makeReferenceLinks(const opentracing::StartSpanOptions& options span_kind = OpentracingRefTypeValues::kFollowsFrom; } - if (context_shim && !span_kind.empty()) + if (context_shim && !span_kind.empty()) { // links.push_back({ context_shim->context(), {{ opentracing::ext::span_kind.data(), span_kind }} }); links.emplace_back(std::piecewise_construct, @@ -98,7 +98,7 @@ static BaggagePtr makeBaggage(const opentracing::StartSpanOptions& options) noex std::unordered_map baggage_items; // If a list of Span references is specified... - for (const auto& entry : options.references) + for (const auto& entry : options.references) { if (auto context_shim = dynamic_cast(entry.second)) { @@ -114,7 +114,7 @@ static BaggagePtr makeBaggage(const opentracing::StartSpanOptions& options) noex } } - // If no such lisf of references is specified, the current Baggage + // If no such lisf of references is specified, the current Baggage // MUST be used as the initial value of the newly created Span. return baggage_items.empty() ? GetBaggage(opentelemetry::context::RuntimeContext::GetCurrent()) @@ -126,7 +126,7 @@ static std::vector> makeTags(cons std::vector> tags; tags.reserve(options.tags.size()); - // If an initial set of tags is specified, the values MUST + // If an initial set of tags is specified, the values MUST // be set at the creation time of the OpenTelemetry Span. for (const auto& entry : options.tags) { @@ -138,26 +138,25 @@ static std::vector> makeTags(cons } // namespace opentracingshim::detail -std::unique_ptr TracerShim::StartSpanWithOptions(opentracing::string_view operation_name, +std::unique_ptr TracerShim::StartSpanWithOptions(opentracing::string_view operation_name, const opentracing::StartSpanOptions& options) const noexcept { if (is_closed_) return nullptr; - + const auto& opts = detail::makeOptionsShim(options); const auto& links = detail::makeReferenceLinks(options); const auto& baggage = detail::makeBaggage(options); const auto& attributes = detail::makeTags(options); - + auto span = tracer_->StartSpan(operation_name.data(), attributes, links, opts); auto span_shim = new SpanShim(*this, span, baggage); // If an initial set of tags is specified and the OpenTracing error tag // is included after the OpenTelemetry Span was created. - const auto& error_entry = std::find_if(options.tags.begin(), options.tags.end(), + const auto& error_entry = std::find_if(options.tags.begin(), options.tags.end(), [](const std::pair& entry){ return entry.first == opentracing::ext::error; }); - // The Shim layer MUST perform the same error handling as described in the Set Tag operation if (error_entry != options.tags.end()) { span_shim->handleError(error_entry->second); @@ -169,7 +168,7 @@ std::unique_ptr TracerShim::StartSpanWithOptions(opentracing: opentracing::expected TracerShim::Inject(const opentracing::SpanContext& sc, std::ostream& writer) const { - // Errors MAY be raised if the specified Format is not recognized, + // Errors MAY be raised if the specified Format is not recognized, // depending on the specific OpenTracing Language API. return opentracing::make_unexpected(opentracing::invalid_carrier_error); } @@ -177,10 +176,10 @@ opentracing::expected TracerShim::Inject(const opentracing::SpanContext& s opentracing::expected TracerShim::Inject(const opentracing::SpanContext& sc, const opentracing::TextMapWriter& writer) const { - // TextMap and HttpHeaders formats MUST use their explicitly specified TextMapPropagator, + // TextMap and HttpHeaders formats MUST use their explicitly specified TextMapPropagator, // if any, or else use the global TextMapPropagator. - const auto& propagator = propagators_.textMap - ? propagators_.textMap + const auto& propagator = propagators_.text_map + ? propagators_.text_map : opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); return injectImpl(sc, writer, propagator); @@ -189,10 +188,10 @@ opentracing::expected TracerShim::Inject(const opentracing::SpanContext& s opentracing::expected TracerShim::Inject(const opentracing::SpanContext& sc, const opentracing::HTTPHeadersWriter& writer) const { - // TextMap and HttpHeaders formats MUST use their explicitly specified TextMapPropagator, + // TextMap and HttpHeaders formats MUST use their explicitly specified TextMapPropagator, // if any, or else use the global TextMapPropagator. - const auto& propagator = propagators_.httpHeaders - ? propagators_.httpHeaders + const auto& propagator = propagators_.http_headers + ? propagators_.http_headers : opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); return injectImpl(sc, writer, propagator); @@ -200,39 +199,39 @@ opentracing::expected TracerShim::Inject(const opentracing::SpanContext& s opentracing::expected> TracerShim::Extract(std::istream& reader) const { - // Errors MAY be raised if either the Format is not recognized or no value + // Errors MAY be raised if either the Format is not recognized or no value // could be extracted, depending on the specific OpenTracing Language API. return opentracing::make_unexpected(opentracing::invalid_carrier_error); } opentracing::expected> TracerShim::Extract(const opentracing::TextMapReader& reader) const { - // TextMap and HttpHeaders formats MUST use their explicitly specified TextMapPropagator, + // TextMap and HttpHeaders formats MUST use their explicitly specified TextMapPropagator, // if any, or else use the global TextMapPropagator. - const auto& propagator = propagators_.textMap - ? propagators_.textMap + const auto& propagator = propagators_.text_map + ? propagators_.text_map : opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); - + return extractImpl(reader, propagator); } opentracing::expected> TracerShim::Extract(const opentracing::HTTPHeadersReader& reader) const { - // TextMap and HttpHeaders formats MUST use their explicitly specified TextMapPropagator, + // TextMap and HttpHeaders formats MUST use their explicitly specified TextMapPropagator, // if any, or else use the global TextMapPropagator. - const auto& propagator = propagators_.httpHeaders - ? propagators_.httpHeaders + const auto& propagator = propagators_.http_headers + ? propagators_.http_headers : opentelemetry::context::propagation::GlobalTextMapPropagator::GetGlobalPropagator(); - + return extractImpl(reader, propagator); } template -opentracing::expected TracerShim::injectImpl(const opentracing::SpanContext& sc, - const T& writer, +opentracing::expected TracerShim::injectImpl(const opentracing::SpanContext& sc, + const T& writer, const PropagatorPtr& propagator) const { - // Inject the underlying OpenTelemetry Span and Baggage using either the explicitly registered + // Inject the underlying OpenTelemetry Span and Baggage using either the explicitly registered // or the global OpenTelemetry Propagators, as configured at construction time. if (auto context_shim = dynamic_cast(&sc)) { @@ -248,10 +247,10 @@ opentracing::expected TracerShim::injectImpl(const opentracing::SpanContex } template -opentracing::expected> TracerShim::extractImpl(const T& reader, +opentracing::expected> TracerShim::extractImpl(const T& reader, const PropagatorPtr& propagator) const { - // Extract the underlying OpenTelemetry Span and Baggage using either the explicitly registered + // Extract the underlying OpenTelemetry Span and Baggage using either the explicitly registered // or the global OpenTelemetry Propagators, as configured at construction time. shimutils::CarrierReaderShim carrier{reader}; auto current_context = opentelemetry::context::RuntimeContext::GetCurrent(); @@ -259,8 +258,8 @@ opentracing::expected> TracerShim::ext auto span_context = opentelemetry::trace::GetSpan(context)->GetContext(); auto baggage = opentelemetry::baggage::GetBaggage(context); - // If the extracted SpanContext is invalid AND the extracted Baggage is empty, - // this operation MUST return a null value, and otherwise it MUST return a + // If the extracted SpanContext is invalid AND the extracted Baggage is empty, + // this operation MUST return a null value, and otherwise it MUST return a // SpanContext Shim instance with the extracted values. SpanContextShim* context_shim = (!span_context.IsValid() && shimutils::isBaggageEmpty(baggage)) ? nullptr