NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/sdk/metrics/state/sync_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 <stddef.h>
7 #include <stdint.h>
8 #include <functional>
9 #include <memory>
10 #include <mutex>
11 
12 #include "opentelemetry/common/key_value_iterable.h"
13 #include "opentelemetry/common/spin_lock_mutex.h"
14 #include "opentelemetry/common/timestamp.h"
15 #include "opentelemetry/context/context.h"
16 #include "opentelemetry/nostd/function_ref.h"
17 #include "opentelemetry/nostd/span.h"
18 #include "opentelemetry/nostd/string_view.h"
19 #include "opentelemetry/sdk/common/attributemap_hash.h"
20 #include "opentelemetry/sdk/metrics/aggregation/aggregation.h"
21 #include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
22 #include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
23 #include "opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h"
24 #include "opentelemetry/sdk/metrics/data/metric_data.h"
25 #include "opentelemetry/sdk/metrics/instruments.h"
26 #include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
27 #include "opentelemetry/sdk/metrics/state/metric_collector.h"
28 #include "opentelemetry/sdk/metrics/state/metric_storage.h"
29 #include "opentelemetry/sdk/metrics/state/temporal_metric_storage.h"
30 #include "opentelemetry/sdk/metrics/view/attributes_processor.h"
31 #include "opentelemetry/version.h"
32 
33 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
34 # include "opentelemetry/sdk/metrics/exemplar/filter_type.h"
35 # include "opentelemetry/sdk/metrics/exemplar/reservoir.h"
36 #endif
37 
39 namespace sdk
40 {
41 namespace metrics
42 {
44 {
45 
46 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
47 
48  static inline bool EnableExamplarFilter(ExemplarFilterType filter_type,
49  const opentelemetry::context::Context &context)
50  {
51  return filter_type == ExemplarFilterType::kAlwaysOn ||
52  (filter_type == ExemplarFilterType::kTraceBased &&
53  opentelemetry::trace::GetSpan(context)->GetContext().IsValid() &&
54  opentelemetry::trace::GetSpan(context)->GetContext().IsSampled());
55  }
56 
57 #endif // ENABLE_METRICS_EXEMPLAR_PREVIEW
58 
59 public:
60  SyncMetricStorage(InstrumentDescriptor instrument_descriptor,
61  const AggregationType aggregation_type,
62  std::shared_ptr<const AttributesProcessor> attributes_processor,
63 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
64  ExemplarFilterType exempler_filter_type,
65  nostd::shared_ptr<ExemplarReservoir> &&exemplar_reservoir,
66 #endif
67  const AggregationConfig *aggregation_config)
68  : instrument_descriptor_(instrument_descriptor),
69  aggregation_config_(AggregationConfig::GetOrDefault(aggregation_config)),
70  attributes_hashmap_(
71  std::make_unique<AttributesHashMap>(aggregation_config_->cardinality_limit_)),
72  attributes_processor_(std::move(attributes_processor)),
73 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
74  exemplar_filter_type_(exempler_filter_type),
75  exemplar_reservoir_(exemplar_reservoir),
76 #endif
77  temporal_metric_storage_(instrument_descriptor, aggregation_type, aggregation_config)
78  {
79  create_default_aggregation_ = [&, aggregation_type,
80  aggregation_config]() -> std::unique_ptr<Aggregation> {
81  return DefaultAggregation::CreateAggregation(aggregation_type, instrument_descriptor_,
82  aggregation_config);
83  };
84  }
85 
86  void RecordLong(int64_t value,
87  const opentelemetry::context::Context &context
88  OPENTELEMETRY_MAYBE_UNUSED) noexcept override
89  {
90  if (instrument_descriptor_.value_type_ != InstrumentValueType::kLong)
91  {
92  return;
93  }
94 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
95  if (EnableExamplarFilter(exemplar_filter_type_, context))
96  {
97  exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now());
98  }
99 #endif
100  static MetricAttributes attr = MetricAttributes{};
101  std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
102  attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value);
103  }
104 
105  void RecordLong(int64_t value,
106  const opentelemetry::common::KeyValueIterable &attributes,
107  const opentelemetry::context::Context &context
108  OPENTELEMETRY_MAYBE_UNUSED) noexcept override
109  {
110  if (instrument_descriptor_.value_type_ != InstrumentValueType::kLong)
111  {
112  return;
113  }
114 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
115  if (EnableExamplarFilter(exemplar_filter_type_, context))
116  {
117  exemplar_reservoir_->OfferMeasurement(value, attributes, context,
118  std::chrono::system_clock::now());
119  }
120 #endif
121 
122  std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
123  attributes_hashmap_
124  ->GetOrSetDefault(attributes, attributes_processor_.get(), create_default_aggregation_)
125  ->Aggregate(value);
126  }
127 
128  void RecordDouble(double value,
129  const opentelemetry::context::Context &context
130  OPENTELEMETRY_MAYBE_UNUSED) noexcept override
131  {
132  if (instrument_descriptor_.value_type_ != InstrumentValueType::kDouble)
133  {
134  return;
135  }
136 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
137  if (EnableExamplarFilter(exemplar_filter_type_, context))
138  {
139  exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now());
140  }
141 #endif
142  static MetricAttributes attr = MetricAttributes{};
143  std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
144  attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value);
145  }
146 
147  void RecordDouble(double value,
148  const opentelemetry::common::KeyValueIterable &attributes,
149  const opentelemetry::context::Context &context
150  OPENTELEMETRY_MAYBE_UNUSED) noexcept override
151  {
152  if (instrument_descriptor_.value_type_ != InstrumentValueType::kDouble)
153  {
154  return;
155  }
156 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
157  if (EnableExamplarFilter(exemplar_filter_type_, context))
158  {
159  exemplar_reservoir_->OfferMeasurement(value, attributes, context,
160  std::chrono::system_clock::now());
161  }
162 #endif
163  std::lock_guard<opentelemetry::common::SpinLockMutex> guard(attribute_hashmap_lock_);
164  attributes_hashmap_
165  ->GetOrSetDefault(attributes, attributes_processor_.get(), create_default_aggregation_)
166  ->Aggregate(value);
167  }
168 
169  bool Collect(CollectorHandle *collector,
170  nostd::span<std::shared_ptr<CollectorHandle>> collectors,
171  opentelemetry::common::SystemTimestamp sdk_start_ts,
172  opentelemetry::common::SystemTimestamp collection_ts,
173  nostd::function_ref<bool(MetricData)> callback) noexcept override;
174 
175 private:
176  InstrumentDescriptor instrument_descriptor_;
177  // hashmap to maintain the metrics for delta collection (i.e, collection since last Collect call)
178  const AggregationConfig *aggregation_config_;
179  std::unique_ptr<AttributesHashMap> attributes_hashmap_;
180  std::function<std::unique_ptr<Aggregation>()> create_default_aggregation_;
181  std::shared_ptr<const AttributesProcessor> attributes_processor_;
182 #ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
183  ExemplarFilterType exemplar_filter_type_;
184  nostd::shared_ptr<ExemplarReservoir> exemplar_reservoir_;
185 #endif
186  TemporalMetricStorage temporal_metric_storage_;
187  opentelemetry::common::SpinLockMutex attribute_hashmap_lock_;
188 };
189 
190 } // namespace metrics
191 } // namespace sdk
OPENTELEMETRY_MAYBE_UNUSED
#define OPENTELEMETRY_MAYBE_UNUSED
Declare variable as maybe unused usage: OPENTELEMETRY_MAYBE_UNUSED int a; class OPENTELEMETRY_MAYBE_U...
Definition: includes/opentelemetry/common/macros.h:196
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
prometheus::detail::make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: includes/prometheus/detail/future_std.h:28
sdk::metrics::TemporalMetricStorage
Definition: includes/opentelemetry/sdk/metrics/state/temporal_metric_storage.h:32
sdk::metrics::SyncMetricStorage::RecordLong
void RecordLong(int64_t value, const opentelemetry::context::Context &context OPENTELEMETRY_MAYBE_UNUSED) noexcept override
Definition: includes/opentelemetry/sdk/metrics/state/sync_metric_storage.h:86
sdk::metrics::SyncMetricStorage
Definition: includes/opentelemetry/sdk/metrics/state/sync_metric_storage.h:43
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
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::SyncMetricStorage::RecordDouble
void RecordDouble(double value, const opentelemetry::common::KeyValueIterable &attributes, const opentelemetry::context::Context &context OPENTELEMETRY_MAYBE_UNUSED) noexcept override
Definition: includes/opentelemetry/sdk/metrics/state/sync_metric_storage.h:147
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
sdk::metrics::SyncWritableMetricStorage
Definition: includes/opentelemetry/sdk/metrics/state/metric_storage.h:47
sdk::metrics::AttributesHashMapWithCustomHash
Definition: includes/opentelemetry/sdk/metrics/state/attributes_hashmap.h:47
nostd::span
Back port of std::span.
Definition: includes/opentelemetry/nostd/span.h:51
sdk::metrics::SyncMetricStorage::SyncMetricStorage
SyncMetricStorage(InstrumentDescriptor instrument_descriptor, const AggregationType aggregation_type, std::shared_ptr< const AttributesProcessor > attributes_processor, const AggregationConfig *aggregation_config)
Definition: includes/opentelemetry/sdk/metrics/state/sync_metric_storage.h:60
context
Definition: includes/opentelemetry/context/context.h:16
sdk::metrics::MetricStorage
Definition: includes/opentelemetry/sdk/metrics/state/metric_storage.h:32
sdk::metrics::InstrumentValueType::kLong
@ kLong
sdk::metrics::SyncMetricStorage::RecordDouble
void RecordDouble(double value, const opentelemetry::context::Context &context OPENTELEMETRY_MAYBE_UNUSED) noexcept override
Definition: includes/opentelemetry/sdk/metrics/state/sync_metric_storage.h:128
sdk::metrics::MetricAttributes
opentelemetry::sdk::metrics::FilteredOrderedAttributeMap MetricAttributes
Definition: includes/opentelemetry/sdk/metrics/instruments.h:216
trace::GetSpan
nostd::shared_ptr< Span > GetSpan(const context::Context &context) noexcept
Definition: includes/opentelemetry/trace/context.h:16
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::MetricData
Definition: includes/opentelemetry/sdk/metrics/data/metric_data.h:33
sdk::metrics::SyncMetricStorage::RecordLong
void RecordLong(int64_t value, const opentelemetry::common::KeyValueIterable &attributes, const opentelemetry::context::Context &context OPENTELEMETRY_MAYBE_UNUSED) noexcept override
Definition: includes/opentelemetry/sdk/metrics/state/sync_metric_storage.h:105
std
Extensions to the C++ standard library.
Definition: includes/opentelemetry/nostd/string_view.h:212
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::SyncMetricStorage::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)> callback) noexcept override