1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
5 #ifdef ENABLE_METRICS_PREVIEW
12 # include <unordered_map>
14 # include "opentelemetry/_metrics/instrument.h"
15 # include "opentelemetry/sdk/_metrics/aggregator/aggregator.h"
16 # include "opentelemetry/sdk/_metrics/record.h"
17 # include "opentelemetry/version.h"
19 OPENTELEMETRY_BEGIN_NAMESPACE
25 # if defined(_MSC_VER)
26 # pragma warning(push)
27 # pragma warning(disable : 4250) // inheriting methods via dominance
30 class Instrument
: virtual public opentelemetry::metrics::Instrument
34 Instrument() = default;
36 Instrument(nostd::string_view name
,
37 nostd::string_view description
,
38 nostd::string_view unit
,
40 opentelemetry::metrics::InstrumentKind kind
)
41 : name_(name
), description_(description
), unit_(unit
), enabled_(enabled
), kind_(kind
)
44 // Returns true if the instrument is enabled and collecting data
45 virtual bool IsEnabled() override
{ return enabled_
; }
47 // Return the instrument name
48 virtual nostd::string_view
GetName() override
{ return name_
; }
50 // Return the instrument description
51 virtual nostd::string_view
GetDescription() override
{ return description_
; }
53 // Return the insrument's units of measurement
54 virtual nostd::string_view
GetUnits() override
{ return unit_
; }
56 virtual opentelemetry::metrics::InstrumentKind
GetKind() override
{ return this->kind_
; }
60 std::string description_
;
64 opentelemetry::metrics::InstrumentKind kind_
;
68 class BoundSynchronousInstrument
70 virtual public opentelemetry::metrics::BoundSynchronousInstrument
<T
>
74 BoundSynchronousInstrument() = default;
76 BoundSynchronousInstrument(nostd::string_view name
,
77 nostd::string_view description
,
78 nostd::string_view unit
,
80 opentelemetry::metrics::InstrumentKind kind
,
81 std::shared_ptr
<Aggregator
<T
>> agg
)
82 : Instrument(name
, description
, unit
, enabled
, kind
), agg_(agg
)
84 this->inc_ref(); // increase reference count when instantiated
88 * Frees the resources associated with this Bound Instrument.
89 * The Metric from which this instrument was created is not impacted.
94 virtual void unbind() override
102 * Increments the reference count. This function is used when binding or instantiating.
107 virtual void inc_ref() override
115 * Returns the current reference count of the instrument. This value is used to
116 * later in the pipeline remove stale instruments.
119 * @return current ref count of the instrument
121 virtual int get_ref() override
130 * Records a single synchronous metric event via a call to the aggregator.
131 * Since this is a bound synchronous instrument, labels are not required in
132 * metric capture calls.
134 * @param value is the numerical representation of the metric being captured
137 virtual void update(T value
) override
145 * Returns the aggregator responsible for meaningfully combining update values.
148 * @return the aggregator assigned to this instrument
150 virtual std::shared_ptr
<Aggregator
<T
>> GetAggregator() final
{ return agg_
; }
153 std::shared_ptr
<Aggregator
<T
>> agg_
;
158 class SynchronousInstrument
: public Instrument
,
159 virtual public opentelemetry::metrics::SynchronousInstrument
<T
>
163 SynchronousInstrument() = default;
165 SynchronousInstrument(nostd::string_view name
,
166 nostd::string_view description
,
167 nostd::string_view unit
,
169 opentelemetry::metrics::InstrumentKind kind
)
170 : Instrument(name
, description
, unit
, enabled
, kind
)
174 * Returns a Bound Instrument associated with the specified labels. Multiples requests
175 * with the same set of labels may return the same Bound Instrument instance.
177 * It is recommended that callers keep a reference to the Bound Instrument
178 * instead of repeatedly calling this operation.
180 * @param labels the set of labels, as key-value pairs
181 * @return a Bound Instrument
183 virtual nostd::shared_ptr
<opentelemetry::metrics::BoundSynchronousInstrument
<T
>> bind(
184 const opentelemetry::common::KeyValueIterable
&labels
) override
186 return nostd::shared_ptr
<BoundSynchronousInstrument
<T
>>();
189 // This function is necessary for batch recording and should NOT be called by the user
190 virtual void update(T value
, const opentelemetry::common::KeyValueIterable
&labels
) override
= 0;
193 * Checkpoints instruments and returns a set of records which are ready for processing.
194 * This method should ONLY be called by the Meter Class as part of the export pipeline
195 * as it also prunes bound instruments with no active references.
198 * @return vector of Records which hold the data attached to this synchronous instrument
200 virtual std::vector
<Record
> GetRecords() = 0;
204 class AsynchronousInstrument
: public Instrument
,
205 virtual public opentelemetry::metrics::AsynchronousInstrument
<T
>
209 AsynchronousInstrument() = default;
211 AsynchronousInstrument(nostd::string_view name
,
212 nostd::string_view description
,
213 nostd::string_view unit
,
215 void (*callback
)(opentelemetry::metrics::ObserverResult
<T
>),
216 opentelemetry::metrics::InstrumentKind kind
)
217 : Instrument(name
, description
, unit
, enabled
, kind
)
219 this->callback_
= callback
;
223 * Captures data through a manual call rather than the automatic collection process instituted
224 * in the run function. Asynchronous instruments are generally expected to obtain data from
225 * their callbacks rather than direct calls. This function is used by the callback to store data.
227 * @param value is the numerical representation of the metric being captured
228 * @param labels is the numerical representation of the metric being captured
231 virtual void observe(T value
, const opentelemetry::common::KeyValueIterable
&labels
) override
= 0;
233 virtual std::vector
<Record
> GetRecords() = 0;
236 * Captures data by activating the callback function associated with the
237 * instrument and storing its return value. Callbacks for asynchronous
238 * instruments are defined during construction.
243 virtual void run() override
= 0;
246 // Helper functions for turning a common::KeyValueIterable into a string
247 inline void print_value(std::stringstream
&ss
,
248 opentelemetry::common::AttributeValue
&value
,
249 bool jsonTypes
= false)
251 switch (value
.index())
253 case opentelemetry::common::AttributeType::kTypeString
:
255 ss
<< nostd::get
<nostd::string_view
>(value
);
260 throw std::invalid_argument("Labels must be strings");
268 // Utility function which converts maps to strings for better performance
269 inline std::string
mapToString(const std::map
<std::string
, std::string
> &conv
)
271 std::stringstream ss
;
275 ss
<< i
.first
<< ':' << i
.second
<< ',';
281 inline std::string
KvToString(const opentelemetry::common::KeyValueIterable
&kv
) noexcept
283 std::stringstream ss
;
285 size_t size
= kv
.size();
290 [&](nostd::string_view key
, opentelemetry::common::AttributeValue value
) noexcept
{
292 print_value(ss
, value
, true);
305 # if defined(_MSC_VER)
306 # pragma warning(pop)
309 } // namespace metrics
311 OPENTELEMETRY_END_NAMESPACE