Program Listing for File span_data.h

Return to documentation for file (/home/docs/checkouts/readthedocs.org/user_builds/opentelemetry-cpp/checkouts/v1.6.1/sdk/include/opentelemetry/sdk/trace/span_data.h)

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <chrono>
#include <unordered_map>
#include <vector>
#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/common/macros.h"
#include "opentelemetry/common/timestamp.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/sdk/common/attribute_utils.h"
#include "opentelemetry/sdk/trace/recordable.h"
#include "opentelemetry/trace/canonical_code.h"
#include "opentelemetry/trace/span.h"
#include "opentelemetry/trace/span_id.h"
#include "opentelemetry/trace/trace_id.h"

#include <string>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace trace
{
class SpanDataEvent
{
public:
  SpanDataEvent(std::string name,
                opentelemetry::common::SystemTimestamp timestamp,
                const opentelemetry::common::KeyValueIterable &attributes)
      : name_(name), timestamp_(timestamp), attribute_map_(attributes)
  {}

  std::string GetName() const noexcept { return name_; }

  opentelemetry::common::SystemTimestamp GetTimestamp() const noexcept { return timestamp_; }

  const std::unordered_map<std::string, common::OwnedAttributeValue> &GetAttributes() const noexcept
  {
    return attribute_map_.GetAttributes();
  }

private:
  std::string name_;
  opentelemetry::common::SystemTimestamp timestamp_;
  common::AttributeMap attribute_map_;
};

class SpanDataLink
{
public:
  SpanDataLink(opentelemetry::trace::SpanContext span_context,
               const opentelemetry::common::KeyValueIterable &attributes)
      : span_context_(span_context), attribute_map_(attributes)
  {}

  const std::unordered_map<std::string, common::OwnedAttributeValue> &GetAttributes() const noexcept
  {
    return attribute_map_.GetAttributes();
  }

  const opentelemetry::trace::SpanContext &GetSpanContext() const noexcept { return span_context_; }

private:
  opentelemetry::trace::SpanContext span_context_;
  common::AttributeMap attribute_map_;
};

class SpanData final : public Recordable
{
public:
  SpanData() : resource_{nullptr}, instrumentation_scope_{nullptr} {}
  opentelemetry::trace::TraceId GetTraceId() const noexcept { return span_context_.trace_id(); }

  opentelemetry::trace::SpanId GetSpanId() const noexcept { return span_context_.span_id(); }

  const opentelemetry::trace::SpanContext &GetSpanContext() const noexcept { return span_context_; }

  opentelemetry::trace::SpanId GetParentSpanId() const noexcept { return parent_span_id_; }

  opentelemetry::nostd::string_view GetName() const noexcept { return name_; }

  opentelemetry::trace::SpanKind GetSpanKind() const noexcept { return span_kind_; }

  opentelemetry::trace::StatusCode GetStatus() const noexcept { return status_code_; }

  opentelemetry::nostd::string_view GetDescription() const noexcept { return status_desc_; }

  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_;
  }

  const opentelemetry::sdk::trace::InstrumentationScope &GetInstrumentationScope() const noexcept
  {
    if (instrumentation_scope_ == nullptr)
    {
      // this shouldn't happen as Tracer ensures there is valid default instrumentation scope.
      static std::unique_ptr<opentelemetry::sdk::instrumentationscope::InstrumentationScope>
          instrumentation_scope =
              opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create(
                  "unknown_service");
      return *instrumentation_scope;
    }
    return *instrumentation_scope_;
  }

  OPENTELEMETRY_DEPRECATED_MESSAGE("Please use GetInstrumentationScope instead")
  const opentelemetry::sdk::trace::InstrumentationScope &GetInstrumentationLibrary() const noexcept
  {
    return GetInstrumentationScope();
  }

  opentelemetry::common::SystemTimestamp GetStartTime() const noexcept { return start_time_; }

  std::chrono::nanoseconds GetDuration() const noexcept { return duration_; }

  const std::unordered_map<std::string, common::OwnedAttributeValue> &GetAttributes() const noexcept
  {
    return attribute_map_.GetAttributes();
  }

  const std::vector<SpanDataEvent> &GetEvents() const noexcept { return events_; }

  const std::vector<SpanDataLink> &GetLinks() const noexcept { return links_; }

  void SetIdentity(const opentelemetry::trace::SpanContext &span_context,
                   opentelemetry::trace::SpanId parent_span_id) noexcept override
  {
    span_context_   = span_context;
    parent_span_id_ = parent_span_id;
  }

  void SetAttribute(nostd::string_view key,
                    const opentelemetry::common::AttributeValue &value) noexcept override
  {
    attribute_map_.SetAttribute(key, value);
  }

  void AddEvent(nostd::string_view name,
                opentelemetry::common::SystemTimestamp timestamp =
                    opentelemetry::common::SystemTimestamp(std::chrono::system_clock::now()),
                const opentelemetry::common::KeyValueIterable &attributes =
                    opentelemetry::common::KeyValueIterableView<std::map<std::string, int>>(
                        {})) noexcept override
  {
    SpanDataEvent event(std::string(name), timestamp, attributes);
    events_.push_back(event);
  }

  void AddLink(const opentelemetry::trace::SpanContext &span_context,
               const opentelemetry::common::KeyValueIterable &attributes) noexcept override
  {
    SpanDataLink link(span_context, attributes);
    links_.push_back(link);
  }

  void SetStatus(opentelemetry::trace::StatusCode code,
                 nostd::string_view description) noexcept override
  {
    status_code_ = code;
    status_desc_ = std::string(description);
  }

  void SetName(nostd::string_view name) noexcept override
  {
    name_ = std::string(name.data(), name.length());
  }

  void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override
  {
    span_kind_ = span_kind;
  }

  void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override
  {
    resource_ = &resource;
  }

  void SetStartTime(opentelemetry::common::SystemTimestamp start_time) noexcept override
  {
    start_time_ = start_time;
  }

  void SetDuration(std::chrono::nanoseconds duration) noexcept override { duration_ = duration; }

  void SetInstrumentationScope(const InstrumentationScope &instrumentation_scope) noexcept override
  {
    instrumentation_scope_ = &instrumentation_scope;
  }

private:
  opentelemetry::trace::SpanContext span_context_{false, false};
  opentelemetry::trace::SpanId parent_span_id_;
  opentelemetry::common::SystemTimestamp start_time_;
  std::chrono::nanoseconds duration_{0};
  std::string name_;
  opentelemetry::trace::StatusCode status_code_{opentelemetry::trace::StatusCode::kUnset};
  std::string status_desc_;
  common::AttributeMap attribute_map_;
  std::vector<SpanDataEvent> events_;
  std::vector<SpanDataLink> links_;
  opentelemetry::trace::SpanKind span_kind_{opentelemetry::trace::SpanKind::kInternal};
  const opentelemetry::sdk::resource::Resource *resource_;
  const InstrumentationScope *instrumentation_scope_;
};
}  // namespace trace
}  // namespace sdk
OPENTELEMETRY_END_NAMESPACE