noop.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. // Please refer to provider.h for documentation on how to obtain a Tracer object.
  5. //
  6. // This file is part of the internal implementation of OpenTelemetry. Nothing in this file should be
  7. // used directly. Please refer to span.h and tracer.h for documentation on these interfaces.
  8. #include <stdint.h>
  9. #include <utility>
  10. #include "opentelemetry/common/attribute_value.h"
  11. #include "opentelemetry/common/key_value_iterable.h"
  12. #include "opentelemetry/common/timestamp.h"
  13. #include "opentelemetry/context/context_value.h"
  14. #include "opentelemetry/nostd/shared_ptr.h"
  15. #include "opentelemetry/nostd/string_view.h"
  16. #include "opentelemetry/nostd/unique_ptr.h"
  17. #include "opentelemetry/trace/span.h"
  18. #include "opentelemetry/trace/span_context.h"
  19. #include "opentelemetry/trace/span_context_kv_iterable.h"
  20. #include "opentelemetry/trace/span_metadata.h"
  21. #include "opentelemetry/trace/span_startoptions.h"
  22. #include "opentelemetry/trace/tracer.h"
  23. #include "opentelemetry/trace/tracer_provider.h"
  24. #include "opentelemetry/version.h"
  25. namespace trace_api = opentelemetry::trace;
  26. OPENTELEMETRY_BEGIN_NAMESPACE
  27. namespace trace
  28. {
  29. /**
  30. * No-op implementation of Span. This class should not be used directly.
  31. */
  32. class OPENTELEMETRY_EXPORT NoopSpan final : public Span
  33. {
  34. public:
  35. explicit NoopSpan(const std::shared_ptr<Tracer> &tracer) noexcept
  36. : tracer_{tracer}, span_context_{new SpanContext(false, false)}
  37. {}
  38. explicit NoopSpan(const std::shared_ptr<Tracer> &tracer,
  39. nostd::unique_ptr<SpanContext> span_context) noexcept
  40. : tracer_{tracer}, span_context_{std::move(span_context)}
  41. {}
  42. void SetAttribute(nostd::string_view /*key*/,
  43. const common::AttributeValue & /*value*/) noexcept override
  44. {}
  45. void AddEvent(nostd::string_view /*name*/) noexcept override {}
  46. void AddEvent(nostd::string_view /*name*/,
  47. common::SystemTimestamp /*timestamp*/) noexcept override
  48. {}
  49. void AddEvent(nostd::string_view /* name */,
  50. const common::KeyValueIterable & /* attributes */) noexcept override
  51. {}
  52. void AddEvent(nostd::string_view /*name*/,
  53. common::SystemTimestamp /*timestamp*/,
  54. const common::KeyValueIterable & /*attributes*/) noexcept override
  55. {}
  56. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  57. void AddLink(const SpanContext & /* target */,
  58. const common::KeyValueIterable & /* attrs */) noexcept override
  59. {}
  60. void AddLinks(const SpanContextKeyValueIterable & /* links */) noexcept override {}
  61. #endif
  62. void SetStatus(StatusCode /*code*/, nostd::string_view /*description*/) noexcept override {}
  63. void UpdateName(nostd::string_view /*name*/) noexcept override {}
  64. void End(const EndSpanOptions & /*options*/) noexcept override {}
  65. bool IsRecording() const noexcept override { return false; }
  66. SpanContext GetContext() const noexcept override { return *span_context_.get(); }
  67. private:
  68. std::shared_ptr<Tracer> tracer_;
  69. nostd::unique_ptr<SpanContext> span_context_;
  70. };
  71. /**
  72. * No-op implementation of Tracer.
  73. */
  74. class OPENTELEMETRY_EXPORT NoopTracer final : public Tracer,
  75. public std::enable_shared_from_this<NoopTracer>
  76. {
  77. public:
  78. // Tracer
  79. NoopTracer()
  80. {
  81. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  82. UpdateEnabled(false);
  83. #endif
  84. }
  85. nostd::shared_ptr<Span> StartSpan(nostd::string_view /*name*/,
  86. const common::KeyValueIterable & /*attributes*/,
  87. const SpanContextKeyValueIterable & /*links*/,
  88. const StartSpanOptions & /*options*/) noexcept override
  89. {
  90. // Don't allocate a no-op span for every StartSpan call, but use a static
  91. // singleton for this case.
  92. static nostd::shared_ptr<trace::Span> noop_span(new trace::NoopSpan{this->shared_from_this()});
  93. return noop_span;
  94. }
  95. #if OPENTELEMETRY_ABI_VERSION_NO == 1
  96. void ForceFlushWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
  97. void CloseWithMicroseconds(uint64_t /*timeout*/) noexcept override {}
  98. #endif /* OPENTELEMETRY_ABI_VERSION_NO */
  99. };
  100. /**
  101. * No-op implementation of a TracerProvider.
  102. */
  103. class OPENTELEMETRY_EXPORT NoopTracerProvider final : public trace::TracerProvider
  104. {
  105. public:
  106. NoopTracerProvider() noexcept
  107. : tracer_{nostd::shared_ptr<trace::NoopTracer>(new trace::NoopTracer)}
  108. {}
  109. #if OPENTELEMETRY_ABI_VERSION_NO >= 2
  110. nostd::shared_ptr<trace::Tracer> GetTracer(
  111. nostd::string_view /* name */,
  112. nostd::string_view /* version */,
  113. nostd::string_view /* schema_url */,
  114. const common::KeyValueIterable * /* attributes */) noexcept override
  115. {
  116. return tracer_;
  117. }
  118. #else
  119. nostd::shared_ptr<trace::Tracer> GetTracer(nostd::string_view /* name */,
  120. nostd::string_view /* version */,
  121. nostd::string_view /* schema_url */) noexcept override
  122. {
  123. return tracer_;
  124. }
  125. #endif
  126. private:
  127. nostd::shared_ptr<trace::Tracer> tracer_;
  128. };
  129. } // namespace trace
  130. OPENTELEMETRY_END_NAMESPACE