NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/sdk/metrics/state/async_metric_storage.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <memory>
7 #include <mutex>
8 #include <unordered_map>
9 
10 #include "opentelemetry/nostd/shared_ptr.h"
11 #include "opentelemetry/sdk/common/attributemap_hash.h"
12 #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
13 #include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
14 
15 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
16 # include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
17 # include "opentelemetry/sdk/metrics/exemplar/reservoir.h"
18 #endif
19 
20 #include "opentelemetry/sdk/metrics/instruments.h"
21 #include "opentelemetry/sdk/metrics/observer_result.h"
22 #include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
23 #include "opentelemetry/sdk/metrics/state/metric_collector.h"
24 #include "opentelemetry/sdk/metrics/state/metric_storage.h"
25 #include "opentelemetry/sdk/metrics/state/temporal_metric_storage.h"
26 #include "opentelemetry/sdk/metrics/view/attributes_processor.h"
27 
29 namespace sdk
30 {
31 namespace metrics
32 {
33 
35 {
36 public:
38  const AggregationType aggregation_type,
39 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
40  ExemplarFilterType exempler_filter_type,
41  nostd::shared_ptr<ExemplarReservoir> &&exemplar_reservoir,
42 #endif
43  const AggregationConfig *aggregation_config)
44  : instrument_descriptor_(instrument_descriptor),
45  aggregation_type_{aggregation_type},
46  aggregation_config_{AggregationConfig::GetOrDefault(aggregation_config)},
47  cumulative_hash_map_(
48  std::make_unique<AttributesHashMap>(aggregation_config_->cardinality_limit_)),
49  delta_hash_map_(
50  std::make_unique<AttributesHashMap>(aggregation_config_->cardinality_limit_)),
51 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
52  exemplar_filter_type_(exempler_filter_type),
53  exemplar_reservoir_(exemplar_reservoir),
54 #endif
55  temporal_metric_storage_(instrument_descriptor, aggregation_type, aggregation_config)
56  {}
57 
58  template <class T>
59  void Record(const std::unordered_map<MetricAttributes, T, AttributeHashGenerator> &measurements,
60  opentelemetry::common::SystemTimestamp /* observation_time */) noexcept
61  {
62  // Async counter always record monotonically increasing values, and the
63  // exporter/reader can request either for delta or cumulative value.
64  // So we convert the async counter value to delta before passing it to temporal storage.
65  std::lock_guard<opentelemetry::common::SpinLockMutex> guard(hashmap_lock_);
66  for (auto &measurement : measurements)
67  {
68 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
69  if (exemplar_filter_type_ == ExemplarFilterType::kAlwaysOn)
70  {
71  exemplar_reservoir_->OfferMeasurement(measurement.second, {}, {},
72  std::chrono::system_clock::now());
73  }
74 #endif
75 
76  auto aggr = DefaultAggregation::CreateAggregation(aggregation_type_, instrument_descriptor_);
77  aggr->Aggregate(measurement.second);
78  auto prev = cumulative_hash_map_->Get(measurement.first);
79  if (prev)
80  {
81  auto delta = prev->Diff(*aggr);
82  // store received value in cumulative map, and the diff in delta map (to pass it to temporal
83  // storage)
84  cumulative_hash_map_->Set(measurement.first, std::move(aggr));
85  delta_hash_map_->Set(measurement.first, std::move(delta));
86  }
87  else
88  {
89  // store received value in cumulative and delta map.
90  cumulative_hash_map_->Set(
91  measurement.first,
92  DefaultAggregation::CloneAggregation(aggregation_type_, instrument_descriptor_, *aggr));
93  delta_hash_map_->Set(measurement.first, std::move(aggr));
94  }
95  }
96  }
97 
98  void RecordLong(
99  const std::unordered_map<MetricAttributes, int64_t, AttributeHashGenerator> &measurements,
100  opentelemetry::common::SystemTimestamp observation_time) noexcept override
101  {
102  if (instrument_descriptor_.value_type_ != InstrumentValueType::kLong)
103  {
104  return;
105  }
106  Record<int64_t>(measurements, observation_time);
107  }
108 
110  const std::unordered_map<MetricAttributes, double, AttributeHashGenerator> &measurements,
111  opentelemetry::common::SystemTimestamp observation_time) noexcept override
112  {
113  if (instrument_descriptor_.value_type_ != InstrumentValueType::kDouble)
114  {
115  return;
116  }
117  Record<double>(measurements, observation_time);
118  }
119 
120  bool Collect(CollectorHandle *collector,
121  nostd::span<std::shared_ptr<CollectorHandle>> collectors,
122  opentelemetry::common::SystemTimestamp sdk_start_ts,
123  opentelemetry::common::SystemTimestamp collection_ts,
124  nostd::function_ref<bool(MetricData)> metric_collection_callback) noexcept override
125  {
126 
127  std::shared_ptr<AttributesHashMap> delta_metrics = nullptr;
128  {
129  std::lock_guard<opentelemetry::common::SpinLockMutex> guard(hashmap_lock_);
130  delta_metrics = std::move(delta_hash_map_);
131  delta_hash_map_ =
132  std::make_unique<AttributesHashMap>(aggregation_config_->cardinality_limit_);
133  }
134 
135  auto status =
136  temporal_metric_storage_.buildMetrics(collector, collectors, sdk_start_ts, collection_ts,
137  delta_metrics, metric_collection_callback);
138  return status;
139  }
140 
141 private:
142  InstrumentDescriptor instrument_descriptor_;
143  AggregationType aggregation_type_;
144  const AggregationConfig *aggregation_config_;
145  std::unique_ptr<AttributesHashMap> cumulative_hash_map_;
146  std::unique_ptr<AttributesHashMap> delta_hash_map_;
147  opentelemetry::common::SpinLockMutex hashmap_lock_;
148 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
149  ExemplarFilterType exemplar_filter_type_;
150  nostd::shared_ptr<ExemplarReservoir> exemplar_reservoir_;
151 #endif
152  TemporalMetricStorage temporal_metric_storage_;
153 };
154 
155 } // namespace metrics
156 } // namespace sdk
sdk::metrics::AggregationType
AggregationType
Definition: includes/opentelemetry/sdk/metrics/instruments.h:44
nostd::function_ref
Definition: includes/opentelemetry/nostd/function_ref.h:17
sdk::metrics::InstrumentValueType::kDouble
@ kDouble
sdk::metrics::TemporalMetricStorage
Definition: includes/opentelemetry/sdk/metrics/state/temporal_metric_storage.h:32
sdk::metrics::AsyncMetricStorage::RecordLong
void RecordLong(const std::unordered_map< MetricAttributes, int64_t, AttributeHashGenerator > &measurements, opentelemetry::common::SystemTimestamp observation_time) noexcept override
Definition: includes/opentelemetry/sdk/metrics/state/async_metric_storage.h:98
sdk::metrics::AggregationConfig
Definition: includes/opentelemetry/sdk/metrics/aggregation/aggregation_config.h:16
sdk::metrics::CollectorHandle
Definition: includes/opentelemetry/sdk/metrics/state/metric_collector.h:25
sdk::metrics::AsyncWritableMetricStorage
Definition: includes/opentelemetry/sdk/metrics/state/metric_storage.h:68
nostd::shared_ptr
Provide a type-erased version of std::shared_ptr that has ABI stability.
Definition: includes/opentelemetry/nostd/shared_ptr.h:28
sdk::metrics::AggregationConfig::GetOrDefault
static const AggregationConfig * GetOrDefault(const AggregationConfig *config)
Definition: includes/opentelemetry/sdk/metrics/aggregation/aggregation_config.h:23
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
sdk::metrics::AsyncMetricStorage::RecordDouble
void RecordDouble(const std::unordered_map< MetricAttributes, double, AttributeHashGenerator > &measurements, opentelemetry::common::SystemTimestamp observation_time) noexcept override
Definition: includes/opentelemetry/sdk/metrics/state/async_metric_storage.h:109
nostd::span
Back port of std::span.
Definition: includes/opentelemetry/nostd/span.h:51
sdk::metrics::MetricStorage
Definition: includes/opentelemetry/sdk/metrics/state/metric_storage.h:32
sdk::metrics::InstrumentValueType::kLong
@ kLong
sdk::metrics::AsyncMetricStorage
Definition: includes/opentelemetry/sdk/metrics/state/async_metric_storage.h:34
sdk::metrics::TemporalMetricStorage::buildMetrics
bool buildMetrics(CollectorHandle *collector, nostd::span< std::shared_ptr< CollectorHandle >> collectors, opentelemetry::common::SystemTimestamp sdk_start_ts, opentelemetry::common::SystemTimestamp collection_ts, const std::shared_ptr< AttributesHashMap > &delta_metrics, nostd::function_ref< bool(MetricData)> callback) noexcept
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
absl::move
constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > && move(T &&t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:199
sdk::metrics::AsyncMetricStorage::Record
void Record(const std::unordered_map< MetricAttributes, T, AttributeHashGenerator > &measurements, opentelemetry::common::SystemTimestamp) noexcept
Definition: includes/opentelemetry/sdk/metrics/state/async_metric_storage.h:59
sdk::metrics::AsyncMetricStorage::AsyncMetricStorage
AsyncMetricStorage(InstrumentDescriptor instrument_descriptor, const AggregationType aggregation_type, const AggregationConfig *aggregation_config)
Definition: includes/opentelemetry/sdk/metrics/state/async_metric_storage.h:37
sdk::metrics::MetricData
Definition: includes/opentelemetry/sdk/metrics/data/metric_data.h:33
sdk::metrics::AsyncMetricStorage::Collect
bool Collect(CollectorHandle *collector, nostd::span< std::shared_ptr< CollectorHandle >> collectors, opentelemetry::common::SystemTimestamp sdk_start_ts, opentelemetry::common::SystemTimestamp collection_ts, nostd::function_ref< bool(MetricData)> metric_collection_callback) noexcept override
Definition: includes/opentelemetry/sdk/metrics/state/async_metric_storage.h:120
sdk::metrics::DefaultAggregation::CloneAggregation
static std::unique_ptr< Aggregation > CloneAggregation(AggregationType aggregation_type, const InstrumentDescriptor &instrument_descriptor, const Aggregation &to_copy)
Definition: includes/opentelemetry/sdk/metrics/aggregation/default_aggregation.h:121
sdk
Definition: includes/opentelemetry/sdk/common/atomic_shared_ptr.h:13
metrics
Definition: includes/opentelemetry/metrics/async_instruments.h:10
sdk::metrics::InstrumentDescriptor
Definition: includes/opentelemetry/sdk/metrics/instruments.h:61
sdk::metrics::DefaultAggregation::CreateAggregation
static std::unique_ptr< Aggregation > CreateAggregation(const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor, const AggregationConfig *aggregation_config)
Definition: includes/opentelemetry/sdk/metrics/aggregation/default_aggregation.h:28
sdk::metrics::InstrumentDescriptor::value_type_
InstrumentValueType value_type_
Definition: includes/opentelemetry/sdk/metrics/instruments.h:67
sdk::metrics::AggregationConfig::cardinality_limit_
size_t cardinality_limit_
Definition: includes/opentelemetry/sdk/metrics/aggregation/aggregation_config.h:33