observer_result.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/common/attribute_value.h"
  5. #include "opentelemetry/common/key_value_iterable_view.h"
  6. #include "opentelemetry/nostd/shared_ptr.h"
  7. #include "opentelemetry/nostd/span.h"
  8. #include "opentelemetry/nostd/string_view.h"
  9. #include "opentelemetry/nostd/type_traits.h"
  10. #include "opentelemetry/nostd/variant.h"
  11. #include "opentelemetry/version.h"
  12. OPENTELEMETRY_BEGIN_NAMESPACE
  13. namespace metrics
  14. {
  15. /**
  16. * ObserverResultT class is necessary for the callback recording asynchronous
  17. * instrument use.
  18. */
  19. template <class T>
  20. class ObserverResultT
  21. {
  22. public:
  23. virtual ~ObserverResultT() = default;
  24. virtual void Observe(T value) noexcept = 0;
  25. virtual void Observe(T value, const common::KeyValueIterable &attributes) noexcept = 0;
  26. template <class U,
  27. nostd::enable_if_t<common::detail::is_key_value_iterable<U>::value> * = nullptr>
  28. void Observe(T value, const U &attributes) noexcept
  29. {
  30. this->Observe(value, common::KeyValueIterableView<U>{attributes});
  31. }
  32. void Observe(T value,
  33. std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>>
  34. attributes) noexcept
  35. {
  36. this->Observe(value, nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>{
  37. attributes.begin(), attributes.end()});
  38. }
  39. };
  40. using ObserverResult = nostd::variant<nostd::shared_ptr<ObserverResultT<int64_t>>,
  41. nostd::shared_ptr<ObserverResultT<double>>>;
  42. } // namespace metrics
  43. OPENTELEMETRY_END_NAMESPACE